This repository was archived by the owner on Nov 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.m
68 lines (50 loc) · 1.58 KB
/
utils.m
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
module
infix "oo";
--export afst, asnd, apair, aboth, pairwith, swap, anth, dropto, number,
--K, I, C, gmap,unionmap, remove, lhead, ltail, part;
afst f (x,y)=(f x,y)
and asnd f (x,y)=(x,f y)
and aboth f (x,y)=(f x,f y)
and apair (f,g) (x,y)=(f x,g y)
and pairwith f x=(x,f x)
and swap (x,y) = (y,x)
and (f oo g) x y = f (g x y)
and rec anth _ _ [] = []
|| anth 1 f (x.xs) = f x.xs
|| anth n f (x.xs) = x.anth (n-1) f xs
and rec dropto p=while (\l.l~=[] & (not o p o hd) l) tl
and number _ [] = []
|| number i (x.xs) = (i,x).number (i+1) xs
and const x y=x
and id x=x
and C f x y=f y x
and loop f = let rec yf = f yf in yf
-- gmap g f = reduce g [] o map f
and rec gmap g f = reduce (\x.\ys.g (f x) ys) []
and unionmap f = gmap union f
and remove a (b.bs) & (a = b) = bs
|| remove a (b.bs) = b.remove a bs
|| remove a [] = []
and replace p [] = [p]
|| replace (t,v) ((t',v').ls) & (t = t') = ((t,v).ls)
|| replace p (l.ls) = l.replace p ls
-- lhead xs ys = head (length xs) ys, but the rhs is stricter
and lhead (x.xs) (y.ys) = y.lhead xs ys
|| lhead _ _ = []
-- ltail xs ys = tail (length xs) ys, but the rhs is stricter
and ltail [] ys = ys
|| ltail _ [] = []
|| ltail (x.xs) (y.ys) = ltail xs ys
-- lsplit xs ys = (lhead xs ys,ltail xs ys), but without the space leak
and lsplit [] ys = ([],ys)
|| lsplit _ [] = ([],[])
|| lsplit (x.xs) (y.ys) =
let (yhs,yts) = lsplit xs ys
in (y.yhs,yts)
-- JSP 920928
and part p [] = ([], [])
|| part p (x.xs) =
let (ys, zs) = part p xs in
if p x then (x.ys, zs) else (ys, x.zs)
and issubset a b = all (C mem b) a
end