comparison interps/egel/prelude-hackeso.eg @ 12312:ab575fb7f10e draft

<b_jonas> `` cp -vi /hackenv/tmp/egel-master/include/prelude-hackeso.eg /hackenv/interps/egel/
author HackEso <hackeso@esolangs.org>
date Thu, 20 Feb 2020 23:23:38 +0000
parents
children
comparison
equal deleted inserted replaced
12311:a4df463e9de0 12312:ab575fb7f10e
1 import "/hackenv/interps/egel/io.ego"
2 import "/hackenv/interps/egel/fs.ego"
3 import "/hackenv/interps/egel/random.ego"
4 import "/hackenv/interps/egel/regex.ego"
5
6 namespace System (
7
8 def or =
9 [ false false -> false
10 | X Y -> true ]
11
12 def and =
13 [ true true -> true
14 | X Y -> false ]
15
16 def not =
17 [ true -> false
18 | X -> true ]
19
20 def . =
21 [ F G X -> F (G X) ]
22
23 def flip = [ F X Y -> F Y X ]
24
25 def uncurry = [ F (X,Y) -> F X Y ]
26
27 def fst = [ (X,Y) -> X ]
28
29 def snd = [ (X,Y) -> Y ]
30
31 def @@ =
32 [ SELF NAME -> (get NAME SELF) SELF ]
33
34 )
35
36 namespace Opt (
37 data just
38 data nothing
39 )
40
41 namespace List (
42
43 using System
44
45 # renamed from length to avoid collision with String:length
46 def len =
47 [ nil -> 0
48 | (cons X XX) -> 1 + (len XX) ]
49
50 def foldl =
51 [ F Z nil -> Z
52 | F Z (cons X XX) -> foldl F (F Z X) XX ]
53
54 def foldr =
55 [ F Z nil -> Z
56 | F Z (cons X XX) -> F X (foldr F Z XX) ]
57
58 def head =
59 [ (cons X XX) -> X ]
60
61 def tail =
62 [ (cons X XX) -> XX ]
63
64 def ++ =
65 [ nil YY -> YY
66 | (cons X XX) YY -> cons X (XX ++ YY) ]
67
68 def map =
69 [ F nil -> nil
70 | F (cons X XX) -> let Y = F X in cons Y (map F XX) ]
71
72 def reverse =
73 foldl (flip cons) nil
74
75 def block =
76 [ 0 -> nil
77 | N -> cons (N - 1) (block (N - 1)) ]
78
79 def nth =
80 [ 0 (cons X XX) -> X
81 | N (cons X XX) -> nth (N - 1) XX ]
82
83 def insert =
84 [ 0 X (cons Y YY) -> cons X YY
85 | I X (cons Y YY) -> cons Y (insert (I - 1) X YY) ]
86
87 def take =
88 [ 0 XX -> nil
89 | N (cons X XX) -> cons X (take (N - 1) XX) ]
90
91 def drop =
92 [ 0 XX -> XX
93 | N (cons X XX) -> drop (N - 1) XX ]
94
95 def fromto =
96 [ X Y ->
97 if X <= Y then cons X (fromto (X+1) Y)
98 else nil ]
99
100 def filter =
101 [ P nil -> nil
102 | P (cons X XX) -> if P X then cons X (filter P XX)
103 else filter P XX ]
104
105 def flatten =
106 [ nil -> nil
107 | (cons nil YY) -> flatten YY
108 | (cons (cons X XX) YY) -> cons X (flatten (cons XX YY)) ]
109
110 def zip =
111 [ (cons X XX) (cons Y YY) -> cons (X,Y) (zip XX YY)
112 | XX YY -> nil ]
113
114 def zipwith =
115 [ Z (cons X XX) (cons Y YY) -> cons (Z X Y) (zipwith Z XX YY)
116 | Z XX YY -> nil ]
117
118 def any =
119 [ P nil -> false
120 | P (cons B BB) -> if P B then true else any P BB ]
121
122 def all =
123 [ P nil -> true
124 | P (cons B BB) -> if P B then all P BB else false ]
125
126 def elem =
127 [ X -> any ((==) X) ]
128
129 def notelem =
130 [ X -> all ((/=) X) ]
131
132 def union =
133 [ nil YY -> YY
134 | (cons X XX) YY -> if elem X YY then union XX YY else cons X (union XX YY) ]
135
136 def insertEverywhere =
137 [ X nil -> {{X}}
138 | X (cons Y YY) -> cons (cons X (cons Y YY)) (map (cons Y) (insertEverywhere X YY)) ]
139
140 def concatMap =
141 [ F -> foldr ((++) . F) nil ]
142
143 def permutations =
144 foldr (concatMap . insertEverywhere) {{}}
145 )