Skip to main content
  1. Notes/

Haskell

Table of Contents

-Wall

Numbers #

  • Integer – arbitrarily sized whole numbers
  • Int – whole numbers up to the size of a CPU word
  • Float – real numbers
  • Rational – rational numbers

Base type classes #

  • Integral – whole numbers
  • Num – numerical
  • Ord – ordered
  • Eq – comparable on equality
(f1 . f2) x = f1 ( f2 x ) 

let definition in expression

(,) :: a -> b -> (a,b)
(,) x y = (x,y)

-- add item to the beginning of the list: (:) 

(:) :: a -> [a] -> [a] 
(++) :: [a] -> [a] -> [a]

null :: [a] -> Bool
null (_:_) = False
null [] = True

-- map list [1,2,3] to a@(x:t), produces a = [1,2,3], x = 1, t = [2,3].

f (a@(x:y)) = x:a++y
f [1,2,3] -> [1,1,2,3,2,3] 

-- head, tail, last, init, length, reverse, minimum, maximum, sum, product, or, and, null
take n xs -- first n items
drop n xs -- list without first n items
(!!) xs n -- returns n-th item
map :: (a -> b) -> [a] -> [b]
filter :: (a -> Bool) -> [a] -> [a]

takeWhile :: (a -> Bool) -> [a] -> [a]
dropWhile :: (a -> Bool) -> [a] -> [a] 
takeWhile odd [1,3,5,6,7,8,9] -> [1,3,5]
dropWhile odd [1,3,5,6,7,8,9] -> [6,7,8,9] 
concat :: [[a]] -> [a] 
zip :: [a] -> [b] -> [(a,b)] -- the length of the produced list is equal to the length of the smaller one
concat [[1,2],[2,3],[3,4]] -> [1,2,2,3,3,4]
zip [1,2,3,4] "abc" -> [(1,'a'),(2,'b'),(3,'c')] 
zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] 
zipWith (+) [1,2,3] [1,2,3,4,5] -> [2,4,6]
zipWith take [1,2,3] ["aaa","bbb","ccc"] -> ["a","bb","ccc"] 
replicate :: Int -> a -> [a] 
unzip :: [(a,b)] -> ([a],[b])