-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab3.xml
62 lines (46 loc) · 1.28 KB
/
lab3.xml
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
fun thenAddOne (f,ys:int)=f ys + 1;
fun pow2 ys=ys*ys;
thenAddOne (pow2,2);
fun maplist(f,[])=[]
| maplist(f,y::ys)=f y :: maplist(f,ys);
fun maplist(f,xs)=map f xs;
maplist(pow2,[2,3,4,5]);
fun maplist' f =
let fun f1(xs)=map f xs;
in f1 end;
maplist' pow2 [2,3,4,5];
fun findOdd [] = NONE
| findOdd (y::ys) =
if ((y mod 2) = 1) then SOME(y) else findOdd ys;
fun subsetSumOption (x,s)=
let
fun subset_sum ([] : int list, s : int) : bool * int list =
if s = 0 then (true, [])
else (false, [])
| subset_sum (_ : int list, 0 : int) : bool * int list = (true, [])
| subset_sum (x::xs : int list, s : int) : bool * int list =
let val ((w,wList),(wo,woList)) =
(subset_sum(xs, s-x), subset_sum(xs, s)) in
case (w,wo) of
(true,_) => (true, x::wList)
| (_,tru e) => (true, woList)
| (_,_) => (false, [])
end;
fun re_2(x,y)=
if(x) then SOME(y) else NONE
in
re_2(subset_sum(x,s))
end;
fun exists f = foldl f false xy;
fun panduan x =
if(x=2) then true else false;
exists panduan [1,3,4];
fun forall f xy = foldl f true;
datatype 'a tree = Empty
| Node of 'a tree * 'a * 'a tree;
fun treeFilter(f)=
let fun tF(Empty)=Empty
| tF(Node(A,x,B))=
if(f(x)) then Node(tF(A),SOME(x),tF(B))
else Node(tF(A),NONE,tF(B))
in tF end;