haskell-course

Compiler Warnings

It’s a good thing to have compiler warnings enabled.

Add {-# OPTIONS_GHC -Wall #-} to the top of your file. Or add -Wall in the package.yaml file of your stack config.

Lists

dotProduct xs ys = go 0 xs ys
  where
    go n [] ys = n
    go n xs [] = n
    go n (x : xs) (y : ys) = go (n + (x * y)) xs ys
dotProduct xs ys = sum $ zipWith (*) xs ys
quicksort (x : xs) = lesser ++ [x] ++ greater
  where
    lesser = [y | y <- xs, y <= x]
    greater = [y | y <- xs, y >= x]

Laziness

myMult 0 a = 0
myMult b a = (a * b)

primes = filterPrime [2..]
  where filterPrime (p:xs) =
          p : filterPrime [x | x <- xs, x `mod` p /= 0]

foldl, foldr, foldl'