-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter_04.hs
More file actions
30 lines (21 loc) · 971 Bytes
/
Copy pathChapter_04.hs
File metadata and controls
30 lines (21 loc) · 971 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module Chapter_04 where
-- Exercise 4.3 (**)
-- Consider a function safeTail :: [a] -> [a] that behaves in the same way as tail except that it maps the empty list to itself rather than producing an error. Using tail and the function null :: [a] -> Bool that decides if a list is empty or not, define safetail using:
-- (a) a conditional expression;
-- (b) guarded equations;
-- (c) pattern matching.
safeTailA :: [a] -> [a]
-- safeTailA xs = if ??? then ??? else ???
safeTailA xs = if (length xs) == 0 then xs else (tail xs)
safeTailB :: [a] -> [a]
safeTailB xs | (length xs) == 0 = xs
| otherwise = tail xs
safeTailC :: [a] -> [a]
safeTailC (_:xs) = xs
safeTailC [] = []
-- Exercise 4.7 (**)
-- Show how the meaning of the following curried function definition can be formalised in terms of lambda expressions:
-- mult :: Int -> Int -> Int -> Int
-- mult x y z = x*y*z
mult :: Int -> (Int -> (Int -> Int))
mult = \x -> (\y -> (\z -> x * y * z))