CIS 554 -- Haskell Quiz Name __________________________________
  1. (5 points) In each of the following, write a single-line expression that computes the desired result. Do no error checking.

    1. Remove all the space characters (' ') from the string named line.

      filter (/= ' ') line

    2. Replace all the space characters (' ') in the string named line with asterisks ('*').

      map (\x -> if x == ' ' then '*' else x) line

    3. Return True if the string named line contains an asterisk ('*').

      any (== '*') line
      foldl1 (||) $ map (== '*') line

    4. Return the smallest character (the one with the least ASCII value) in the string line. You may assume that line is not an empty string. (Hint: foldl1 and foldr1 do not require a starting value.)

      foldl1 min line
      foldl1 (\ x y -> if x < y then x else y) line

    5. Given two strings line1 and line2 of equal length, return a string containing the smaller of each pair of corresponding characters. For example, given "haskell" and "clojure", return "caojele". (Hint: My solution uses zip, min, and a list comprehension.)

      [min x y | (x, y) <- (zip line1 line2)]

  2. (1 point) If f, g, and h are functions, and x and y are numbers, what is the difference between  f g h x y  and  f $ g $ h x y  ?

    f g h x y  means the same as   ((f g) h) x y
    f $ g $ h x y  means the same as  f (g (h x y))

  3. (2 points) Give the type of each of the following expressions (as returned by :t in ghci).

    1. map

      (a -> b) -> [a] -> [b]

    2. flip map

      [a] -> (a -> b) -> [b]

  4. (1 point) Write an anonymous function to return the nth element of a nonempty list (counting the head of the list as element 1). Assume that the list contains at least n elements, that is, do no error checking.

    \ n lst -> head (drop (n - 1) lst)

  5. (1 point) Briefly describe how the IO monad allows Haskell to remain a "pure" functional language.

    Best answer: The monad encapsulates "state" as a parameter which is implicitly returned from each function in turn and implicitly passed as a parameter to the next function in a sequence.

    Good answer: The IO monad provides an "imperative" group of functions which can call pure functions, but cannot be called by them.