Skip to content

Commit 7743d0e

Browse files
committed
Basic types, and a lot of function def mods.
1 parent da230aa commit 7743d0e

27 files changed

+1924
-211
lines changed

Anonymous_functions.fold

+36-21
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,63 @@
33
--
44

55

6-
-> \ x y -> x + y
7-
-> \ 5
8-
-> (~ + ~)
6+
7+
map (x => x * x) [1..10]
8+
9+
map { 2 => x * 4
10+
x => x * x } [1..10]
11+
12+
[1..10] . map { \1 * \2 }
13+
14+
15+
tree . { Leaf => "x"
16+
Node x => str x }
17+
18+
=> \ x y => x + y
19+
=> \ 5
20+
=> (~ + ~)
921

1022
(-> system :fetcher :conf :credentials :username)
1123

1224
-> system #fetcher #conf #credentials #username
1325

14-
-> map (data flag -> process data verbose: (not flag)) [1, 2, 3, 4, 5]
15-
-> map (process ~ verbose: (not ~)) [1, 2, 3, 4, 5]
26+
-> map (data flag => process data verbose: (not flag)) [1, 2, 3, 4, 5]
27+
=> map (process _ verbose: (not _)) [1, 2, 3, 4, 5]
28+
29+
=> map { data flag => process data verbose: (not flag) } [1, 2, 3, 4, 5]
30+
=> map { process _ verbose: (not _) } [1, 2, 3, 4, 5]
1631

17-
-> map (\data flag -> process data verbose: (not flag)) [1, 2, 3, 4, 5]
18-
-> map \(process ~ verbose: (not ~)) [1, 2, 3, 4, 5]
32+
=> map (data flag => process data verbose: (not flag)) [1, 2, 3, 4, 5]
33+
=> map {process _ verbose: (not _)} [1, 2, 3, 4, 5]
1934

20-
-> map (λ data flag process data verbose: (not flag)) [1, 2, 3, 4, 5]
21-
-> map λ(process ~ verbose: (not ~)) [1, 2, 3, 4, 5]
35+
=> map (λ data flag process data verbose: (not flag)) [1, 2, 3, 4, 5]
36+
=> map λ(process _ verbose: (not _)) [1, 2, 3, 4, 5]
2237

23-
-> map (data flag -> process data verbose: (not flag)) [1, 2, 3, 4, 5]
24-
-> map {process ~ verbose: (not ~)} [1, 2, 3, 4, 5]
38+
=> map (data flag => process data verbose: (not flag)) [1, 2, 3, 4, 5]
39+
=> map {process _ verbose: (not _)} [1, 2, 3, 4, 5]
2540

26-
-> map (|data flag| -> process data verbose: (not flag)) [1, 2, 3, 4, 5]
27-
-> map {process ~ verbose: (not ~)} [1, 2, 3, 4, 5]
41+
=> map (|data flag| => process data verbose: (not flag)) [1, 2, 3, 4, 5]
42+
=> map {process _ verbose: (not _)} [1, 2, 3, 4, 5]
2843

29-
-> map (|x| -> x * x) [1 .. 5]
44+
=> map (|x| => x * x) [1 .. 5]
3045
= [1, 4, 9, 16, 25]
3146

32-
-> map \(~1 * ~1) [1 .. 5]
47+
=> map \(_1 * _1) [1 .. 5]
3348
= [1, 4, 9, 16, 25]
3449

35-
-- Consider using ~0 as a reference to the anonymous function, allowing this way recursion.
50+
-- Consider using _0 as a reference to the anonymous function, allowing this way recursion.
3651

3752
-- Mathematica
3853
-- If[#1 == 1, 1, #1 * #0[#1-1]]&
3954

40-
-> \(~1 == 1 ? 1 : ~1 * (~0 (~1 - 1)))
41-
-> \(if (~1 == 1): 1 else: ~1 * (~0 (~1 - 1)))
55+
=> \(_1 == 1 ? 1 : _1 * (_0 (_1 - 1)))
56+
=> \(if (_1 == 1): 1 else: _1 * (_0 (_1 - 1)))
4257

43-
-> \(\1 == 1 ? 1 : @1 * (@0 (@1 - 1)))
58+
=> \(\1 == 1 ? 1 : @1 * (@0 (@1 - 1)))
4459

4560

46-
fix = \f -> (\x a -> f (x x) a) (\x a -> f (x x) a)
61+
fix = \f => (\x a => f (x x) a) (\x a => f (x x) a)
4762

48-
f -> (x a -> f (x x) a) (x a -> f (x x) a)
63+
f => (x a => f (x x) a) (x a => f (x x) a)
4964

5065

Binary_search.fold

+44-20
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,58 @@
1-
--
1+
--
22
-- Binary search
33
-- http://rosettacode.org/wiki/Binary_search#Metaphor
44
--
55

66

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
7+
binary_search <Ord T> :: [T] -> T -> Int?
8+
binary_search haystack needle = do
9+
search l h = do
10+
m = (low + high) / 2
11+
if | (h < l) => None
12+
| (haystack # m > needle) => search l (m - 1)
13+
| (haystack # m < needle) => search (m + 1) h
14+
| otherwise => Some m
15+
end
16+
search 0 (#haystack - 1)
1617
end
1718

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-
}
19+
binary_search <Ord T> :: [T] -> T -> Int?
20+
binary_search haystack needle = {
21+
search l h = {
22+
m = (low + high) / 2
23+
if | h < l => None
24+
| haystack # m > needle => search l (m - 1)
25+
| haystack # m < needle => search (m + 1) h
26+
| otherwise => Some m
2727
}
28-
recurse::<T>(0, haystack.len() - 1, haystack, needle)
28+
search 0 (#haystack - 1)
2929
}
3030

3131

32+
binarySearch :: Integral a => (a -> Ordering) -> (a, a) -> Maybe a
33+
binarySearch p (l, h)
34+
| h < l = 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+
44+
function {Ord a} binary_search haystack::[a] needle::a -> Int?
45+
function recurse low high haystack needle
46+
mid = (low + high) / 2
47+
if | high < low -> None
48+
| haystack.mid > needle -> recurse low (mid - 1) haystack needle
49+
| haystack.mid < needle -> recurse (mid + 1) high haystack needle
50+
| otherwise -> Some mid
51+
end
52+
recurse 0 (#haystack - 1) haystack needle
53+
end
54+
55+
3256
binarySearch :: Integral a => (a -> Ordering) -> (a, a) -> Maybe a
3357
binarySearch p (low,high)
3458
| high < low = Nothing

0 commit comments

Comments
 (0)