CIS 554 -- Haskell Quiz | Name
__________________________________ |

- (5 points) In each of the following, write a single-line
**expression**that computes the desired result. Do no error checking.

- Remove all the space characters (
`' '`

) from the string named`line`

.

filter (/= ' ') line

- Replace all the space characters (
`' '`

) in the string named`line`

with asterisks (`'*'`

).

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

- Return
`True`

if the string named`line`

contains an asterisk (`'*'`

).

any (== '*') line

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

- 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

- 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)]

- Remove all the space characters (
- (1 point) If
`f`

,`g`

, and`h`

are functions, and`x`

and`y`

are numbers, what is the difference betweenf g h x y 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))

- (2 points) Give the type of each of the following expressions (as returned by
`:t`

in`ghci`

).

`map`

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

`flip map`

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

- (1 point) Write an anonymous function to return the
`n`

^{th}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)

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

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.