Skip to content

Instantly share code, notes, and snippets.

@donfour
Created February 10, 2020 14:20
Show Gist options
  • Save donfour/7ecf91e768e4d5affa1173874f4a91ba to your computer and use it in GitHub Desktop.
Save donfour/7ecf91e768e4d5affa1173874f4a91ba to your computer and use it in GitHub Desktop.
Haskell Cheatsheet

Start ghci ghci

List manipulation:

head [1,2,3] 1

tail [1,2,3,4,5] [2,3,4,5]

[1,2,3,4,5] !! 2 3 *Note: !! is O(n). Not recommended!

take 3 [1,2,3,4,5] [1,2,3]

drop 3 [1,2,3,4,5] [4,5]

length [1,2,3,4,5] 5 *Note: length is O(n).

sum [1,2,3,4,5] 15

product [1,2,3,45] 120

[1,2,3] ++ [4,5] [1,2,3,4,5]

reverse [1,2,3,4,5] [5,4,3,2,1]

Sample functions:

double x = x + x

quadruple x = double (double x)

factorial n = product [1..n]

average ns = sum ns div length ns Note: x f y is equivalent to f x y

To load a file: ghci test.hs

To reload :reload

To find type of an expression :type expr

To quit ghci :quit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment