Commit dbea251e authored by Jochen Schulz's avatar Jochen Schulz
Browse files

removed dependency of map (which comes later)

parent 8ff99eb7
%% Cell type:markdown id: tags:
# Exercises: pattern matching
## problem 1
Write a function `whatToDoToday` that accepts three `Bool`s `rain`, `cold` and `wind` and returns a string
- `"kite flying"` on `wind` and no `rain`
- `"don't forget your umbrella"` on `rain` and no `cold`
- `"stay at home at the fireplace"` on `rain` and `cold`
- `"take a sunbath"` without `rain` and `wind`
Use `case ... of ...` and pattern matching.
%% Cell type:code id: tags:
``` haskell
whatToDoToday :: (Bool, Bool, Bool) -> String
whatToDoToday (regen,kalt,wind) =
case (regen,kalt,wind) of
(True,True,_) -> "amKaminSitzen"
(True,False,_) -> "regenschirmMitnehmen"
(False,_,True) -> "drachenSteigenLassen"
(False,_,False) -> "sonnenBaden"
whatToDoToday (True,False,True)
```
%% Cell type:markdown id: tags:
## problem 2
Same as above, but use partial function definition instead of `case`.
%% Cell type:code id: tags:
``` haskell
whatToDoToday :: (Bool,Bool,Bool) -> String
whatToDoToday (True,True,_) = "amKaminSitzen"
whatToDoToday (True,False,_) = "regenschirmMitnehmen"
whatToDoToday (False,_,True) = "drachenSteigenLassen"
whatToDoToday (False,_,False) = "sonnenBaden"
whatToDoToday (True, True, False)
```
%% Cell type:markdown id: tags:
## problem 3
For tuples with 2 elements, `fst` and `snd` extract the first and second component, respectively. Write functions `fst3`, `snd3` and `thr3` that work on tuples with 3 elements.
%% Cell type:code id: tags:
``` haskell
first :: (a, b, c) -> a
first (x, _, _) = x
second :: (a, b, c) -> b
second (_, x, _) = x
third :: (a, b, c) -> c
third (_, _, x) = x
first (1,2,3)
second (1,2,3)
third (1,2,3)
first (1,'a',"foo")
second (1,'a',"foo")
third (1,'a',"foo")
```
%% Cell type:markdown id: tags:
## problem 4
Write a function `reverseMe :: String -> String` that reverses its argument. Proceed as follows:
- empty strings are left unchanged
- non-empty strings are split into first letter and rest
- `reverseMe` is then recursively applied to the rest, and the first letter is appended (note the type of `(++)`)
%% Cell type:code id: tags:
``` haskell
reverseMe :: String -> String
reverseMe [] = []
reverseMe (x:xs) = reverseMe xs ++ [x]
reverseMe "forward"
```
%% Cell type:markdown id: tags:
## problem 5
- Write a function `initial` that returns the first letter of a string.
- Use Hoogle or Hayoo to find a function that splits a string into words (*Hint*: What should be the type of that function? Type signatures can be directly used for searching.)
- Write a function `initials` that accepts a name in the form "Firstname Middlename Lastname" and returns the initials in the form "F. M. L."
%% Cell type:code id: tags:
``` haskell
initial :: String -> Char
initial [] = ' '
initial (x:xs) = x
```
%% Cell type:code id: tags:
``` haskell
initials :: String -> String
initials [] = ""
initials str = unwords $ map (\w -> initial w : ".") $ words str
initials str = [initial str1, '.', initial str2, '.', initial str3, '.' ]
where (str1:str2:str3:xs) = words str
```
%% Cell type:code id: tags:
``` haskell
initial "foo"
initials "foo bar gaa"
```
%% Cell type:code id: tags:
``` haskell
initials3 :: String -> String
initials3 l = words l
where
```
%% Cell type:code id: tags:
``` haskell
```
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment