cs_lang:haskell
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| cs_lang:haskell [2012/04/29 11:44] – cedric | cs_lang:haskell [2012/04/30 16:51] (current) – [Caeser cipher] cedric | ||
|---|---|---|---|
| Line 8: | Line 8: | ||
| * strongly typed; | * strongly typed; | ||
| * inferred. | * inferred. | ||
| + | |||
| ====== Learning Haskell ====== | ====== Learning Haskell ====== | ||
| * [[http:// | * [[http:// | ||
| + | |||
| + | |||
| + | ====== Simple examples ====== | ||
| + | |||
| + | You can compare functions below with the [[cs_lang: | ||
| + | |||
| + | ===== Increment a number ===== | ||
| + | <code haskell> | ||
| + | let inc = (+) n | ||
| + | |||
| + | -- Alternative: | ||
| + | let inc n = succ n | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Caesar cipher ===== | ||
| + | |||
| + | <code haskell> | ||
| + | -- Shifts a character to the right if positive, left if negative. | ||
| + | shift :: Int -> Char -> Char | ||
| + | shift n c | isUpper c = chr $ ord ' | ||
| + | | isLower c = chr $ ord ' | ||
| + | | otherwise = c | ||
| + | |||
| + | |||
| + | encode :: Int -> String -> String | ||
| + | encode _ [] = [] | ||
| + | encode n xs = map (shift n) xs | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Parity ===== | ||
| + | <code haskell> | ||
| + | parity :: Integral a => [a] -> [Bool] | ||
| + | -- Using recursion | ||
| + | parity [] = [] | ||
| + | parity (x:list) = [even x] ++ parity list | ||
| + | |||
| + | -- Using map | ||
| + | parity list = map even list | ||
| + | </ | ||
cs_lang/haskell.1335692684.txt.gz · Last modified: by cedric
