Professional Documents
Culture Documents
Basic Haskell Cheat Sheet: Declaring Types and Classes Common Functions
Basic Haskell Cheat Sheet: Declaring Types and Classes Common Functions
Structure
func :: type -> type
func x = expr
main = do code
code
...
typeclass
Function Application
f
f
f
f
f
f
x
x
$
$
$
g
y
y
g
g
g
$
$ h x
x y
h x
typeclass instance
(f x) y
((f x) y) z
f (g x)
f (g (h x))
f (g x y)
f g (h x)
string
tuple
ordering relation
function ()
expr
True || False
a
1
31337
31337^10
1.2
1.2
[]
[1,2,3]
[a,b,c]
"abc"
[[1,2],[3,4]]
"asdf"
(1,2)
([1,2],a)
LT, EQ, GT
\x -> e
::
::
::
::
::
::
::
::
::
::
::
::
::
::
::
::
::
::
type
Bool
Char
Int
Integer
Integer
Float
Double
[a]
[Integer]
[Char]
[Char]
[[Integer]]
String
(Int,Int)
([Int],Char)
Ordering
a -> a
expr
137
1.2
1.2
a
731
::
::
::
::
::
::
Common functions
type
type
type
type
type
data
MyType = Type
UserId = Integer
UserName = String
User = (UserId,UserName)
UserList = [User]
MyData = MyData Type Type Type
deriving (Class,Class )
data MyData = Simple Type |
Duple Type Type |
Nople
class MyClass a where
foo :: a -> a -> b
goo :: a -> a
...
instance MyClass MyType where
foo x y = ...
goo x = ...
...
Misc
id
const
undefined
error
not
flip
::
::
::
::
::
::
a -> a
id x x -- identity
a -> b -> a
(const x ) y x
a
undefined (lifts error)
String -> a
error cs (lifts error cs)
Bool -> Bool
not True False
(a -> b -> c) -> (b -> a -> c)
flip f $ x y f y x
Tuples
fst
snd
curry
:: (a, b) -> a
fst (x,y ) x
:: (a, b) -> b
snd (x,y ) y
:: ((a, b) -> c) -> a -> b -> c
curry (\(x,y ) -> e ) \x y -> e
Lists
null
head
tail
init
reverse
take
drop
length
elem
repeat
cycle
::
::
::
::
::
::
::
::
::
::
::
Special folds
and
or
sum
product
concat
maximum
minimum
::
::
::
::
::
::
::
a
a
a
a
and [p,q,r ]
or [p,q,r ]
sum [i,j,k ]
product [i,j,k ]
concat [xs,ys,zs ]
maximum [10,0,5]
minimum [10,0,5]
p && q &&r
p || q ||r
i + j + k
i * j * k
xs ++ys ++zs
10
0
Defining fixity
non associative fixity
left associative fixity
right associative fixity
default, implied when no fixity given
a + b
(a +) b
(+ b) a
a f b
(+) a b
((+) a) b
\x -> ((+) x b)) a
Copyright 2014, Rudy Matela Compiled on October 26, 2014 Upstream: https://github.com/rudymatela/ultimate-cheat-sheets
infix 0-9 op
infixl 0-9 +--+
infixr 0-9 -!infixl 9
Higher-order / Functors
map
foldr
This text is avaliable under the Creative Commons Attribution-ShareAlike 3.0 Licence, or, the GNU Free Documentation License version 1.3 or Later.
Numeric
Expressions / Clauses
abs
:: Num a => a -> a
abs -10 10
even, odd :: Num a => a -> Bool
even -10 True
gcd, lcm :: Integral a => a -> a -> a
gcd 4 2 2
recip
:: Fractional a => a -> a
recip x 1/x
pi
:: Floating a => a
pi 3.1415...
sqrt, log :: Floating a => a -> a
sqrt x x**0.5
exp, sin, cos, tan, asin, acos, atan :: Floating a => a -> a
truncate, round :: (RealFrac a, Integral b) => a -> b
ceiling, floor :: (RealFrac a, Integral b) => a -> b
putChar c
putStr cs
putStrLn cs
print x
getChar
getLine
getContents
interact foo
writeFile fn cs
appendFile fn cs
readFile fn
Strings
lines
Pattern Matching
Simple Pattern Matching
Number 3
Empty string
3
""
Character a
Ignore value
[]
(x:xs)
( :xs)
[a,b,c]
(x:3:xs)
Libraries / Modules
importing
importing (qualified)
importing (subset)
declaring
./File/On/Disk.hs
import PathTo.Lib
import PathTo.Lib as PL
import PathTo.Lib (foo,goo )
module Module.Name
( foo
, goo
)
where
...
import File.On.Disk
(a,b)
(a,_)
(a,b,c)
Just a
Nothing
MyData a b c
MyData a _ c
((a,b):xs)
s@(a,b)
a@(x:xs)
p@(MyData a b c)
List Comprehensions
[x |
[f x
[x |
[f x
[x+y
x
|
x
y
|
<- xs]
x <- xs, p x]
<- xs, p x, q x]
| x <- xs, y <- ys]
x <- [a,b], y <- [i,j]]
Copyright 2014, Rudy Matela Compiled on October 26, 2014 Upstream: https://github.com/rudymatela/ultimate-cheat-sheets
guarded equations
foo ... | boolExpr = exprA
| otherwise = exprB
nested if expression
if boolExpr1
then exprA
else if boolExpr2
then exprB
else exprC
guarded equations
foo ... | boolExpr1 = exprA
| boolExpr2 = exprB
| otherwise = exprC
case expression
case x of
pat1 -> exprA
pat2 -> exprB
-> exprC
let expression
let nameA =exprA
nameB =exprB
...
in mainExpression
where clause
foo ... = mainExpression
where nameA =exprA
nameB =exprB
...
do notation
do statement
pat <- exp
statement
pat <- exp
...
desugarized do notation
statement >>
exp >>= \pat ->
statement >>
exp >>= \pat ->
...
statement separator
statement grouping
;
{ }
-- or line break
-- or layout/indentation
As-pattern
Debug.Trace
if expression
if boolExpr
then exprA
else exprB
xs
map f $ filter p xs
filter q $ filter p xs
zipWith f xs ys
[a+i, a+j, b+i, b+j]
compiling program.hs
running
running directly
interactive mode (GHCi)
GHCi load
GHCi reload
GHCi activate stats
GHCi help
Type of an expression
Info (oper./func./class)
install package pkg
update package list
list/search for packages matching pat
information about package pkg
$
$
$
$
>
>
>
>
>
>
$
$
$
$
ghc program.hs
./program
run haskell program.hs
ghci
:l program.hs
:r
:set +s
:?
:t expr
:i thing
cabal install pkg
cabal update
cabal list pat
cabal info pkg
This text is avaliable under the Creative Commons Attribution-ShareAlike 3.0 Licence, or, the GNU Free Documentation License version 1.3 or Later.