Skip to content

Commit 7358bf7

Browse files
committed
Initial
0 parents  commit 7358bf7

19 files changed

+1540
-0
lines changed

100_doors.fold

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--
2+
-- 100 doors
3+
-- http://rosettacode.org/wiki/100_doors
4+
--
5+
6+
doors = replicate 100 False
7+
8+
for a = 1:100, b in a:a:100
9+
doors[b] = !doors[b]
10+
end
11+
for a = 1:100 println("Door $a is " * (doors[a] ? "open" : "close") ) end

Anonymous_functions.fold

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
--
2+
-- # Anonymous Functions
3+
--
4+
5+
6+
-> \ x y -> x + y
7+
-> \ 5
8+
-> (~ + ~)
9+
10+
(-> system :fetcher :conf :credentials :username)
11+
12+
=> system #fetcher #conf #credentials #username
13+
14+
-> map (data flag => process data verbose: (not flag)) [1, 2, 3, 4, 5]
15+
-> map (process ~ verbose: (not ~)) [1, 2, 3, 4, 5]
16+
17+
-> map (\data flag => process data verbose: (not flag)) [1, 2, 3, 4, 5]
18+
-> map \(process ~ verbose: (not ~)) [1, 2, 3, 4, 5]
19+
20+
-> map (λ data flag → process data verbose: (not flag)) [1, 2, 3, 4, 5]
21+
-> map λ(process ~ verbose: (not ~)) [1, 2, 3, 4, 5]
22+
23+
-> map (data flag => process data verbose: (not flag)) [1, 2, 3, 4, 5]
24+
-> map {process ~ verbose: (not ~)} [1, 2, 3, 4, 5]
25+
26+
-> map (\x -> x * x) [1 .. 5]
27+
= [1, 4, 9, 16, 25]
28+
29+
-> map \(~1 * ~1) [1 .. 5]
30+
= [1, 4, 9, 16, 25]
31+
32+
33+
-- Consider using ~0 as a reference to the anonymous function, allowing this way recursion.
34+
35+
-- Mathematica
36+
-- If[#1 == 1, 1, #1 * #0[#1-1]]&
37+
38+
-> \(~1 == 1 ? 1 : ~1 * (~0 (~1 - 1)))
39+
-> \(if (~1 == 1): 1 else: ~1 * (~0 (~1 - 1)))
40+
41+
-> \(\1 == 1 ? 1 : @1 * (@0 (@1 - 1)))
42+
43+
44+
fix = \f -> (\x a -> f (x x) a) (\x a -> f (x x) a)
45+
46+
f -> (x a -> f (x x) a) (x a -> f (x x) a)
47+
48+

Binary_search.fold

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
--
2+
-- Binary search
3+
-- http://rosettacode.org/wiki/Binary_search#Metaphor
4+
--
5+
6+
7+
function {Ord a} binary_search haystack::[a] needle::a -> Int?
8+
function recurse low high haystack needle
9+
mid = (low + high) / 2
10+
if | high < low -> None
11+
| haystack.mid > needle -> recurse low (mid - 1) haystack needle
12+
| haystack.mid < needle -> recurse (mid + 1) high haystack needle
13+
| otherwise -> Some mid
14+
end
15+
recurse 0 (#haystack - 1) haystack needle
16+
end
17+
18+
19+
fn binary_search_rec<T: Ord>(haystack: &[T], needle: T) -> Option<uint> {
20+
fn recurse<T: Ord>(low: uint, high: uint, haystack: &[T], needle: T) -> Option<uint> {
21+
match (low + high) / 2 {
22+
_ if high < low => None,
23+
mid if haystack[mid] > needle => recurse(low, mid - 1, haystack, needle),
24+
mid if haystack[mid] < needle => recurse(mid + 1, high, haystack, needle),
25+
mid => Some(mid)
26+
}
27+
}
28+
recurse::<T>(0, haystack.len() - 1, haystack, needle)
29+
}
30+
31+
32+
binarySearch :: Integral a => (a -> Ordering) -> (a, a) -> Maybe a
33+
binarySearch p (low,high)
34+
| high < low = Nothing
35+
| otherwise =
36+
let mid = (low + high) `div` 2 in
37+
case p mid of
38+
LT -> binarySearch p (low, mid-1)
39+
GT -> binarySearch p (mid+1, high)
40+
EQ -> Just mid
41+
42+
43+
binary_search :: {Comparable a} -> [a] -> a -> Int?
44+
binary_search haystack needle =
45+
search 0 (#haystack - 1) haystack needle where
46+
search low high haystack needle =
47+
let mid = (low + high) / 2 in
48+
if | high < low -> None
49+
| haystack[mid] > needle -> search low (mid - 1) haystack needle
50+
| haystack[mid] < needle -> search (mid + 1) high haystack needle
51+
| otherwise -> Some mid
52+
53+
54+
binary_search :: {Comparable a} -> [a] -> a -> Int?
55+
binary_search haystack needle =
56+
[search 0 (#haystack - 1) haystack needle] where
57+
[search low high haystack needle] =
58+
let mid = (low + high) / 2 in
59+
if | high < low -> None
60+
| haystack[mid] > needle -> [search low (mid - 1) haystack needle]
61+
| haystack[mid] < needle -> [search (mid + 1) high haystack needle]
62+
| otherwise -> Some mid
63+
64+
65+
binary_search{Comparable A}(haystack: [A], needle: A) -> Int? =
66+
search(0, |haystack| - 1, haystack, needle) where
67+
search(low, high, haystack, needle) =
68+
let mid = (low + high) / 2 in
69+
if | high < low -> None
70+
| haystack[mid] > needle -> search(low, mid - 1, haystack, needle)
71+
| haystack[mid] < needle -> search(mid + 1, high, haystack, needle)
72+
| otherwise -> Some mid

Blocks.fold

+264
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
2+
3+
--
4+
-- do-end notation
5+
--
6+
7+
function assoc[T] v::PersistentVector[T] i::Int el -> PersistentVector:
8+
boundscheck! v i
9+
if (i > |v| - |v.tail|):
10+
newtail = v.tail # (1..end)
11+
newtail # (mask i) := el
12+
PersistentVector v.trie newtail v.length
13+
else:
14+
newnode = v.trie[i][1..end]
15+
newnode # (mask i) := el
16+
PersistentVector (assoc v.trie i newnode) v.tail v.length
17+
end
18+
end
19+
20+
function max list:
21+
reduce (a b -> a > b ? a : b) list
22+
end
23+
24+
25+
type Tree[a] =
26+
| Empty
27+
| Node (a, Tree[a], Tree[a])
28+
29+
30+
function map :: (a -> b) -> Tree[a] -> Tree[b]
31+
function map f =
32+
| Empty => Empty
33+
| Node (x, l, r) => Node (f x, map f l, map f r)
34+
end
35+
36+
37+
--
38+
-- { } notation
39+
--
40+
41+
function assoc[T] v::PersistentVector[T] i::Int el = {
42+
boundscheck! v i
43+
if (i > |v| - |v.tail|) {
44+
newtail = v.tail # (1..end)
45+
newtail # (mask i) := el
46+
PersistentVector v.trie newtail v.length
47+
}
48+
else {
49+
newnode = v.trie[i][1:end]
50+
newnode # (mask i) := el
51+
PersistentVector (assoc v.trie i newnode) v.tail v.length
52+
}
53+
}
54+
55+
function max list = {
56+
reduce (a b -> a > b ? a : b) list
57+
}
58+
59+
60+
type Tree[a] = {
61+
| Empty
62+
| node (a, Tree[a], Tree[a])
63+
}
64+
65+
function map :: (a -> b) -> Tree[a] -> Tree[b]
66+
function map f = {
67+
| Empty => Empty
68+
| Node (x, l, r) => Node (f x, map f l, map f r)
69+
}
70+
71+
72+
73+
--
74+
-- indentation notation (= for block definitions)
75+
-- (Maybe*)
76+
--
77+
78+
function assoc[T] v::PersistentVector[T] i::Int el =
79+
boundscheck! v i
80+
if (i > |v| - |v.tail|):
81+
newtail = v.tail # (1..end)
82+
newtail # (mask i) := el
83+
PersistentVector v.trie newtail v.length
84+
else:
85+
newnode = v.trie[i][1:end]
86+
newnode # (mask i) := el
87+
PersistentVector (assoc v.trie i newnode) v.tail v.length
88+
89+
function max list =
90+
reduce (a b -> a > b ? a : b) list
91+
92+
type Tree[a] = Empty
93+
| node (a, Tree[a], Tree[a])
94+
95+
function map :: (a -> b) -> Tree[a] -> Tree[b]
96+
function map f =
97+
| Empty => Empty
98+
| Node (x, l, r) => Node (f x, map f l, map f r)
99+
100+
101+
102+
--
103+
-- haskell-like notation
104+
-- (Maybe)
105+
--
106+
107+
assoc [T](v::PersistentVector[T] i::Int el) =
108+
boundscheck! v i
109+
if (i > |v| - |v.tail|):
110+
newtail = v.tail # (1..end)
111+
newtail # (mask i) := el
112+
PersistentVector v.trie newtail v.length
113+
else:
114+
newnode = v.trie[i][1:end]
115+
newnode # (mask i) := el
116+
PersistentVector (assoc v.trie i newnode) v.tail v.length
117+
118+
max list =
119+
reduce (a b -> a > b ? a : b) list
120+
121+
type Tree[a] = Empty
122+
| node (a, Tree[a], Tree[a])
123+
124+
map :: (a -> b) -> Tree[a] -> Tree[b]
125+
map f = | Empty => Empty
126+
| Node (x, l, r) => Node (f x, map f l, map f r)
127+
128+
129+
--
130+
-- indentation notation (expression definitions)
131+
-- (No.)
132+
--
133+
134+
assoc = [T](v::PersistentVector[T] i::Int el) ->
135+
boundscheck! v i
136+
if (i > |v| - |v.tail|):
137+
newtail = v.tail # (1..end)
138+
newtail # (mask i) := el
139+
PersistentVector v.trie newtail v.length
140+
else:
141+
newnode = v.trie[i][1:end]
142+
newnode # (mask i) := el
143+
PersistentVector (assoc v.trie i newnode) v.tail v.length
144+
145+
max = list ->
146+
reduce (a b -> a > b ? a : b) list
147+
148+
Tree = type[a]: Empty
149+
| node (a, Tree[a], Tree[a])
150+
151+
map :: (a -> b) -> Tree[a] -> Tree[b]
152+
map = f -> | Empty => Empty
153+
| Node (x, l, r) => Node (f x, map f l, map f r)
154+
155+
156+
--
157+
-- do notation
158+
--
159+
160+
function assoc[T] v::PersistentVector[T] i::Int el -> PersistentVector do
161+
boundscheck! v i
162+
if (i > |v| - |v.tail|) do
163+
newtail = v.tail # (1..end)
164+
newtail # (mask i) := el
165+
PersistentVector v.trie newtail v.length
166+
else
167+
newnode = v.trie[i][1:end]
168+
newnode # (mask i) := el
169+
PersistentVector (assoc v.trie i newnode) v.tail v.length
170+
end
171+
end
172+
173+
function max list do
174+
reduce (a b -> a > b ? a : b) list
175+
end
176+
177+
type Tree[a] =
178+
| Empty
179+
| Node (a, Tree[a], Tree[a])
180+
181+
function map :: (a -> b) -> Tree[a] -> Tree[b]
182+
function map f do
183+
| Empty => Empty
184+
| Node (x, l, r) => Node (f x, map f l, map f r)
185+
end
186+
187+
188+
189+
--
190+
-- pyret notation
191+
--
192+
193+
function assoc (v::PersistentVector a) (i::Int) (el::a) -> PersistentVector:
194+
boundscheck! v i
195+
if (i > |v| - |v.tail|):
196+
newtail = v.tail # (1..end)
197+
newtail # (mask i) := el
198+
PersistentVector v.trie newtail v.length
199+
else:
200+
newnode = v.trie[i][1:end]
201+
newnode # (mask i) := el
202+
PersistentVector (assoc v.trie i newnode) v.tail v.length
203+
end
204+
end
205+
206+
height = Leaf => 0
207+
| Node (l, _, r) => 1 + max (height l) (height r)
208+
209+
type Tree a = Leaf
210+
| Node (Tree a, a, Tree a)
211+
212+
map :: (a -> b) -> Tree a -> Tree b
213+
map f = Leaf => Leaf
214+
| Node (l, x, r) => Node (map f l, f x, map f r)
215+
216+
protocol Show a =
217+
show :: T -> String
218+
end
219+
220+
instance Show (Tree a) =
221+
show = Leaf => "()"
222+
| Node (x, l, r) => "[{x}]: ({show l} | {show r})"
223+
end
224+
225+
226+
-- // --
227+
228+
229+
(function factorial [n]
230+
(log/info "Will calculate factorial of {n}")
231+
(if (= n 0)
232+
1
233+
(* n (factorial (- n 1)))))
234+
235+
function factorial n do
236+
Log.info "Will calculate factorial of {n}"
237+
(n == 0) ? 1 : n * factorial (n - 1)
238+
end
239+
240+
let factorial n =
241+
Log.info "Will calculate factorial of {n}"
242+
(n == 0) ? 1 : n * factorial (n - 1)
243+
end
244+
245+
function factorial n:
246+
Log.info "Will calculate factorial of {n}"
247+
(n == 0) ? 1 : n * factorial (n - 1)
248+
end
249+
250+
factorial n = do
251+
Log.info "Will calculate factorial of {n}"
252+
(n == 0) ? 1 : n * factorial (n - 1)
253+
end
254+
255+
function factorial n {
256+
Log.info "Will calculate factorial of {n}"
257+
(n == 0) ? 1 : n * factorial (n - 1)
258+
}
259+
260+
factorial n = {
261+
Log.info "Will calculate factorial of {n}"
262+
(n == 0) ? 1 : n * factorial (n - 1)
263+
}
264+

0 commit comments

Comments
 (0)