Nithin Bekal

Posts About Notes Slides

Haskell

Example code:

add a b = a + b

main = do
  print(add 1 3)

> :type add
add :: Num a => a -> a -> a

Lists

[1, 4..10] -- [1, 4, 7]

let list = [1..10]
head list  -- 1
tail list  -- [2, 3, 4, ... 10]
filter even list  -- [2, 4, 6, 8, 10]

Tuples

names = ("a" "b")
fst names  -- "a"
snd names  -- "b"

Expressions

canIVote age = do
  let msg = if age > 17
            then "please vote"
            else "not yet"
  print(msg)

Currying

add a b = a + b
-- add :: Num => a -> a -> a

Lazy evaluation

-- Create infinite list and take 10 from it
numbers = 1 : map (+1) numbers
take 10 numbers