function :: type -> typefunction x = exprfunction2 :: type -> [type] -> typefunction2 x xs = exprmain = do action ...
Function Application
f x y ≡ (f x) y ≡ ((f) (x)) (y)f x y z ≡ ((f x) y) z ≡ (f x y) zf $ g x ≡ f (g x) ≡ f . g $ xf $ g $ h x ≡ f (g (h x)) ≡ f . g . h $ xf $ g x y ≡ f (g x y) ≡ f . g x $ yf g $ h x ≡ f g (h x) ≡ f g . h $ x
Values and Types
Item
Example
Type
boolean
True | False
Bool
character
'a'
Char
fixed-precision integer
1
Int
integer (arbitrary sz.)
31337
Integer
31337^10
Integer
single precision float
1.2
Float
double precision float
1.2
Double
list
[]
[a]
[1,2,3]
[Integer]
['a','b','c']
[Char]
"abc"
[Char]
[[1,2],[3,4]]
[[Integer]]
string
"asdf"
String
tuple
(1,2)
(Int,Int)
([1,2],'a')
([Int],Char)
ordering relation
LT, EQ, GT
Ordering
function (λ)
\x -> e
a -> b
maybe (just something
Just 10
Maybe Int
or nothing)
Nothing
Maybe a
Values and Typeclasses
Item
Example
Type
Numeric (+,-,*)
137
Num a => a
Fractional (/)
1.2
Fractional a => a
Floating
1.2
Floating a => a
Equatable (==)
'a'
Eq a => a
Ordered (⇐,>=,>,<)
731
Ord a => a
Declaring Types and Classes
Type Synonym
type MyType = Typetype PairList a b = [(a,b)]type String = [Char] -- from Prelude
Data (single constructor)
data MyData = MyData Type Type deriving (Class, Class)
Data (multi constructor)
data MyData = Simple Type | Duple Type Type | Nople
newtype MyType = MyType Type deriving (Class, Class)
Typeclass
class MyClass a where foo :: a -> a -> b goo :: a -> a
Typeclass instance
instance MyClass MyType where foo x y = ... goo x = ...
Operators (grouped by precedence)
Operator
Description
!!, .
List index, function composition
^, ^^, **
raise to: Non-neg. Int, Int, Float
*, /
multiplication, fractional division
div, mod
integral division (⇒ −∞), modulus
quot, rem
integral quotient (⇒ 0), remainder
+, -
addition, subtraction
:, ++
list construction, append lists
\\
list difference
>, >=, <, <=, ==, /=
comparisons
elem, notElem
list membership
&&
boolean and
|
boolean or
>>=, >>
sequencing: bind and then
$, $!, seq
application, strict apl., sequencing
NOTE: Highest precedence (first line) is 9, lowest precedence is 0. Operator listings aligned left, right, and center indicate left-, right-, and non-associativity.
Defining fixity:
infix 0-9 'op' -- non associativeinfixl 0-9 +-+ -- left associativeinfixr 0-9 -!- -- right associativeinfixl 9 -- default (when none given)
Functions ≡ Infix operators
f a b ≡ a 'f' ba + b ≡ (+) a b(a +) b ≡ ((+) a) b(+ b) a ≡ (\x -> x + b) a
Pattern Matching (fn. declaration, lambda, case, let, where)
Pattern
Description
Example
fixed
number
3
character
'a'
ignore value
_
empty string
""
list
empty
[]
head x and tail xs
(x:xs)
tail xs (ignore head)
(_:xs)
list with 3 elements
[a,b,c]
list where 2nd element is 3
(x:3:xs)
tuple
pair values a and b
(a,b)
ignore second element
(a,_)
triple values a, b and c
(a,b,c)
mixed
first tuple on list
((a,b):xs)
maybe
just constructor
Just a
nothing constructor
Nothing
custom
user-defined type
MyData a b c
ignore second field
MyData a _ c
user-defined record type
MyR {f1=x, f2=y}
as-pattern
tuple s and its values
s@(a,b)
list a, its head and tail
a@(x:xs)
Prelude functions
Misc
id :: a -> a id x ≡ x -- identityconst :: a -> b -> a (const x) y ≡ xundefined :: a undefined ≡ ⊥ (lifts error)error :: [Char] -> a error cs ≡ ⊥ (lifts error cs)not :: Bool -> Bool not True ≡ Falseflip :: (a -> b -> c) -> b -> a -> c flip f $ x y ≡ f y x
Lists
null :: [a] -> Bool null [] ≡ True -- ∅?length :: [a] -> Int length [x,y,z] ≡ 3elem :: a -> [a] -> Bool y 'elem' [x,y] ≡ True -- ∈?head :: [a] -> a head [x,y,z,w] ≡ xlast :: [a] -> a last [x,y,z,w] ≡ wtail :: [a] -> [a] tail [x,y,z,w] ≡ [y,z,w]init :: [a] -> [a] init [x,y,z,w] ≡ [x,y,z]reverse :: [a] -> [a] reverse [x,y,z] ≡ [z,y,x]take :: Int -> [a] -> [a] take 2 [x,y,z] ≡ [x,y]drop :: Int -> [a] -> [a] drop 2 [x,y,z] ≡ [z]takeWhile, dropWhile :: (a -> Bool) -> [a] -> [a] takeWhile (/= z) [x,y,z,w] ≡ [x,y]zip :: [a] -> [b] -> [(a, b)] zip [x,y,z] [a,b] ≡ [(x,a),(y,b)]
Infinite Lists
repeat :: a -> [a] repeat x ≡ [x,x,x,x,x,x,...]cycle :: [a] -> [a] cycle xs ≡ xs++xs++xs++... cycle [x,y] ≡ [x,y,x,y,x,y,...]iterate :: (a -> a) -> a -> [a] iterate f x ≡ [x,f x,f (f x),...]
Higher-order / Functors
map :: (a->b) -> [a] -> [b] map f [x,y,z] ≡ [f x, f y, f z]zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] zipWith f [x,y,z] [a,b] ≡ [f x a, f y b]filter :: (a -> Bool) -> [a] -> [a] filter (/=y) [x,y,z] ≡ [x,z]foldr :: (a -> b -> b) -> b -> [a] -> b foldr f z [x,y] ≡ x 'f' (y 'f' z)foldl :: (a -> b -> a) -> a -> [b] -> a foldl f x [y,z] ≡ (x 'f' y) 'f' z
Special folds
and :: [Bool] -> Bool and [p,q,r] ≡ p && q && ror :: [Bool] -> Bool or [p,q,r] ≡ p || q || rsum :: Num a => [a] -> a sum [i,j,k] ≡ i+j+kproduct :: Num a => [a] -> a product [i,j,k] ≡ i*j*kmaximum :: Ord a => [a] -> a maximum [9,0,5] ≡ 9minimum :: Ord a => [a] -> a minimum [9,0,5] ≡ 0concat :: [[a]] ->[a] concat [xs,ys,zs] ≡ xs++ys++zs
Tuples
fst :: (a, b) -> a fst (x,y) ≡ xsnd :: (a, b) -> b snd (x,y) ≡ ycurry :: ((a, b) -> c) -> a -> b -> c curry (\(x,y) -> e) ≡ \x y -> euncurry :: (a -> b -> c) -> (a, b) -> c uncurry (\x y -> e) ≡ \(x,y) -> e
Numeric
abs :: Num a => a -> a abs (-9) ≡ 9even, odd :: Integral a => a -> Bool even 10 ≡ Truegcd, lcm :: Integral a => a -> a -> a gcd 6 8 ≡ 2recip :: Fractional a => a -> a recip x ≡ 1/xpi :: Floating a => a pi ≡ 3.14...sqrt, log :: Floating a => a -> a sqrt x ≡ x**0.5exp, sin, cos, tan, asin, acos :: Floating a => a -> atruncate, round :: (RealFrac a, Integral b) => a -> bceiling, floor :: (RealFrac a, Integral b) => a -> b
show :: Show a => a -> String show 137 ≡ "137"read :: Show a => String -> a read "2" ≡ 2
Ord Class
min :: Ord a => a -> a -> a min 'a' 'b' ≡ 'a'max :: Ord a => a -> a -> a max "b" "ab" ≡ "b"compare :: Ord a => a->a->Ordering compare 1 2 ≡ LT
IO – Must be “inside” the IO Monad
putChar c -- Write char c to stdoutputStr cs -- Write string cs to stdoutputStrLn cs -- Write string cs to stdout w/ a newlineprint x -- Print x, a show instance, to stdoutgetChar -- Read char from stdingetLine -- Read line from stdin as a stringgetContents -- Read all input from stdin as a stringinteract foo -- Bind stdin/out to foo (:: String -> String)writeFile fn cs -- Write string cs to a file named fnappendFile fn cs -- Append string cs to a file named fnreadFile fn -- Read contents from a file named fn
List Comprehensions
Take pat from list. If boolPredicate, add element expr to list:
[expr | pat <- list, boolPredicate, ...][x | x <- xs] ≡ xs[f x | x <- xs, p x] ≡ map f $ filter p xs[x | x <- xs, p x, q x] ≡ filter q $ filter p xs[x+y | x <- [a,b], y <- [i,j]] ≡ [a+i, a+j, b+i, b+j][x | boolE] ≡ if boolE then [x] else []
trace string $ expr -- Print string, return exprtraceShow expr $ expr -- Call show before printingtraceShow (x,y) False -- Trace function f x y values
GHC - Glasgow Haskell Compiler (and Cabal)
$ ghc program.hs -- compiling program.hs
$ ./program -- running
$ run_haskell program.hs -- running directly
$ ghci -- interactive mode (GHCi)
> :l program.hs -- GHCi load
> :r -- GHCi reload
> :set +s -- GHCi activate stats
> :? -- GHCi help
> :t expr -- Type of an expression
> :i thing -- Info (oper./func./class)
$ ghc-pkg list [pkg_name] -- Installed GHC packages
{-# LANGUAGE Pragma #-} -- Activating some pragma
$ ghc -XSomePragma ... -- Same, via GHC call
$ cabal install pkg -- install package pkg
$ cabal update -- update package list
$ cabal list pat -- list packages matching pat
$ cabal info pkg -- information about package
$ cabal help [command] -- help on commands
$ cabal run/test/bench [name] -- run executable/test/bench
$ cabal sandbox init -- initialize sandbox
$ cabal sandbox add-source dir -- add custom sandbox source
Copyright 2014-2021, Rudy Matela – Compiled on November 8, 2021
Upstream: https://github.com/rudymatela/concise-cheat-sheets Basic Haskell Cheat Sheet v1.2
This text is available under the Creative Commons Attribution-ShareAlike 3.0 Licence,
or (at your option), the GNU Free Documentation License version 1.3 or Later.