Defining
a = 10
b = "foo"
Letting
let x = 5
y = 10
in x + y
Where-ing
x + y
where x = 5
y = 10
Defining
add x y = x + y
Calling
total = add 5 10 -- 15
Partial Application
inc = add 1
total = inc 5 -- 6
Anonymous Functions
(\x y -> x + y)
Infixing
total = 5 `add` 10 -- 15
Currying
incChar = chr . add 1 . ord
incChar 'a' -- 'b'
Calling
total = 2 + 2 -- 4
Prefixing
total = (+) 2 2 -- 4
Sectioning (partially apply second/rhs argument)
divideBy2 = (/ 2)
total = divideBy2 10 -- 5
Defining
words = ["foo", "bar", "faz", "boz"]
Ranges
oneToTen = [1 .. 10]
aToZ = ['a' .. 'z']
Indexing
third = words !! 2 -- "faz"
Concatenating
otherWords = words ++ ["fop", "fob"]
Prepending
oneMoreWord = "baz" : otherWords
Tips
first = head [1, 2, 3] -- 1
last = tail [1, 2, 3] -- 3
Slices
firstTwo = take 2 [1, 2, 3] -- [1, 2]
lastTwo = drop 1 [1, 2, 3] -- [2, 3]
Splitting
splitAt 5 [1..10] -- ([1,2,3,4,5],[6,7,8,9,10])
splitAt 10 ['a'..'z'] -- ("abcdefghij","klmnopqrstuvwxyz")
Pattern Matching
(x : _) = [1, 2, 3] -- 1
(_ : xs) = [1, 2, 3] -- [2, 3]
[a, b, c] = [1, 2, 3]
List Comprehensions
[ toUpper x | x <- ['a'..'c'] ] -- "ABC"
[ x * x | x <- [1..10], (mod x 2) == 0 ] -- [4,16,36,64,100]
type String = [Char]
Defining
-- these are the same
foo = "bar" -- String
foo = ['b','a','r'] -- [Char]
Printing
-- print almost anything
print "Hello World"
print (53, "hello")
-- print String specifically
putStr "Hello World" -- no newline
putStrLn "!!!" -- newline added
List Equivalent Operations
-- these work the same as lists
-- because String is [Char]
third = "abcde" !! 2 -- 'c'
merged = "foo" ++ "bar" -- "foobar"
prefixed = '$' : "10.00" -- "$10.00"
first = head "abc" -- 'a'
last = tail "abc" -- 'c'
firstTwo = take 2 "abc" -- "ab"
lastTwo = drop 1 "abc" -- "bc"
Defining
point = (4, 23)
Destructuring
(x, y) = point
Accessing
x = fst point
y = snd point
Defining
data Person = Person String String Int
Constructing
joe = Person "Joe" "Shmoe" 42
Destructuring
Person first last age = joe
Deriving
data Person = Person String String Int deriving (Show, Eq)
Defining
data Make = Ford | Toyota | Tesla
data Airline = American | Southwest | Delta
data Vehicle = Car Make | Plane Airline
Constructing
car = Car Tesla
plane = Plane Southwest
Guards
isItTwo 2 = True
isItTwo _ = False
isItTwo 2 -- True
isItTwo 10 -- False
Pattern Matching Arguments
isPlane (Plane _) = True
isPlane _ = False
isPlane car -- False
isPlane plane -- True
Pattern Matching Cases
isDelta (Plane airline) =
case airline of
Delta -> True
_ -> False
isDelta plane -- False
isDelta (Plane Delta) -- True
Recursive Functions
fib 0 = 0 -- base case
fib 1 = 1 -- base case
fib x = fib (x - 1) + fib (x - 2)
Higher-order Functions
map _ [] = [] -- base case
map f (x:xs) = f x : map f xs
filter _ [] = []
filter f (x:xs)
| f x = x : (filter f xs)
| otherwise = filter f xs
Point-free Functions
-- these are the same
upperFirstFive xs = map toUpper (take 5 xs)
upperFirstFive = map toUpper . take 5 -- no declared args
$ :: (a -> b) -> a -> b
f $ a = f a
-- these are the same
foo (bar 10)
foo $ bar 10
& :: a -> (a -> b) -> b
a & f = f a
map toUpper "abcdefg" & take 3 --- "ABC"