Haskell/Solutions/Control structures

From Wikibooks, open books for an open world
Jump to navigation Jump to search

← Back to Control structures

case expressions[edit | edit source]

Exercises
Use a case statement to implement a fakeIf function which might be used as a replacement to the familiar if expressions.


fakeIf :: Bool -> a -> a -> a
fakeIf condition ifTrue ifFalse =
  case condition of
    True  -> ifTrue
    False -> ifFalse

Controlling actions, revisited[edit | edit source]

Exercises
  1. Redo the "Haskell greeting" exercise in Simple input and output/Controlling actions, this time using a case statement.
  2. What does the following program print out? And why?
main =
 do x <- getX
    putStrLn x

getX =
 do return "My Shangri-La"
    return "beneath"
    return "the summer moon"
    return "I will"
    return "return"
    return "again"


1.

main = do
  putStrLn "Hello, what is your name?"
  name <- getLine
  case name of
      "Simon" -> greatlanguage
      "John"  -> greatlanguage
      "Phil"  -> greatlanguage
      "Koen"  -> putStrLn "I think debugging Haskell is fun."
      _       -> putStrLn "Sorry, I don't know you."
      where
      greatlanguage = putStrLn "I think Haskell is a great programming language."


2. Executing main will print "again". Remember that the value of a sequence of IO actions is the same as the value of the last action in the sequence. getX can also be written as:

getX =
  do return "again"

or even shorter, as:

getX = return "again"

As a result, x in the main function has the value "again", which will then be written to the screen.

Operators[edit | edit source]

Exercises
  • Lambdas are a nice way to avoid defining unnecessary separate functions. Convert the following let- or where-bindings to lambdas:
    • map f xs where f x = x * 2 + 3
    • let f x y = read x + y in foldr f 1 xs
  • Sections are just syntactic sugar for lambda operations. I.e. (+2) is equivalent to \x -> x + 2. What would the following sections 'desugar' to? What would be their types?
    • (4+)
    • (1 `elem`)
    • (`notElem` "abc")

1.

  • Substitute f to get map (\ x -> x * 2 + 3) xs
  • Substitute f to get foldr (\ x y -> read x + y) 1 xs

2.

  • (4+)
    • Becomes (\ x -> 4 + x)
    • Has type Num a => a -> a
  • (1 `elem`)
    • Becomes(\ x -> 1 `elem` x)
    • Alternately written as (\ x -> elem 1 x)
    • Has type Num a :: a -> Bool
      • Note: The full type is (Foldable t, Eq a, Num a) => t a -> Bool but this has not been covered yet.
  • (`notElem` "abc")
    • Becomes (\ x -> x `notElem` "abc")
    • Alternately written as (\ x -> notElem x "abc")
    • Has type Char -> Bool