====== Introduction to Haskell ====== Different ways of defining the ''sum'' function in Haskell: sum1 :: [Int] -> Int -> Int -> Int sum1 v crt_sum crt_index = if crt_index == (length v) then crt_sum else sum1 v (crt_sum + (v !! crt_index)) (crt_index + 1) sum2 v crt_sum crt_index | crt_index == (length v) = crt_sum | otherwise = sum2 v (crt_sum + (v !! crt_index)) (crt_index + 1) sum3 v crt_sum | v == [] = crt_sum | otherwise = sum3 (tail v) (crt_sum + (head v)) sum4 [] crt_sum = crt_sum sum4 (x:xs) crt_sum = sum4 xs (crt_sum + x) sum5 :: [Int] -> Int sum5 v = local v 0 where local [] acc = acc local (x:xs) acc = local xs (acc + x) Different ways of writing ''atLeastTwo'': atLeastTwo1 :: [Int] -> Bool atLeastTwo1 l | l == [] = False | otherwise = atLeastOne (tail l) where atLeastOne ls | ls == [] = False | otherwise = True atLeastTwo2 [] = False atLeastTwo2 (x:xs) = atLeastOne xs where atLeastOne [] = False atLeastOne _ = True atLeastTwo3 [] = False atLeastTwo3 [x] = False atLeastTwo3 (x:y:xs) = True atLeastTwo4 (_:_:_) = True atLeastTwo4 _ = False