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.
map
, filter
map
and filter
take
, drop
, length
, (!!)
, zip
, zipWith
map (+1) [1,2,3] = [2,3,4]
triangles = [ (a,b,c) | c <- [1..10], b <- [1..10], a <- [1..10] ]
rightTriangles = [ (a,b,c) | c <- [1..10], b <- [1..c], a <- [1..b], a^2 + b^2 == c^2]
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]
myMult 0 a = 0
myMult b a = (a * b)
(1/0)
is undefined
myMult 0 (1/0)
is defined.[1..]
does not terminate but take 3 [1..]
works perfectly
primes = filterPrime [2..]
where filterPrime (p:xs) =
p : filterPrime [x | x <- xs, x `mod` p /= 0]
repeat
, iterate
cycle
foldl
, foldr
, foldl'
foldr (\x y -> "( " ++ show x ++ " * " ++ y ++ ")") "init" [1..5]
foldl (\x y -> "( " ++ x ++ " * " ++ show y ++ ")") "init" [1..5]
foldr1
, foldl1
foldr
foldr op init [] = init
foldr op init (x:xs) = x `op` (foldr op init xs)
foldl
foldl op init [] = init
foldl op init (x:xs) = foldl op (init `op` x) xs
foldr
lends itself to laziness. foldr (&&) False (repeat False)