codesnippets:binding
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| codesnippets:binding [2021/03/17 19:31] – f2b216 | codesnippets:binding [2021/04/13 21:05] (current) – removed f2b216 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Binding ====== | ||
| - | |||
| - | A binding binds a name or even several names with a function definition. | ||
| - | |||
| - | In Haskell, a binding is defined by symbol " | ||
| - | - on the left hand side | ||
| - | - the function name and its parameters | ||
| - | - or a pattern is given, | ||
| - | - and on the right had side the definition of the function. | ||
| - | ===== 1) with function name and parameter ===== | ||
| - | |||
| - | ==== Example ==== | ||
| - | |||
| - | <code Haskell> | ||
| - | half x = x / 2 | ||
| - | </ | ||
| - | |||
| - | ...works in... | ||
| - | |||
| - | <code Haskell> | ||
| - | module Main where | ||
| - | |||
| - | main :: IO () | ||
| - | main = | ||
| - | do | ||
| - | print $ half 5 | ||
| - | |||
| - | half x = x / 2 | ||
| - | </ | ||
| - | |||
| - | ===== 2) with pattern matching ===== | ||
| - | |||
| - | ==== Example 1 ==== | ||
| - | |||
| - | <code Haskell> | ||
| - | (x1, x2, x3) = (42, " | ||
| - | </ | ||
| - | |||
| - | ...works in... | ||
| - | |||
| - | <code Haskell> | ||
| - | module Main where | ||
| - | |||
| - | main :: IO () | ||
| - | main = | ||
| - | do | ||
| - | print x1 | ||
| - | print x2 | ||
| - | print x3 | ||
| - | |||
| - | (x1, x2, x3) = (42, " | ||
| - | </ | ||
| - | |||
| - | ==== Example 2 ==== | ||
| - | |||
| - | <code Haskell> | ||
| - | Person name number = Person " | ||
| - | </ | ||
| - | |||
| - | ...works in... | ||
| - | |||
| - | <code Haskell> | ||
| - | module Main where | ||
| - | |||
| - | main :: IO () | ||
| - | main = | ||
| - | do | ||
| - | print name | ||
| - | print number | ||
| - | |||
| - | data Person = Person { sName :: String, iNumber :: Integer } | ||
| - | |||
| - | Person name number = Person " | ||
| - | </ | ||
| - | |||
| - | ==== Example 3 ==== | ||
| - | |||
| - | <code Haskell> | ||
| - | (x1, Person x2 x3, x4) = (" | ||
| - | </ | ||
| - | |||
| - | ...works in... | ||
| - | |||
| - | <code Haskell> | ||
| - | module Main where | ||
| - | |||
| - | main :: IO () | ||
| - | main = | ||
| - | do | ||
| - | print x1 | ||
| - | print x2 | ||
| - | print x3 | ||
| - | print x4 | ||
| - | |||
| - | data Person = Person { sName :: String, iNumber :: Integer } | ||
| - | |||
| - | (x1, Person x2 x3, x4) = (" | ||
| - | </ | ||
| - | |||
| - | |||
codesnippets/binding.1616005887.txt.gz · Last modified: (external edit)
