-
Notifications
You must be signed in to change notification settings - Fork 457
/
Copy pathSet10b.hs
93 lines (79 loc) · 2.97 KB
/
Set10b.hs
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
-- This exercise set hides most of Prelude. You only have access to
-- the Bool, Int and list types, and pattern matching.
--
-- In particular, seq is not available, so you must use pattern
-- matching to force evaluation!
{-# LANGUAGE NoImplicitPrelude #-}
module Set10b where
import Mooc.VeryLimitedPrelude
import Mooc.Todo
------------------------------------------------------------------------------
-- Ex 1: Define the operator ||| that works like ||, but forces its
-- _right_ argument instead of the left one.
--
-- Examples:
-- False ||| False ==> False
-- True ||| False ==> True
-- undefined ||| True ==> True
-- False ||| undefined ==> an error!
(|||) :: Bool -> Bool -> Bool
x ||| y = todo
------------------------------------------------------------------------------
-- Ex 2: Define the function boolLength, that returns the length of a
-- list of booleans and forces all of the elements
--
-- Examples:
-- boolLength [False,True,False] ==> 3
-- boolLength [False,undefined] ==> an error!
--
-- Note that with the ordinary length function,
-- length [False,undefined] ==> 2
boolLength :: [Bool] -> Int
boolLength xs = todo
------------------------------------------------------------------------------
-- Ex 3: Define the function validate which, given a predicate and a
-- value, evaluates to the value. However, validate should also force the
-- result of `predicate value`, even though it is not used.
--
-- Examples:
-- validate even 3 ==> 3
-- validate odd 3 ==> 3
-- validate undefined 3 ==> an error!
-- validate (\x -> undefined) 3 ==> an error!
validate :: (a -> Bool) -> a -> a
validate predicate value = todo
------------------------------------------------------------------------------
-- Ex 4: Even though we can't implement the generic seq function
-- ourselves, we can implement it manually for specific datatypes.
--
-- The type class MySeq contains the method myseq which is supposed to
-- work like the built-in seq function. Implement the given MySeq
-- instances.
--
-- Just like in the course material, we use the special value
-- `undefined` here to illustrate what myseq evaluates. The tests for
-- this exercise also use undefined.
--
-- Examples:
-- myseq True 0 ==> 0
-- myseq ((\x -> x) True) 0 ==> 0
-- myseq (undefined :: Bool) 0
-- ==> *** Exception: Prelude.undefined
-- myseq (3::Int) True ==> True
-- myseq (undefined::Int) True
-- ==> *** Exception: Prelude.undefined
-- myseq [1,2] 'z' ==> 'z'
-- myseq [undefined] 'z' ==> 'z' -- [undefined] is in WHNF
-- myseq (1:undefined) 'z' ==> 'z' -- 1:undefined is in WHNF
-- myseq (undefined:[2,3]) 'z' ==> 'z' -- undefined:[2,3] is in WHNF
-- myseq [1..] 'z' ==> 'z'
-- myseq (undefined::[Int])
-- ==> *** Exception: Prelude.undefined
class MySeq a where
myseq :: a -> b -> b
instance MySeq Bool where
myseq = todo
instance MySeq Int where
myseq = todo
instance MySeq [a] where
myseq = todo