Tuesday, October 21, 2014

Haskell Evaluation

Conditional evaluation

-- file: ch02/myDrop.hs
myDrop n xs = if n <= 0 || null xs
              then xs
              else myDrop (n - 1) (tail xs)  
The if keyword introduces an expression that has three components.
  • An expression of type Bool, immediately following the if. We refer to this as a predicate.
  • A then keyword, followed by another expression. This expression will be used as the value of the if expression if the predicate evaluates to True.
  • An else keyword, followed by another expression. This expression will be used as the value of the if expression if the predicate evaluates to False.
  • The null function indicates whether a list is empty, while the (||) operator performs a logical “or” of its Bool-typed arguments. 
ghci> :type null
null :: [a] -> Bool
ghci> :type (||)
(||) :: Bool -> Bool -> Bool