Haskell ======= .. contents:: By analogy to Python... Imports ------- +-------------------------+-------------------------+-------------------------+ |Python |Haskell |Notes | +-------------------------+-------------------------+-------------------------+ |from module import * |import module |Import everything from | | | |the module directly into | | | |the namespace | +-------------------------+-------------------------+-------------------------+ |from module import a,b |import module (a,b) |Import selected items | | | |from the module directly | | | |into the namespace | +-------------------------+-------------------------+-------------------------+ |import module |import qualified module |Make all ``module.NAME`` | | | |available in the name | | | |space | +-------------------------+-------------------------+-------------------------+ |import module; |import qualified module |Give module a shorter | |M = module; |as M |alias | |del module | | | | | | | +-------------------------+-------------------------+-------------------------+ |n/a |import module hiding (c) |Import everything from | | | |the module directly into | | | |the namespec, except | | | |selected items | +-------------------------+-------------------------+-------------------------+ Math ---- +-------------------------+-------------------------+-------------------------+ |Python |Haskell |Notes | +-------------------------+-------------------------+-------------------------+ |a+b, a*b, a-b |a+b, a*b, a-b | | +-------------------------+-------------------------+-------------------------+ | |a/b |Integer division giving a| | | |float | +-------------------------+-------------------------+-------------------------+ |a and b, a or b, not a |a && b, a || b, not a | | +-------------------------+-------------------------+-------------------------+ |a == b, a != b |a==b, a /= b | | +-------------------------+-------------------------+-------------------------+ |min(a,b), max(a,b) |min a b, max a b | | +-------------------------+-------------------------+-------------------------+ |min([a,b,c]), |minimum [a,b,c], maximum | | |max([a,b,c]), |[a,b,c], sum [a,b,c] | | |sum([a,b,c]) | | | +-------------------------+-------------------------+-------------------------+ | | | | +-------------------------+-------------------------+-------------------------+ | | | | +-------------------------+-------------------------+-------------------------+ Expressions ----------- +-------------------------+-------------------------+-------------------------+ |Python |Haskell |Notes | +-------------------------+-------------------------+-------------------------+ |b if a else c |if a then b else c |Both are expressions, not| | | |statements | +-------------------------+-------------------------+-------------------------+ | | | | +-------------------------+-------------------------+-------------------------+ | | | | +-------------------------+-------------------------+-------------------------+ | | | | +-------------------------+-------------------------+-------------------------+ Lists ----- +-------------------------+-------------------------+-------------------------+ |Python |Haskell |Notes | +-------------------------+-------------------------+-------------------------+ |[1,2,3,4] |[1,2,3,4] | | +-------------------------+-------------------------+-------------------------+ |[1, 'a', "foo"] |n/a |Haskell lists must be | | | |homogeneous | +-------------------------+-------------------------+-------------------------+ |[1,2] + [3,4] |[1,2] ++ [3,4] | | +-------------------------+-------------------------+-------------------------+ |[1] + [2,3] |1:[2,3] |Haskell has more syntax | | | |for list expressions | +-------------------------+-------------------------+-------------------------+ | | | | +-------------------------+-------------------------+-------------------------+ |['a','b','c'][1] |['a', 'b', 'c'] !! 1 |Extract a list element | +-------------------------+-------------------------+-------------------------+ |['a','b','c'][:3] |take 3 ['a','b','c'] |First N elements | +-------------------------+-------------------------+-------------------------+ |['a','b','c'][3:] |drop 3 ['a','b','c'] |Drop N, take rest | +-------------------------+-------------------------+-------------------------+ |['a','b','c'][1:2] |take 2 . drop 1 |Slices are uglier in | | |['a','b','c'] |Haskell | +-------------------------+-------------------------+-------------------------+ |['a','b','c'][0] |head ['a','b','c'] |First elt | +-------------------------+-------------------------+-------------------------+ |['a','b','c'][1:] |tail ['a','b','c'] |All but first elt | | | | | +-------------------------+-------------------------+-------------------------+ |['a','b','c'][-1] |last ['a','b','c'] | | +-------------------------+-------------------------+-------------------------+ |['a','b','c'][:-1] |init ['a','b','c'] |All but last elt | +-------------------------+-------------------------+-------------------------+ |len(['a','b','c']) |length ['a','b','c'] | | +-------------------------+-------------------------+-------------------------+ |not len(['a','b','c']) |null ['a','b','c'] |is list empty (but would | | | |probably just do | | | |bool(list) in Python) | +-------------------------+-------------------------+-------------------------+ |reverse(['a','b','c']) |reverse ['a','b','c'] | | +-------------------------+-------------------------+-------------------------+ |'a' in ['a','b','c'] |'a' `elem` ['a','b','c'] | | +-------------------------+-------------------------+-------------------------+ |range(1,21) |[1..20] |Python counts to the last| | | |value before the 2nd | | | |parm, Haskell includes it| +-------------------------+-------------------------+-------------------------+ |set(x) - set(y) |x \\ y |Set difference for lists | | | |x and y - except Haskell | | | |isn't really, it just | | | |removes one value 'z' | | | |from x for each | | | |occurrence of 'z' in y. | | | |See Data.Set for real | | | |sets. | +-------------------------+-------------------------+-------------------------+ |set(x) + set(y) |x `union` y |Haskell adds one | | | |occurrence of each | | | |element of y to x if x | | | |doesn't already have one | +-------------------------+-------------------------+-------------------------+ | | | | +-------------------------+-------------------------+-------------------------+ | | | | +-------------------------+-------------------------+-------------------------+ Functions --------- +-------------------------+-------------------------+-------------------------+ |Python |Haskell |Notes | +-------------------------+-------------------------+-------------------------+ |def doubleMe(x): |doubleMe x = x + x | | | return x + x | | | +-------------------------+-------------------------+-------------------------+ |def doubleUs(x, y): |doubleUs x y = x*2 + y+2 | | | return x*2 + y*2 | | | +-------------------------+-------------------------+-------------------------+ | | | | +-------------------------+-------------------------+-------------------------+ | | | | +-------------------------+-------------------------+-------------------------+