-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPLS_HW5_sol.sml
74 lines (62 loc) · 2.41 KB
/
PLS_HW5_sol.sml
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
(*--------------------------------------------------------------------------------------------------*)
(*------------------------------------------- Question 2 -------------------------------------------*)
(*--------------------------------------------------------------------------------------------------*)
datatype 'a seq = Nil | Cons of 'a * (unit-> 'a seq);
exception EmptySeq;
fun head(Cons(x,_)) = x | head Nil = raise EmptySeq;
fun tail(Cons(_,xf)) = xf() | tail Nil = raise EmptySeq;
datatype direction = Back | Forward;
datatype 'a bseq = bNil
| bCons of 'a * (direction -> 'a bseq);
fun bHead(bCons(x,_)) = x | bHead bNil = raise EmptySeq;
fun bForward(bCons(_,xf)) = xf(Forward) | bForward bNil = raise EmptySeq;
fun bBack(bCons(_,xf)) = xf(Back) | bBack bNil = raise EmptySeq;
(*---------------- Question 2 Section 1 ----------------*)
fun intbseq x =
bCons(x,fn Forward => intbseq (x+1) | Back => intbseq (x-1));
(*---------------- Question 2 Section 2 ----------------*)
fun bmap f (bCons (x,xf)) =
bCons(f x,
fn Forward => bmap f (xf Forward)
| Back => bmap f (xf Back))
| bmap _ _ = bNil;
(*---------------- Question 2 Section 3 ----------------*)
fun bfilter pred d (bCons (x,xf)) =
if (pred x = true)
then bCons(x,
fn Forward => bfilter pred Forward (xf Forward)
| Back => bfilter pred Back (xf Back))
else if (d = Forward)
then bfilter pred d (xf Forward)
else bfilter pred d (xf Back)
| bfilter _ _ _ = bNil;
(*---------------- Question 2 Section 4 ----------------*)
(* there is a better solution *)
local
exception Zero_Target;
fun get_n_obj s 1 = head s
| get_n_obj _ 0 = raise Zero_Target
| get_n_obj s n = get_n_obj (tail s) (n-1);
fun helper a b (~1) Forward = bCons (head b,(helper a b 1))
| helper a b 1 Back = bCons (head a,(helper a b (~1)))
| helper _ _ 0 _ = raise Zero_Target
| helper a b n Forward =
if n>0
then bCons (get_n_obj b (n+1),helper a b (n+1))
else bCons (get_n_obj a (abs (n+1)),helper a b (n+1))
| helper a b n Back =
if n>0
then bCons (get_n_obj b (n-1),helper a b (n-1))
else bCons (get_n_obj a (abs (n-1)),helper a b (n-1));
in
fun seq2bseq a b = helper a b 2 Back;
end;
(*---------------- Question 2 Section 5 ----------------*)
local
fun jump_n 0 s _ = s
| jump_n n (bCons (x,xf)) d = jump_n (n-1) (xf d) d
| jump_n _ _ _ = raise Match;
in
fun bSeqJump s n =
bCons(bHead s,fn d => bSeqJump (jump_n n s d) n);
end;