From 9a0d879d5052ed1b544b4664b9a534d410e09b6e Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Sun, 12 Jul 2015 22:07:52 +0200 Subject: [PATCH 01/35] test.lambda --- .gitattributes | 1 + test.lambda | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 .gitattributes create mode 100644 test.lambda diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..a147805 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*text=auto diff --git a/test.lambda b/test.lambda new file mode 100644 index 0000000..9d03523 --- /dev/null +++ b/test.lambda @@ -0,0 +1,20 @@ +let range = fix f, a, b -> + if > a b + then {} + else (f a (- b 1)).concat b in + +let fibonacci = fix f, n -> + if or (= n 1) (= n 2) + then 1 + else + (f (- n 1)) (f (- n 2)) in + +let square = n -> + ** n 2 in + +let main = fix f, a -> + f in + +main + (console.log ((range 1 5).map square)) + (console.log ((range 1 5).map fibonacci)) + (console.log "Ammazza, oh, cheffico!") From b9f19de967544f9ac1e3dd8662a94952a3d7657e Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Wed, 15 Jul 2015 10:26:19 +0200 Subject: [PATCH 02/35] More memories --- test.lambda => test0.lambda | 0 test1.lambda | 23 +++++++++++++++++++++++ 2 files changed, 23 insertions(+) rename test.lambda => test0.lambda (100%) create mode 100644 test1.lambda diff --git a/test.lambda b/test0.lambda similarity index 100% rename from test.lambda rename to test0.lambda diff --git a/test1.lambda b/test1.lambda new file mode 100644 index 0000000..e965b80 --- /dev/null +++ b/test1.lambda @@ -0,0 +1,23 @@ +let fizz = n -> + = 0 (% n 3) +in + +let buzz = n -> + = 0 (% n 5) +in + +let fizzbuzz = n -> + or (fizz n) (buzz n) +in + +let range = fix f, a, b -> + if > a b + then {} + else (f a (- b 1)).concat({b}) +in + +(range 1 100).map (n -> + if fizzbuzz n then "FizzBuzz" + else if fizz n then "Fizz" + else if buzz n then "Buzz" + else n) From 39d3658e5a54de7443853518cc5add2c4a84b635 Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Wed, 15 Jul 2015 12:52:32 +0200 Subject: [PATCH 03/35] Fixes --- test1.lambda | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test1.lambda b/test1.lambda index e965b80..4f0187a 100644 --- a/test1.lambda +++ b/test1.lambda @@ -7,13 +7,13 @@ let buzz = n -> in let fizzbuzz = n -> - or (fizz n) (buzz n) + and (fizz n) (buzz n) in let range = fix f, a, b -> if > a b then {} - else (f a (- b 1)).concat({b}) + else (f a (- b 1)).concat b in (range 1 100).map (n -> From 60e36312b87975bb9ec7ada422cc0c4141cee421 Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Sat, 8 Aug 2015 13:28:01 +0200 Subject: [PATCH 04/35] Other test scripts --- server.lambda | 6 ++++++ test2.lambda | 9 +++++++++ 2 files changed, 15 insertions(+) create mode 100644 server.lambda create mode 100644 test2.lambda diff --git a/server.lambda b/server.lambda new file mode 100644 index 0000000..27a220d --- /dev/null +++ b/server.lambda @@ -0,0 +1,6 @@ +let http = require "http" in + +let server = http.createServer request, response -> + response.end.apply {"Oh hai!"} in + +server.listen 8080 diff --git a/test2.lambda b/test2.lambda new file mode 100644 index 0000000..09258a4 --- /dev/null +++ b/test2.lambda @@ -0,0 +1,9 @@ +let main = fix f, x -> f in + +let add = y -> + this.x y in +let o1.x = 123, o1.add = add in +let o2.x = 456, o2.add = add in + +main + (console.log (o1.add 321)) + (console.log (o2.add 321)) From 4572d116ecf9cdb046a312cbb90dcf9fd00eb23b Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Sun, 9 Aug 2015 10:15:24 +0200 Subject: [PATCH 05/35] Updated to new syntax Commas cannot appear in array literals and let initializers --- main.lambda | 6 ++++++ server.lambda | 4 ++-- test0.lambda | 11 +++++------ test1.lambda | 4 ++-- test2.lambda | 2 +- 5 files changed, 16 insertions(+), 11 deletions(-) create mode 100644 main.lambda diff --git a/main.lambda b/main.lambda new file mode 100644 index 0000000..77ba722 --- /dev/null +++ b/main.lambda @@ -0,0 +1,6 @@ +let main = fix (f, x -> f) in + +main + (console.log "hello 1") + (console.log "hello 2") + (console.log "hello 3") diff --git a/server.lambda b/server.lambda index 27a220d..2ffe4a6 100644 --- a/server.lambda +++ b/server.lambda @@ -1,6 +1,6 @@ let http = require "http" in -let server = http.createServer request, response -> - response.end.apply {"Oh hai!"} in +let server = http.createServer (request, response -> + response.end.apply {"Oh hai!"}) in server.listen 8080 diff --git a/test0.lambda b/test0.lambda index 9d03523..c33622d 100644 --- a/test0.lambda +++ b/test0.lambda @@ -1,18 +1,17 @@ -let range = fix f, a, b -> +let range = fix (f, a, b -> if > a b then {} - else (f a (- b 1)).concat b in + else (f a (- b 1)).concat b) in -let fibonacci = fix f, n -> +let fibonacci = fix (f, n -> if or (= n 1) (= n 2) then 1 - else + (f (- n 1)) (f (- n 2)) in + else + (f (- n 1)) (f (- n 2))) in let square = n -> ** n 2 in -let main = fix f, a -> - f in +let main = fix (f, a -> f) in main (console.log ((range 1 5).map square)) diff --git a/test1.lambda b/test1.lambda index 4f0187a..92f6126 100644 --- a/test1.lambda +++ b/test1.lambda @@ -10,10 +10,10 @@ let fizzbuzz = n -> and (fizz n) (buzz n) in -let range = fix f, a, b -> +let range = fix (f, a, b -> if > a b then {} - else (f a (- b 1)).concat b + else (f a (- b 1)).concat b) in (range 1 100).map (n -> diff --git a/test2.lambda b/test2.lambda index 09258a4..b2d8074 100644 --- a/test2.lambda +++ b/test2.lambda @@ -1,4 +1,4 @@ -let main = fix f, x -> f in +let main = fix (f, x -> f) in let add = y -> + this.x y in let o1.x = 123, o1.add = add in From 75b068742ec5f0367ecf0f68874c253d5fb2aa69 Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Thu, 27 Aug 2015 08:29:20 +0200 Subject: [PATCH 06/35] Conway's Game of Life --- life.lambda | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 life.lambda diff --git a/life.lambda b/life.lambda new file mode 100644 index 0000000..9c62ff6 --- /dev/null +++ b/life.lambda @@ -0,0 +1,97 @@ +let in_range = (v, a, b -> + and (>= v a) (<= v b) +) in + +let min = (a, b -> + if < a b + then a + else b +) in + +let max = (a, b -> + if > a b + then a + else b +) in + +let range = fix (f, a, b -> + if > a b + then {} + else {a}.concat (f (+ a 1) b) +) in + +let sequentially = fix (f, x -> + f +) in + +let sum = (l -> + l.reduce (a, v -> + a v) 0 +) in + +let grid.slice_row = (g, i, j -> + if in_range i 0 (- g.length 1) + then g[i].slice (max (- j 1) 0) (min (+ j 2) g[i].length) + else {} +) in + +let grid.neighbors = (g, i, j -> + sum {sum (grid.slice_row g (- i 1) j), + sum (grid.slice_row g i j), + sum (grid.slice_row g (+ i 1) j), + - 0 g[i][j]} +) in + +let grid.next_state_from = (a, n -> + if or (= n 3) (and (= a 1) (= n 2)) + then 1 + else 0 +) in + +let grid.next_state = (g, i, j -> + grid.next_state_from g[i][j] (grid.neighbors g i j) +) in + +let grid.step_column = fix (f, g, i, j -> + if in_range j 0 (- g[i].length 1) + then {grid.next_state g i j}.concat (f g i (+ j 1)) + else {} +) in + +let grid.step_row = fix (f, g, i -> + if in_range i 0 (- g.length 1) + then {grid.step_column g i 0}.concat (f g (+ i 1)) + else {} +) in + +let grid.step = (g -> + grid.step_row g 0 +) in + +let grid.advance_n_steps = fix (f, g, n -> + if > n 0 + then f (grid.step g) (- n 1) + else g +) in + +let grid.print = (g -> + g.forEach (r -> + console.log (r.reduce (a, v -> + a (if = v 1 then "*" else " ")) "") + ) +) in + +let m = {{0, 1, 0, 0, 0, 0, 0}, + {0, 0, 1, 0, 0, 0, 0}, + {1, 1, 1, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0}} +in + +(range 0 10).forEach (n -> + sequentially + (console.log (+ "Round #" n)) + (console.log "") + (grid.print (grid.advance_n_steps m n)) + (console.log "") +) From 877b1e6f515d551f472eb0b6af323b42c3d8aa1a Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Fri, 28 Aug 2015 10:48:23 +0200 Subject: [PATCH 07/35] New version of Game of Life --- life3.lambda | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 life3.lambda diff --git a/life3.lambda b/life3.lambda new file mode 100644 index 0000000..208d81a --- /dev/null +++ b/life3.lambda @@ -0,0 +1,61 @@ +# generates an array of n natural numbers +let range = (fix range, n -> + if > n 0 + then (range (- n 1)).concat {- n 1} + else {}) in + +# sequential execution +let seq = (fix f, x -> f) in + +# prints a grid +let print = (grid, next -> seq + (grid.forEach row -> + console.log ((row.map cell -> + if != 0 cell + then "*" + else " ").join "")) + (next grid)) in + +# wraps coordinates +let mod = (i, n -> % (+ (% i n) n) n) in + +# samples a circular array at the given index +let get = (array, i -> array[mod i array.length]) in + +# returns the count of live neighbors of the specified coordinates +let liveNeighbors = (grid, i, j -> + { + get (get grid (- i 1)) (- j 1), + get (get grid (- i 1)) j, + get (get grid (- i 1)) (+ j 1), + get (get grid i) (- j 1), + get (get grid i) (+ j 1), + get (get grid (+ i 1)) (- j 1), + get (get grid (+ i 1)) j, + get (get grid (+ i 1)) (+ j 1) + }.reduce + 0) in + +# steps a grid to the next state +let step = (grid -> + (range grid.length).map i -> + (range grid[i].length).map j -> + let liveCount = liveNeighbors grid i j in + if != 0 grid[i][j] + then if or (= 2 liveCount) (= 3 liveCount) then 1 else 0 + else if = 3 liveCount then 1 else 0) in + +# initial state +let grid = { + {0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, + {1, 1, 1, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} +} in + +print grid (fix f, grid -> print (step grid) f) From 69758a77e8cc31fe3df469df14297e372c1f7129 Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Sat, 29 Aug 2015 10:23:16 +0200 Subject: [PATCH 08/35] Updates --- test0.lambda | 2 +- test1.lambda | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test0.lambda b/test0.lambda index c33622d..d0ffc26 100644 --- a/test0.lambda +++ b/test0.lambda @@ -1,7 +1,7 @@ let range = fix (f, a, b -> if > a b then {} - else (f a (- b 1)).concat b) in + else (f a (- b 1)).concat {b}) in let fibonacci = fix (f, n -> if or (= n 1) (= n 2) diff --git a/test1.lambda b/test1.lambda index 92f6126..b7b4de8 100644 --- a/test1.lambda +++ b/test1.lambda @@ -13,7 +13,7 @@ in let range = fix (f, a, b -> if > a b then {} - else (f a (- b 1)).concat b) + else (f a (- b 1)).concat {b}) in (range 1 100).map (n -> From 6569ac310fd93d81cf922afb7ef46aa1d24413e3 Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Fri, 22 Apr 2016 18:14:36 +0100 Subject: [PATCH 09/35] Syntax update based on #62 --- life.lambda | 72 +++++++++++++++++++----------------------- life3.lambda | 87 +++++++++++++++++++++++++-------------------------- main.lambda | 2 +- server.lambda | 4 +-- test0.lambda | 20 ++++++------ test1.lambda | 14 ++++----- test2.lambda | 10 +++--- 7 files changed, 98 insertions(+), 111 deletions(-) diff --git a/life.lambda b/life.lambda index 9c62ff6..9f052b9 100644 --- a/life.lambda +++ b/life.lambda @@ -1,83 +1,78 @@ -let in_range = (v, a, b -> +let in_range = fn v, a, b -> and (>= v a) (<= v b) -) in +in -let min = (a, b -> +let min = fn a, b -> if < a b then a else b -) in +in -let max = (a, b -> +let max = fn a, b -> if > a b then a else b -) in +in -let range = fix (f, a, b -> +let range = fix fn f, a, b -> if > a b then {} else {a}.concat (f (+ a 1) b) -) in - -let sequentially = fix (f, x -> - f -) in +in -let sum = (l -> - l.reduce (a, v -> + a v) 0 -) in +let sum = fn l -> + l.reduce (fn a, v -> + a v) 0 +in -let grid.slice_row = (g, i, j -> +let grid.slice_row = fn g, i, j -> if in_range i 0 (- g.length 1) then g[i].slice (max (- j 1) 0) (min (+ j 2) g[i].length) else {} -) in +in -let grid.neighbors = (g, i, j -> +let grid.neighbors = fn g, i, j -> sum {sum (grid.slice_row g (- i 1) j), sum (grid.slice_row g i j), sum (grid.slice_row g (+ i 1) j), - 0 g[i][j]} -) in +in -let grid.next_state_from = (a, n -> +let grid.next_state_from = fn a, n -> if or (= n 3) (and (= a 1) (= n 2)) then 1 else 0 -) in +in -let grid.next_state = (g, i, j -> +let grid.next_state = fn g, i, j -> grid.next_state_from g[i][j] (grid.neighbors g i j) -) in +in -let grid.step_column = fix (f, g, i, j -> +let grid.step_column = fix fn f, g, i, j -> if in_range j 0 (- g[i].length 1) then {grid.next_state g i j}.concat (f g i (+ j 1)) else {} -) in +in -let grid.step_row = fix (f, g, i -> +let grid.step_row = fix fn f, g, i -> if in_range i 0 (- g.length 1) then {grid.step_column g i 0}.concat (f g (+ i 1)) else {} -) in +in -let grid.step = (g -> +let grid.step = fn g -> grid.step_row g 0 -) in +in -let grid.advance_n_steps = fix (f, g, n -> +let grid.advance_n_steps = fix fn f, g, n -> if > n 0 then f (grid.step g) (- n 1) else g -) in +in -let grid.print = (g -> - g.forEach (r -> - console.log (r.reduce (a, v -> + a (if = v 1 then "*" else " ")) "") - ) -) in +let grid.print = fn g -> + g.forEach fn r -> + console.log (r.reduce (fn a, v -> + a (if = v 1 then "*" else " ")) "") +in let m = {{0, 1, 0, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 0, 0}, @@ -88,10 +83,9 @@ let m = {{0, 1, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}} in -(range 0 10).forEach (n -> - sequentially +(range 0 10).forEach fn n -> + seq (console.log (+ "Round #" n)) (console.log "") (grid.print (grid.advance_n_steps m n)) (console.log "") -) diff --git a/life3.lambda b/life3.lambda index 208d81a..66b01d7 100644 --- a/life3.lambda +++ b/life3.lambda @@ -1,61 +1,58 @@ # generates an array of n natural numbers -let range = (fix range, n -> - if > n 0 - then (range (- n 1)).concat {- n 1} - else {}) in - -# sequential execution -let seq = (fix f, x -> f) in +let range = fix fn range, n -> + if > n 0 + then (range (- n 1)).concat {- n 1} + else {} in # prints a grid -let print = (grid, next -> seq - (grid.forEach row -> - console.log ((row.map cell -> - if != 0 cell - then "*" - else " ").join "")) - (next grid)) in +let print = fn grid, next -> seq + (grid.forEach fn row -> + console.log ((row.map fn cell -> + if != 0 cell + then "*" + else " ").join "")) + (next grid) in # wraps coordinates -let mod = (i, n -> % (+ (% i n) n) n) in +let mod = fn i, n -> % (+ (% i n) n) n in # samples a circular array at the given index -let get = (array, i -> array[mod i array.length]) in +let get = fn array, i -> array[mod i array.length] in # returns the count of live neighbors of the specified coordinates -let liveNeighbors = (grid, i, j -> - { - get (get grid (- i 1)) (- j 1), - get (get grid (- i 1)) j, - get (get grid (- i 1)) (+ j 1), - get (get grid i) (- j 1), - get (get grid i) (+ j 1), - get (get grid (+ i 1)) (- j 1), - get (get grid (+ i 1)) j, - get (get grid (+ i 1)) (+ j 1) - }.reduce + 0) in +let liveNeighbors = fn grid, i, j -> + { + get (get grid (- i 1)) (- j 1), + get (get grid (- i 1)) j, + get (get grid (- i 1)) (+ j 1), + get (get grid i) (- j 1), + get (get grid i) (+ j 1), + get (get grid (+ i 1)) (- j 1), + get (get grid (+ i 1)) j, + get (get grid (+ i 1)) (+ j 1) + }.reduce + 0 in # steps a grid to the next state -let step = (grid -> - (range grid.length).map i -> - (range grid[i].length).map j -> - let liveCount = liveNeighbors grid i j in - if != 0 grid[i][j] - then if or (= 2 liveCount) (= 3 liveCount) then 1 else 0 - else if = 3 liveCount then 1 else 0) in +let step = fn grid -> + (range grid.length).map fn i -> + (range grid[i].length).map fn j -> + let liveCount = liveNeighbors grid i j in + if != 0 grid[i][j] + then if or (= 2 liveCount) (= 3 liveCount) then 1 else 0 + else if = 3 liveCount then 1 else 0 in # initial state let grid = { - {0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, - {1, 1, 1, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} + {0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, + {1, 1, 1, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} } in -print grid (fix f, grid -> print (step grid) f) +print grid (fix fn f, grid -> print (step grid) f) diff --git a/main.lambda b/main.lambda index 77ba722..8b6d47b 100644 --- a/main.lambda +++ b/main.lambda @@ -1,4 +1,4 @@ -let main = fix (f, x -> f) in +let main = fix fn f, x -> f in main (console.log "hello 1") diff --git a/server.lambda b/server.lambda index 2ffe4a6..f054190 100644 --- a/server.lambda +++ b/server.lambda @@ -1,6 +1,6 @@ let http = require "http" in -let server = http.createServer (request, response -> - response.end.apply {"Oh hai!"}) in +let server = http.createServer fn request, response -> + response.end.apply {"Oh hai!"} in server.listen 8080 diff --git a/test0.lambda b/test0.lambda index d0ffc26..bb5fa4e 100644 --- a/test0.lambda +++ b/test0.lambda @@ -1,19 +1,17 @@ -let range = fix (f, a, b -> +let range = fix fn f, a, b -> if > a b then {} - else (f a (- b 1)).concat {b}) in - -let fibonacci = fix (f, n -> + else (f a (- b 1)).concat {b} in + +let fibonacci = fix fn f, n -> if or (= n 1) (= n 2) then 1 - else + (f (- n 1)) (f (- n 2))) in - -let square = n -> + else + (f (- n 1)) (f (- n 2)) in + +let square = fn n -> ** n 2 in - -let main = fix (f, a -> f) in - -main + +seq (console.log ((range 1 5).map square)) (console.log ((range 1 5).map fibonacci)) (console.log "Ammazza, oh, cheffico!") diff --git a/test1.lambda b/test1.lambda index b7b4de8..2fbfd26 100644 --- a/test1.lambda +++ b/test1.lambda @@ -1,23 +1,23 @@ -let fizz = n -> +let fizz = fn n -> = 0 (% n 3) in -let buzz = n -> +let buzz = fn n -> = 0 (% n 5) in -let fizzbuzz = n -> +let fizzbuzz = fn n -> and (fizz n) (buzz n) in -let range = fix (f, a, b -> +let range = fix fn f, a, b -> if > a b then {} - else (f a (- b 1)).concat {b}) + else (f a (- b 1)).concat {b} in -(range 1 100).map (n -> +(range 1 100).map fn n -> if fizzbuzz n then "FizzBuzz" else if fizz n then "Fizz" else if buzz n then "Buzz" - else n) + else n diff --git a/test2.lambda b/test2.lambda index b2d8074..c49534f 100644 --- a/test2.lambda +++ b/test2.lambda @@ -1,9 +1,7 @@ -let main = fix (f, x -> f) in - -let add = y -> + this.x y in +let add = fn y -> + this.x y in let o1.x = 123, o1.add = add in let o2.x = 456, o2.add = add in -main - (console.log (o1.add 321)) - (console.log (o2.add 321)) +seq + (console.log (o1.add 321)) + (console.log (o2.add 321)) From 10e9ce51762f672ccc4c20539c9f22b8ab308968 Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Sat, 23 Apr 2016 03:45:27 +0100 Subject: [PATCH 10/35] Fibonacci --- .gitignore | 3 +++ fibonacci.lambda | 6 ++++++ 2 files changed, 9 insertions(+) create mode 100644 .gitignore create mode 100644 fibonacci.lambda diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f190142 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules/ +/bin/ +/doc/ diff --git a/fibonacci.lambda b/fibonacci.lambda new file mode 100644 index 0000000..2bc18ff --- /dev/null +++ b/fibonacci.lambda @@ -0,0 +1,6 @@ +let fibonacci = fix fn f, n -> + if < n 2 + then 1 + else + (f (- n 1)) (f (- n 2)) in + +{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}.map fibonacci From caa3103973b737ddf60f7ccb764f42e3965e9ce3 Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Sat, 23 Apr 2016 12:10:38 +0100 Subject: [PATCH 11/35] forEach is now each --- life.lambda | 4 ++-- life3.lambda | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/life.lambda b/life.lambda index 9f052b9..16a4289 100644 --- a/life.lambda +++ b/life.lambda @@ -70,7 +70,7 @@ let grid.advance_n_steps = fix fn f, g, n -> in let grid.print = fn g -> - g.forEach fn r -> + g.each fn r -> console.log (r.reduce (fn a, v -> + a (if = v 1 then "*" else " ")) "") in @@ -83,7 +83,7 @@ let m = {{0, 1, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}} in -(range 0 10).forEach fn n -> +(range 0 10).each fn n -> seq (console.log (+ "Round #" n)) (console.log "") diff --git a/life3.lambda b/life3.lambda index 66b01d7..d5347c5 100644 --- a/life3.lambda +++ b/life3.lambda @@ -6,7 +6,7 @@ let range = fix fn range, n -> # prints a grid let print = fn grid, next -> seq - (grid.forEach fn row -> + (grid.each fn row -> console.log ((row.map fn cell -> if != 0 cell then "*" From 1ffd57d53275d706f83e102b48edfaa97cb29baf Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Sat, 23 Apr 2016 20:31:38 +0100 Subject: [PATCH 12/35] Refactors the Game of Life --- life3.lambda | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/life3.lambda b/life3.lambda index d5347c5..b896380 100644 --- a/life3.lambda +++ b/life3.lambda @@ -5,13 +5,12 @@ let range = fix fn range, n -> else {} in # prints a grid -let print = fn grid, next -> seq +let print = fn grid -> (grid.each fn row -> console.log ((row.map fn cell -> if != 0 cell then "*" - else " ").join "")) - (next grid) in + else " ").join "")) in # wraps coordinates let mod = fn i, n -> % (+ (% i n) n) n in @@ -20,7 +19,7 @@ let mod = fn i, n -> % (+ (% i n) n) n in let get = fn array, i -> array[mod i array.length] in # returns the count of live neighbors of the specified coordinates -let liveNeighbors = fn grid, i, j -> +let live_neighbors = fn grid, i, j -> { get (get grid (- i 1)) (- j 1), get (get grid (- i 1)) j, @@ -36,10 +35,10 @@ let liveNeighbors = fn grid, i, j -> let step = fn grid -> (range grid.length).map fn i -> (range grid[i].length).map fn j -> - let liveCount = liveNeighbors grid i j in + let live_count = live_neighbors grid i j in if != 0 grid[i][j] - then if or (= 2 liveCount) (= 3 liveCount) then 1 else 0 - else if = 3 liveCount then 1 else 0 in + then if or (= 2 live_count) (= 3 live_count) then 1 else 0 + else if = 3 live_count then 1 else 0 in # initial state let grid = { @@ -55,4 +54,7 @@ let grid = { {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} } in -print grid (fix fn f, grid -> print (step grid) f) +(fix fn f, grid -> + seq + (print grid) + (f (step grid))) grid From 7832c15654748305f650c2482ce9c4f0fc763350 Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Sun, 1 May 2016 11:31:07 +0100 Subject: [PATCH 13/35] Types --- fibonacci.lambda | 2 +- life3.lambda | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/fibonacci.lambda b/fibonacci.lambda index 2bc18ff..262ace7 100644 --- a/fibonacci.lambda +++ b/fibonacci.lambda @@ -1,4 +1,4 @@ -let fibonacci = fix fn f, n -> +let fibonacci = fix fn f, n: int -> if < n 2 then 1 else + (f (- n 1)) (f (- n 2)) in diff --git a/life3.lambda b/life3.lambda index b896380..59acdf6 100644 --- a/life3.lambda +++ b/life3.lambda @@ -1,25 +1,25 @@ # generates an array of n natural numbers -let range = fix fn range, n -> +let range = fix fn range, n: int -> if > n 0 then (range (- n 1)).concat {- n 1} else {} in # prints a grid -let print = fn grid -> - (grid.each fn row -> - console.log ((row.map fn cell -> +let print = fn grid: uint** -> + (grid.each fn row: uint* -> + console.log ((row.map fn cell: uint -> if != 0 cell then "*" else " ").join "")) in # wraps coordinates -let mod = fn i, n -> % (+ (% i n) n) n in +let mod = fn i: int, n: uint -> % (+ (% i n) n) n in # samples a circular array at the given index -let get = fn array, i -> array[mod i array.length] in +let get = fn array: uint*, i: int -> array[mod i array.length] in # returns the count of live neighbors of the specified coordinates -let live_neighbors = fn grid, i, j -> +let live_neighbors = fn grid: uint**, i: int, j: int -> { get (get grid (- i 1)) (- j 1), get (get grid (- i 1)) j, @@ -32,9 +32,9 @@ let live_neighbors = fn grid, i, j -> }.reduce + 0 in # steps a grid to the next state -let step = fn grid -> - (range grid.length).map fn i -> - (range grid[i].length).map fn j -> +let step = fn grid: uint** -> + (range grid.length).map fn i: int -> + (range grid[i].length).map fn j: int -> let live_count = live_neighbors grid i j in if != 0 grid[i][j] then if or (= 2 live_count) (= 3 live_count) then 1 else 0 @@ -54,7 +54,7 @@ let grid = { {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} } in -(fix fn f, grid -> +(fix fn f, grid: uint** -> seq (print grid) (f (step grid))) grid From caf1427152eda7d36ed8371cad843f0152363ded Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Sun, 15 May 2016 12:25:18 +0100 Subject: [PATCH 14/35] Fixes for #77 --- fibonacci.lambda | 2 +- life3.lambda | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/fibonacci.lambda b/fibonacci.lambda index 262ace7..81053d1 100644 --- a/fibonacci.lambda +++ b/fibonacci.lambda @@ -1,4 +1,4 @@ -let fibonacci = fix fn f, n: int -> +let fibonacci = fix fn f, n: integer -> if < n 2 then 1 else + (f (- n 1)) (f (- n 2)) in diff --git a/life3.lambda b/life3.lambda index 59acdf6..c63e83f 100644 --- a/life3.lambda +++ b/life3.lambda @@ -1,25 +1,25 @@ # generates an array of n natural numbers -let range = fix fn range, n: int -> +let range = fix fn range, n: integer -> if > n 0 then (range (- n 1)).concat {- n 1} else {} in # prints a grid -let print = fn grid: uint** -> - (grid.each fn row: uint* -> - console.log ((row.map fn cell: uint -> +let print = fn grid: natural** -> + (grid.each fn row: natural* -> + console.log ((row.map fn cell: natural -> if != 0 cell then "*" else " ").join "")) in # wraps coordinates -let mod = fn i: int, n: uint -> % (+ (% i n) n) n in +let mod = fn i: integer, n: natural -> % (+ (% i n) n) n in # samples a circular array at the given index -let get = fn array: uint*, i: int -> array[mod i array.length] in +let get = fn array: natural*, i: integer -> array[mod i array.length] in # returns the count of live neighbors of the specified coordinates -let live_neighbors = fn grid: uint**, i: int, j: int -> +let live_neighbors = fn grid: natural**, i: integer, j: integer -> { get (get grid (- i 1)) (- j 1), get (get grid (- i 1)) j, @@ -32,9 +32,9 @@ let live_neighbors = fn grid: uint**, i: int, j: int -> }.reduce + 0 in # steps a grid to the next state -let step = fn grid: uint** -> - (range grid.length).map fn i: int -> - (range grid[i].length).map fn j: int -> +let step = fn grid: natural** -> + (range grid.length).map fn i: integer -> + (range grid[i].length).map fn j: integer -> let live_count = live_neighbors grid i j in if != 0 grid[i][j] then if or (= 2 live_count) (= 3 live_count) then 1 else 0 @@ -54,7 +54,7 @@ let grid = { {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} } in -(fix fn f, grid: uint** -> +(fix fn f, grid: natural** -> seq (print grid) (f (step grid))) grid From edf15c07f632b015ced819d1ff5a35a263b7965c Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Sun, 15 May 2016 14:16:37 +0100 Subject: [PATCH 15/35] Fix --- life.lambda | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/life.lambda b/life.lambda index 16a4289..147b4a0 100644 --- a/life.lambda +++ b/life.lambda @@ -85,7 +85,7 @@ in (range 0 10).each fn n -> seq - (console.log (+ "Round #" n)) + (console.log (+ "Round #" n.str)) (console.log "") (grid.print (grid.advance_n_steps m n)) (console.log "") From 5b67d3c432b9fb37e1a718e63ae1e3cebd4bc911 Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Tue, 17 May 2016 12:50:45 +0100 Subject: [PATCH 16/35] New convention for list reduction --- life.lambda | 4 ++-- life3.lambda | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/life.lambda b/life.lambda index 147b4a0..0339832 100644 --- a/life.lambda +++ b/life.lambda @@ -21,7 +21,7 @@ let range = fix fn f, a, b -> in let sum = fn l -> - l.reduce (fn a, v -> + a v) 0 + l.reduce 0 + in let grid.slice_row = fn g, i, j -> @@ -71,7 +71,7 @@ in let grid.print = fn g -> g.each fn r -> - console.log (r.reduce (fn a, v -> + a (if = v 1 then "*" else " ")) "") + console.log (r.reduce "" (fn v, a -> + a (if = v 1 then "*" else " "))) in let m = {{0, 1, 0, 0, 0, 0, 0}, diff --git a/life3.lambda b/life3.lambda index c63e83f..360b2c9 100644 --- a/life3.lambda +++ b/life3.lambda @@ -29,7 +29,7 @@ let live_neighbors = fn grid: natural**, i: integer, j: integer -> get (get grid (+ i 1)) (- j 1), get (get grid (+ i 1)) j, get (get grid (+ i 1)) (+ j 1) - }.reduce + 0 in + }.reduce 0 + in # steps a grid to the next state let step = fn grid: natural** -> From ab06c81e9dc8e55503c24ae0d94f0d9546000c85 Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Tue, 17 May 2016 12:53:50 +0100 Subject: [PATCH 17/35] Hanoi --- hanoi.lambda | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 hanoi.lambda diff --git a/hanoi.lambda b/hanoi.lambda new file mode 100644 index 0000000..477a4e7 --- /dev/null +++ b/hanoi.lambda @@ -0,0 +1,9 @@ +let Hanoi = fix fn Hanoi, n: integer, a, b, c -> + if < n 2 + then console.log ({a, c}.join " to ") + else seq + (Hanoi (- n 1) a c b) + (Hanoi 1 a b c) + (Hanoi (- n 1) b a c) in + +Hanoi 7 "a" "b" "c" From 02146b0208acebbec292728e6051674b7ac26cbc Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Tue, 17 May 2016 16:42:48 +0100 Subject: [PATCH 18/35] Trailing commas! Yay! --- life3.lambda | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/life3.lambda b/life3.lambda index 360b2c9..e4262bf 100644 --- a/life3.lambda +++ b/life3.lambda @@ -51,7 +51,7 @@ let grid = { {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, } in (fix fn f, grid: natural** -> From 9dbf577e02b574083d822c993a311c462b62eed7 Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Tue, 17 May 2016 16:54:30 +0100 Subject: [PATCH 19/35] `append` --- life3.lambda | 2 +- test0.lambda | 2 +- test1.lambda | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/life3.lambda b/life3.lambda index e4262bf..dce9cee 100644 --- a/life3.lambda +++ b/life3.lambda @@ -1,7 +1,7 @@ # generates an array of n natural numbers let range = fix fn range, n: integer -> if > n 0 - then (range (- n 1)).concat {- n 1} + then (range (- n 1)).append (- n 1) else {} in # prints a grid diff --git a/test0.lambda b/test0.lambda index bb5fa4e..b6819aa 100644 --- a/test0.lambda +++ b/test0.lambda @@ -1,7 +1,7 @@ let range = fix fn f, a, b -> if > a b then {} - else (f a (- b 1)).concat {b} in + else (f a (- b 1)).append (b) in let fibonacci = fix fn f, n -> if or (= n 1) (= n 2) diff --git a/test1.lambda b/test1.lambda index 2fbfd26..10558e6 100644 --- a/test1.lambda +++ b/test1.lambda @@ -13,7 +13,7 @@ in let range = fix fn f, a, b -> if > a b then {} - else (f a (- b 1)).concat {b} + else (f a (- b 1)).append b in (range 1 100).map fn n -> From 2e06bfbfc487867aff40cd3e7a6406aa71f277de Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Wed, 18 May 2016 12:33:37 +0100 Subject: [PATCH 20/35] See #26 --- server.lambda | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.lambda b/server.lambda index f054190..bc294d4 100644 --- a/server.lambda +++ b/server.lambda @@ -1,4 +1,4 @@ -let http = require "http" in +let http = process.mainModule.require "http" in let server = http.createServer fn request, response -> response.end.apply {"Oh hai!"} in From f4ad926c45cbbc5e39f90cd1dc2d6c04f5c55e46 Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Tue, 24 May 2016 16:58:54 +0100 Subject: [PATCH 21/35] First bug found in a Lambda program! --- life.lambda | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/life.lambda b/life.lambda index 0339832..c19f5c7 100644 --- a/life.lambda +++ b/life.lambda @@ -26,7 +26,7 @@ in let grid.slice_row = fn g, i, j -> if in_range i 0 (- g.length 1) - then g[i].slice (max (- j 1) 0) (min (+ j 2) g[i].length) + then g[i].slice (max (- j 1) 0) (min (+ j 2) (- g[i].length 1)) else {} in From 44754d072f718607d73d166f4e2009fb1c85641d Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Fri, 17 Jun 2016 18:58:29 +0100 Subject: [PATCH 22/35] Fixes for #84 --- life.lambda | 18 +++++++++--------- test2.lambda | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/life.lambda b/life.lambda index c19f5c7..aec5459 100644 --- a/life.lambda +++ b/life.lambda @@ -24,52 +24,52 @@ let sum = fn l -> l.reduce 0 + in -let grid.slice_row = fn g, i, j -> +let grid.slice_row = fn grid, g, i, j -> if in_range i 0 (- g.length 1) then g[i].slice (max (- j 1) 0) (min (+ j 2) (- g[i].length 1)) else {} in -let grid.neighbors = fn g, i, j -> +let grid.neighbors = fn grid, g, i, j -> sum {sum (grid.slice_row g (- i 1) j), sum (grid.slice_row g i j), sum (grid.slice_row g (+ i 1) j), - 0 g[i][j]} in -let grid.next_state_from = fn a, n -> +let grid.next_state_from = fn grid, a, n -> if or (= n 3) (and (= a 1) (= n 2)) then 1 else 0 in -let grid.next_state = fn g, i, j -> +let grid.next_state = fn grid, g, i, j -> grid.next_state_from g[i][j] (grid.neighbors g i j) in -let grid.step_column = fix fn f, g, i, j -> +let grid.step_column = fix fn f, grid, g, i, j -> if in_range j 0 (- g[i].length 1) then {grid.next_state g i j}.concat (f g i (+ j 1)) else {} in -let grid.step_row = fix fn f, g, i -> +let grid.step_row = fix fn f, grid, g, i -> if in_range i 0 (- g.length 1) then {grid.step_column g i 0}.concat (f g (+ i 1)) else {} in -let grid.step = fn g -> +let grid.step = fn grid, g -> grid.step_row g 0 in -let grid.advance_n_steps = fix fn f, g, n -> +let grid.advance_n_steps = fix fn f, grid, g, n -> if > n 0 then f (grid.step g) (- n 1) else g in -let grid.print = fn g -> +let grid.print = fn grid, g -> g.each fn r -> console.log (r.reduce "" (fn v, a -> + a (if = v 1 then "*" else " "))) in diff --git a/test2.lambda b/test2.lambda index c49534f..5631586 100644 --- a/test2.lambda +++ b/test2.lambda @@ -1,4 +1,4 @@ -let add = fn y -> + this.x y in +let add = fn this, y -> + this.x y in let o1.x = 123, o1.add = add in let o2.x = 456, o2.add = add in From e7da19f757e29aa55929f7d5f697ebd0f5ba07b8 Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Sun, 19 Jun 2016 17:27:15 +0100 Subject: [PATCH 23/35] Fixes for #84 See #99 --- life.lambda | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/life.lambda b/life.lambda index aec5459..289e740 100644 --- a/life.lambda +++ b/life.lambda @@ -47,13 +47,13 @@ let grid.next_state = fn grid, g, i, j -> grid.next_state_from g[i][j] (grid.neighbors g i j) in -let grid.step_column = fix fn f, grid, g, i, j -> +let grid.step_column = fn grid -> fix fn f, g, i, j -> if in_range j 0 (- g[i].length 1) then {grid.next_state g i j}.concat (f g i (+ j 1)) else {} in -let grid.step_row = fix fn f, grid, g, i -> +let grid.step_row = fn grid -> fix fn f, g, i -> if in_range i 0 (- g.length 1) then {grid.step_column g i 0}.concat (f g (+ i 1)) else {} @@ -63,7 +63,7 @@ let grid.step = fn grid, g -> grid.step_row g 0 in -let grid.advance_n_steps = fix fn f, grid, g, n -> +let grid.advance_n_steps = fn grid -> fix fn f, g, n -> if > n 0 then f (grid.step g) (- n 1) else g From 7a1e398ef199d29260bad29d68dba5c373546f3c Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Wed, 7 Sep 2016 19:39:21 +0100 Subject: [PATCH 24/35] Latest changes --- fibonacci.lambda | 4 ++-- hanoi.lambda | 10 +++++----- life.lambda | 44 ++++++++++++++++++++++---------------------- life3.lambda | 38 +++++++++++++++++++------------------- main.lambda | 6 +++--- server.lambda | 8 ++++---- test0.lambda | 16 ++++++++-------- test1.lambda | 16 ++++++++-------- test2.lambda | 6 +++--- 9 files changed, 74 insertions(+), 74 deletions(-) diff --git a/fibonacci.lambda b/fibonacci.lambda index 81053d1..b062e1b 100644 --- a/fibonacci.lambda +++ b/fibonacci.lambda @@ -1,6 +1,6 @@ let fibonacci = fix fn f, n: integer -> - if < n 2 + if n < 2 then 1 - else + (f (- n 1)) (f (- n 2)) in + else (f (n - 1)) + (f (n - 2)) in {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}.map fibonacci diff --git a/hanoi.lambda b/hanoi.lambda index 477a4e7..3eb41c9 100644 --- a/hanoi.lambda +++ b/hanoi.lambda @@ -1,9 +1,9 @@ let Hanoi = fix fn Hanoi, n: integer, a, b, c -> - if < n 2 - then console.log ({a, c}.join " to ") + if n < 2 + then console.log {{a, c}.join ' to '} else seq - (Hanoi (- n 1) a c b) + (Hanoi (n - 1) a c b) (Hanoi 1 a b c) - (Hanoi (- n 1) b a c) in + (Hanoi (n - 1) b a c) in -Hanoi 7 "a" "b" "c" +Hanoi 7 'a' 'b' 'c' diff --git a/life.lambda b/life.lambda index 289e740..fbc3096 100644 --- a/life.lambda +++ b/life.lambda @@ -1,44 +1,44 @@ let in_range = fn v, a, b -> - and (>= v a) (<= v b) + v >= a and v <= b in let min = fn a, b -> - if < a b + if a < b then a else b in let max = fn a, b -> - if > a b + if a > b then a else b in let range = fix fn f, a, b -> - if > a b + if a > b then {} - else {a}.concat (f (+ a 1) b) + else {a}.concat (f (a + 1) b) in let sum = fn l -> - l.reduce 0 + + l.reduce 0 (+) in let grid.slice_row = fn grid, g, i, j -> - if in_range i 0 (- g.length 1) - then g[i].slice (max (- j 1) 0) (min (+ j 2) (- g[i].length 1)) + if in_range i 0 (g.length - 1) + then g[i].slice (max (j - 1) 0) (min (j + 2) (g[i].length - 1)) else {} in let grid.neighbors = fn grid, g, i, j -> - sum {sum (grid.slice_row g (- i 1) j), + sum {sum (grid.slice_row g (i - 1) j), sum (grid.slice_row g i j), - sum (grid.slice_row g (+ i 1) j), - - 0 g[i][j]} + sum (grid.slice_row g (i + 1) j), + 0 - g[i][j]} in let grid.next_state_from = fn grid, a, n -> - if or (= n 3) (and (= a 1) (= n 2)) + if n = 3 or a = 1 and n = 2 then 1 else 0 in @@ -48,14 +48,14 @@ let grid.next_state = fn grid, g, i, j -> in let grid.step_column = fn grid -> fix fn f, g, i, j -> - if in_range j 0 (- g[i].length 1) - then {grid.next_state g i j}.concat (f g i (+ j 1)) + if in_range j 0 (g[i].length - 1) + then {grid.next_state g i j}.concat (f g i (j + 1)) else {} in let grid.step_row = fn grid -> fix fn f, g, i -> - if in_range i 0 (- g.length 1) - then {grid.step_column g i 0}.concat (f g (+ i 1)) + if in_range i 0 (g.length - 1) + then {grid.step_column g i 0}.concat (f g (i + 1)) else {} in @@ -64,14 +64,14 @@ let grid.step = fn grid, g -> in let grid.advance_n_steps = fn grid -> fix fn f, g, n -> - if > n 0 - then f (grid.step g) (- n 1) + if n > 0 + then f (grid.step g) (n - 1) else g in let grid.print = fn grid, g -> g.each fn r -> - console.log (r.reduce "" (fn v, a -> + a (if = v 1 then "*" else " "))) + console.log {r.reduce '' (fn v, a -> a + if v = 1 then '*' else ' ')} in let m = {{0, 1, 0, 0, 0, 0, 0}, @@ -85,7 +85,7 @@ in (range 0 10).each fn n -> seq - (console.log (+ "Round #" n.str)) - (console.log "") + (console.log {'Round #' + n.str}) + (console.log {''}) (grid.print (grid.advance_n_steps m n)) - (console.log "") + (console.log {''}) diff --git a/life3.lambda b/life3.lambda index dce9cee..5b3fcf7 100644 --- a/life3.lambda +++ b/life3.lambda @@ -1,19 +1,19 @@ # generates an array of n natural numbers let range = fix fn range, n: integer -> - if > n 0 - then (range (- n 1)).append (- n 1) + if n > 0 + then (range (n - 1)).append (n - 1) else {} in # prints a grid let print = fn grid: natural** -> (grid.each fn row: natural* -> - console.log ((row.map fn cell: natural -> - if != 0 cell - then "*" - else " ").join "")) in + console.log {(row.map fn cell: natural -> + if 0 != cell + then '*' + else ' ').join ''}) in # wraps coordinates -let mod = fn i: integer, n: natural -> % (+ (% i n) n) n in +let mod = fn i: integer, n: natural -> (i % n + n) % n in # samples a circular array at the given index let get = fn array: natural*, i: integer -> array[mod i array.length] in @@ -21,24 +21,24 @@ let get = fn array: natural*, i: integer -> array[mod i array.length] in # returns the count of live neighbors of the specified coordinates let live_neighbors = fn grid: natural**, i: integer, j: integer -> { - get (get grid (- i 1)) (- j 1), - get (get grid (- i 1)) j, - get (get grid (- i 1)) (+ j 1), - get (get grid i) (- j 1), - get (get grid i) (+ j 1), - get (get grid (+ i 1)) (- j 1), - get (get grid (+ i 1)) j, - get (get grid (+ i 1)) (+ j 1) - }.reduce 0 + in + get (get grid (i - 1)) (j - 1), + get (get grid (i - 1)) j, + get (get grid (i - 1)) (j + 1), + get (get grid i) (j - 1), + get (get grid i) (j + 1), + get (get grid (i + 1)) (j - 1), + get (get grid (i + 1)) j, + get (get grid (i + 1)) (j + 1) + }.reduce 0 (+) in # steps a grid to the next state let step = fn grid: natural** -> (range grid.length).map fn i: integer -> (range grid[i].length).map fn j: integer -> let live_count = live_neighbors grid i j in - if != 0 grid[i][j] - then if or (= 2 live_count) (= 3 live_count) then 1 else 0 - else if = 3 live_count then 1 else 0 in + if 0 != grid[i][j] + then if 2 = live_count or 3 = live_count then 1 else 0 + else if 3 = live_count then 1 else 0 in # initial state let grid = { diff --git a/main.lambda b/main.lambda index 8b6d47b..e87fc33 100644 --- a/main.lambda +++ b/main.lambda @@ -1,6 +1,6 @@ let main = fix fn f, x -> f in main - (console.log "hello 1") - (console.log "hello 2") - (console.log "hello 3") + (console.log {'hello 1'}) + (console.log {'hello 2'}) + (console.log {'hello 3'}) diff --git a/server.lambda b/server.lambda index bc294d4..cbdedf0 100644 --- a/server.lambda +++ b/server.lambda @@ -1,6 +1,6 @@ -let http = process.mainModule.require "http" in +let http = process.mainModule.require {'http'} in -let server = http.createServer fn request, response -> - response.end.apply {"Oh hai!"} in +let server = http.createServer {fn request, response -> + response.end {'Oh hai!'}} in -server.listen 8080 +server.listen {8080} diff --git a/test0.lambda b/test0.lambda index b6819aa..bfdf9ce 100644 --- a/test0.lambda +++ b/test0.lambda @@ -1,17 +1,17 @@ let range = fix fn f, a, b -> - if > a b + if a > b then {} - else (f a (- b 1)).append (b) in + else (f a (b - 1)).append b in let fibonacci = fix fn f, n -> - if or (= n 1) (= n 2) + if n = 1 or n = 2 then 1 - else + (f (- n 1)) (f (- n 2)) in + else (f (n - 1)) + (f (n - 2)) in let square = fn n -> - ** n 2 in + n ** 2 in seq - (console.log ((range 1 5).map square)) - (console.log ((range 1 5).map fibonacci)) - (console.log "Ammazza, oh, cheffico!") + (console.log {(range 1 5).map square}) + (console.log {(range 1 5).map fibonacci}) + (console.log {'Ammazza, oh, cheffico!'}) diff --git a/test1.lambda b/test1.lambda index 10558e6..e953b18 100644 --- a/test1.lambda +++ b/test1.lambda @@ -1,23 +1,23 @@ let fizz = fn n -> - = 0 (% n 3) + 0 = n % 3 in let buzz = fn n -> - = 0 (% n 5) + 0 = n % 5 in let fizzbuzz = fn n -> - and (fizz n) (buzz n) + fizz n and buzz n in let range = fix fn f, a, b -> - if > a b + if a > b then {} - else (f a (- b 1)).append b + else (f a (b - 1)).append b in (range 1 100).map fn n -> - if fizzbuzz n then "FizzBuzz" - else if fizz n then "Fizz" - else if buzz n then "Buzz" + if fizzbuzz n then 'FizzBuzz' + else if fizz n then 'Fizz' + else if buzz n then 'Buzz' else n diff --git a/test2.lambda b/test2.lambda index 5631586..aeee26f 100644 --- a/test2.lambda +++ b/test2.lambda @@ -1,7 +1,7 @@ -let add = fn this, y -> + this.x y in +let add = fn this, y -> this.x + y in let o1.x = 123, o1.add = add in let o2.x = 456, o2.add = add in seq - (console.log (o1.add 321)) - (console.log (o2.add 321)) + (console.log {o1.add 321}) + (console.log {o2.add 321}) From 4b23913c0a8427379b259070a9ae3975394ab86a Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Thu, 8 Sep 2016 18:12:52 +0100 Subject: [PATCH 25/35] Update --- test0.lambda | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test0.lambda b/test0.lambda index bfdf9ce..27aa95b 100644 --- a/test0.lambda +++ b/test0.lambda @@ -1,15 +1,14 @@ let range = fix fn f, a, b -> if a > b then {} - else (f a (b - 1)).append b in + else (f a (b - 1)) + {b} in let fibonacci = fix fn f, n -> if n = 1 or n = 2 then 1 else (f (n - 1)) + (f (n - 2)) in -let square = fn n -> - n ** 2 in +let square = fn n -> n ** 2 in seq (console.log {(range 1 5).map square}) From f393680c0f59b0a2f9cea28a39b7fe87f8533c5d Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Thu, 8 Sep 2016 18:13:40 +0100 Subject: [PATCH 26/35] Update --- test1.lambda | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test1.lambda b/test1.lambda index e953b18..b0d4ee8 100644 --- a/test1.lambda +++ b/test1.lambda @@ -13,7 +13,7 @@ in let range = fix fn f, a, b -> if a > b then {} - else (f a (b - 1)).append b + else (f a (b - 1)) + {b} in (range 1 100).map fn n -> From 662ea9ab796f97bda58d3e70f40fa398d437ae06 Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Sun, 11 Sep 2016 14:08:24 +0100 Subject: [PATCH 27/35] Update --- life.lambda | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/life.lambda b/life.lambda index fbc3096..518eea8 100644 --- a/life.lambda +++ b/life.lambda @@ -17,7 +17,7 @@ in let range = fix fn f, a, b -> if a > b then {} - else {a}.concat (f (a + 1) b) + else {a} + (f (a + 1) b) in let sum = fn l -> @@ -49,13 +49,13 @@ in let grid.step_column = fn grid -> fix fn f, g, i, j -> if in_range j 0 (g[i].length - 1) - then {grid.next_state g i j}.concat (f g i (j + 1)) + then {grid.next_state g i j} + (f g i (j + 1)) else {} in let grid.step_row = fn grid -> fix fn f, g, i -> if in_range i 0 (g.length - 1) - then {grid.step_column g i 0}.concat (f g (i + 1)) + then {grid.step_column g i 0} + (f g (i + 1)) else {} in From 4ee6235d32c794e4d5d4c6c01fe5563d03f00ae5 Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Tue, 13 Sep 2016 14:29:04 +0100 Subject: [PATCH 28/35] `range` is now a built-in --- life.lambda | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/life.lambda b/life.lambda index 518eea8..7a25fcb 100644 --- a/life.lambda +++ b/life.lambda @@ -14,12 +14,6 @@ let max = fn a, b -> else b in -let range = fix fn f, a, b -> - if a > b - then {} - else {a} + (f (a + 1) b) -in - let sum = fn l -> l.reduce 0 (+) in @@ -83,7 +77,7 @@ let m = {{0, 1, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}} in -(range 0 10).each fn n -> +(range 10).each fn n -> seq (console.log {'Round #' + n.str}) (console.log {''}) From 5c3568871cbf4a20ac2b57aee555f88a714d1b88 Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Wed, 14 Sep 2016 11:12:16 +0100 Subject: [PATCH 29/35] Update --- life.lambda | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/life.lambda b/life.lambda index 7a25fcb..cc5f67a 100644 --- a/life.lambda +++ b/life.lambda @@ -65,7 +65,7 @@ in let grid.print = fn grid, g -> g.each fn r -> - console.log {r.reduce '' (fn v, a -> a + if v = 1 then '*' else ' ')} + console.log {r.reduce '' (fn a, v -> a + if v = 1 then '*' else ' ')} in let m = {{0, 1, 0, 0, 0, 0, 0}, From ccab4bbeca595b01840ea7ff7685517601fedf9e Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Wed, 14 Sep 2016 21:51:26 +0100 Subject: [PATCH 30/35] range is now a predefined term --- life3.lambda | 6 ------ 1 file changed, 6 deletions(-) diff --git a/life3.lambda b/life3.lambda index 5b3fcf7..ce501bf 100644 --- a/life3.lambda +++ b/life3.lambda @@ -1,9 +1,3 @@ -# generates an array of n natural numbers -let range = fix fn range, n: integer -> - if n > 0 - then (range (n - 1)).append (n - 1) - else {} in - # prints a grid let print = fn grid: natural** -> (grid.each fn row: natural* -> From fdf0074ad436509d9acaa7cba8b312fef02813d9 Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Sun, 18 Sep 2016 15:57:34 +0100 Subject: [PATCH 31/35] Game of Life --- life.lambda | 14 +---------- life4.lambda | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 13 deletions(-) create mode 100644 life4.lambda diff --git a/life.lambda b/life.lambda index cc5f67a..06e4349 100644 --- a/life.lambda +++ b/life.lambda @@ -2,25 +2,13 @@ let in_range = fn v, a, b -> v >= a and v <= b in -let min = fn a, b -> - if a < b - then a - else b -in - -let max = fn a, b -> - if a > b - then a - else b -in - let sum = fn l -> l.reduce 0 (+) in let grid.slice_row = fn grid, g, i, j -> if in_range i 0 (g.length - 1) - then g[i].slice (max (j - 1) 0) (min (j + 2) (g[i].length - 1)) + then g[i].slice {j - 1, 0}.max {j + 2, g[i].length - 1}.min else {} in diff --git a/life4.lambda b/life4.lambda new file mode 100644 index 0000000..7e227a6 --- /dev/null +++ b/life4.lambda @@ -0,0 +1,68 @@ +let Grid = fix fn Grid, matrix -> + let mod = fn n, i -> (i % n + n) % n in + + let get = fn array, i -> array[mod array.length i] in + + let matrix = matrix.map fn row -> + let row.get = get in row, + matrix.get = get in + + let grid.print = fn grid -> seq + (matrix.each fn row -> + console.log {(row.map fn cell -> + if cell then '*' else ' ').join ''}) + (console.log {}) + in + + let live_neighbors = fn i, j -> + let count = fn i, j -> + if (matrix.get i).get j then 1 else 0 + in + { + count (i - 1) (j - 1), + count (i - 1) j, + count (i - 1) (j + 1), + count i (j - 1), + count i (j + 1), + count (i + 1) (j - 1), + count (i + 1) j, + count (i + 1) (j + 1), + }.reduce 0 (+) + in + + let grid.next = fn grid -> + Grid ((range matrix.length).map fn i -> + (range matrix[i].length).map fn j -> + let live_count = live_neighbors i j in + if (matrix.get i).get j + then live_count = 2 or live_count = 3 + else live_count = 3) + in + + grid +in + +let loop = fix fn f, grid -> seq + (grid.print) + (f grid.next) +in + +let to_bool = fn matrix -> + matrix.map fn row -> + row.map fn cell -> cell > 0 +in + +let grid = Grid (to_bool { + {0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, + {1, 1, 1, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, +}) in + +loop grid From 9ec9a8678254c68b5d7c8f53a137f317ccbd280a Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Tue, 20 Sep 2016 23:06:22 +0100 Subject: [PATCH 32/35] Collatz's conjecture --- collatz.lambda | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 collatz.lambda diff --git a/collatz.lambda b/collatz.lambda new file mode 100644 index 0000000..6f5726a --- /dev/null +++ b/collatz.lambda @@ -0,0 +1,16 @@ +let f = fn n: natural -> + if n % 2 = 0 + then n / 2 + else 3 * n + 1 +in + +let a = fix fn a, n, i: natural -> + if i = 0 + then n + else f (a n (i - 1)) +in + +let a_n = a 19 in + +(range 21).each fn i -> + console.log {i.str + ': ' + (a_n i).str} From 7afa1e19f96c9369df4fdec7990a6d324985e2d3 Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Sat, 24 Sep 2016 17:20:04 +0100 Subject: [PATCH 33/35] ls.lambda --- ls.lambda | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 ls.lambda diff --git a/ls.lambda b/ls.lambda new file mode 100644 index 0000000..0a78414 --- /dev/null +++ b/ls.lambda @@ -0,0 +1,6 @@ +# run with -n switch + +let fs = require 'fs' in + +(fs.readdirSync {'.'}).each fn item -> + console.log {item} From 063f59d3e5442a719cd02a10623a0fd802dba764 Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Sat, 24 Sep 2016 21:03:41 +0100 Subject: [PATCH 34/35] Classes --- life4.lambda | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/life4.lambda b/life4.lambda index 7e227a6..dd0dce9 100644 --- a/life4.lambda +++ b/life4.lambda @@ -1,4 +1,4 @@ -let Grid = fix fn Grid, matrix -> +let Grid = fix fn Grid, this, matrix -> let mod = fn n, i -> (i % n + n) % n in let get = fn array, i -> array[mod array.length i] in @@ -7,7 +7,7 @@ let Grid = fix fn Grid, matrix -> let row.get = get in row, matrix.get = get in - let grid.print = fn grid -> seq + let this.print = fn this -> seq (matrix.each fn row -> console.log {(row.map fn cell -> if cell then '*' else ' ').join ''}) @@ -30,8 +30,8 @@ let Grid = fix fn Grid, matrix -> }.reduce 0 (+) in - let grid.next = fn grid -> - Grid ((range matrix.length).map fn i -> + let this.next = fn this -> + new Grid ((range matrix.length).map fn i -> (range matrix[i].length).map fn j -> let live_count = live_neighbors i j in if (matrix.get i).get j @@ -39,12 +39,12 @@ let Grid = fix fn Grid, matrix -> else live_count = 3) in - grid + this in -let loop = fix fn f, grid -> seq +let loop = fix fn loop, grid -> seq (grid.print) - (f grid.next) + (loop grid.next) in let to_bool = fn matrix -> @@ -52,7 +52,7 @@ let to_bool = fn matrix -> row.map fn cell -> cell > 0 in -let grid = Grid (to_bool { +let grid = new Grid (to_bool { {0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, {1, 1, 1, 0, 0, 0, 0, 0, 0, 0}, From bf00302a8edf36e4ec95ce985f1beded59137c35 Mon Sep 17 00:00:00 2001 From: Alberto La Rocca Date: Wed, 19 Oct 2016 10:34:17 +0100 Subject: [PATCH 35/35] box.lambda --- box.lambda | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 box.lambda diff --git a/box.lambda b/box.lambda new file mode 100644 index 0000000..039b5dd --- /dev/null +++ b/box.lambda @@ -0,0 +1,8 @@ +let Box = fix fn Box, this, s -> + let this.value = global.parseInt {s, 10} + , this.next = fn this -> + new Box (this.value + 1).str + in this +in + +(new Box '5').next.next.value