aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--samples/rosetta/compare_a_list_of_strings.bruijn18
-rw-r--r--std/List.bruijn9
-rw-r--r--std/Math.bruijn42
-rw-r--r--std/Meta.bruijn2
-rw-r--r--std/Number/Binary.bruijn9
-rw-r--r--std/Number/Bruijn.bruijn2
-rw-r--r--std/Number/Ternary.bruijn15
-rw-r--r--std/Number/Unary.bruijn2
-rw-r--r--std/String.bruijn53
9 files changed, 123 insertions, 29 deletions
diff --git a/samples/rosetta/compare_a_list_of_strings.bruijn b/samples/rosetta/compare_a_list_of_strings.bruijn
new file mode 100644
index 0000000..636a98d
--- /dev/null
+++ b/samples/rosetta/compare_a_list_of_strings.bruijn
@@ -0,0 +1,18 @@
+:import std/String .
+
+all-eq? [land? (zip-with eq? 0 (tail 0))]
+
+all-gre? [land? (zip-with les? 0 (tail 0))]
+
+# --- tests ---
+
+list-a "abc" : ("abc" : {}("abc"))
+
+list-b "abc" : ("def" : {}("ghi"))
+
+:test (all-eq? list-a) ([[1]])
+:test (all-eq? list-b) ([[0]])
+:test (all-gre? list-a) ([[0]])
+:test (all-gre? list-b) ([[1]])
+
+main [[0]]
diff --git a/std/List.bruijn b/std/List.bruijn
index d355ea9..a240e3d 100644
--- a/std/List.bruijn
+++ b/std/List.bruijn
@@ -420,6 +420,15 @@ eq? ⋀?‣ ∘∘∘ zip-with ⧗ (a → a → Boolean) → (List a) → Boolea
:test (eq? …=?… ((+1) : {}(+2)) ((+2) : {}(+2))) (false)
:test (eq? …=?… empty empty) (true)
+# returns eq, lt, gt depending on comparison of two lists and comparison function
+compare-case z [[[[[[[rec]]]]]]] ⧗ (a → a → Boolean) → c → d → e → (List a) → (List a) → Boolean
+ rec ∅?1 (∅?0 4 3) (∅?0 2 go)
+ go [=?0 (7 6 5 4 3 ~2 ~1) 0] (5 ^1 ^0)
+
+# returns 1 if a>b, -1 if a<b and 0 if a=b
+# also: spaceship operator
+compare [compare-case 0 (+0) (+1) (-1)] ⧗ (a → a → Boolean) → Binary → Binary → Number
+
# returns true if list is prefix of other list
prefix? z [[[[rec]]]] ⧗ (a → a → Boolean) → (List a) → (List a) → Boolean
rec ∅?1 true (∅?0 false go)
diff --git a/std/Math.bruijn b/std/Math.bruijn
index 62d04e0..78e3006 100644
--- a/std/Math.bruijn
+++ b/std/Math.bruijn
@@ -3,14 +3,14 @@
:input std/Number
-:import std/List .
+:import std/List L
# adds all values in list
-sum foldl add (+0) ⧗ (List Number) → Number
+sum L.foldl add (+0) ⧗ (List Number) → Number
∑‣ sum
-:test (∑((+1) : ((+2) : {}(+3)))) ((+6))
+:test (∑((+1) : ((+2) : L.{}(+3)))) ((+6))
# digit sum of all values
digit-sum sum ∘ number→list ⧗ Number → Number
@@ -20,48 +20,48 @@ digit-sum sum ∘ number→list ⧗ Number → Number
:test ((digit-sum (+19)) =? (+10)) (true)
# returns max value of list
-lmax foldl1 max ⧗ (List Number) → Number
+lmax L.foldl1 max ⧗ (List Number) → Number
-:test (lmax ((+1) : ((+3) : {}(+2)))) ((+3))
+:test (lmax ((+1) : ((+3) : L.{}(+2)))) ((+3))
# returns min value of list
-lmin foldl1 min ⧗ (List Number) → Number
+lmin L.foldl1 min ⧗ (List Number) → Number
-:test (lmin ((+2) : ((+1) : {}(+0)))) ((+0))
+:test (lmin ((+2) : ((+1) : L.{}(+0)))) ((+0))
# list from num to num
{…→…} z [[[rec]]] ⧗ Number → Number → (List Number)
- rec (1 =? ++0) case-end case-list
+ rec 1 =? ++0 case-end case-list
case-list 1 : (2 ++1 0)
- case-end empty
+ case-end L.empty
-:test ({ (+0) → (+2) }) ((+0) : ((+1) : {}(+2)))
+:test ({ (+0) → (+2) }) ((+0) : ((+1) : L.{}(+2)))
# equivalent of mathematical sum function
∑…→…|… z [[[[[rec]]]]] (+0) ⧗ Number → Number → (Number → Number) → Number
- rec (2 =? ++1) case-end case-sum
+ rec 2 =? ++1 case-end case-sum
case-sum 4 (3 + (0 2)) ++2 1 0
case-end 3
:test (∑ (+1) → (+3) | ++‣) ((+9))
# multiplies all values in list
-product foldl mul (+1) ⧗ (List Number) → Number
+product L.foldl mul (+1) ⧗ (List Number) → Number
∏‣ product
-:test (∏((+1) : ((+2) : {}(+3)))) ((+6))
+:test (∏((+1) : ((+2) : L.{}(+3)))) ((+6))
# equivalent of mathematical product function
∏…→…|… z [[[[[rec]]]]] (+1) ⧗ Number → Number → (Number → Number) → Number
- rec (2 =? ++1) case-end case-sum
+ rec 2 =? ++1 case-end case-sum
case-sum 4 (3 ⋅ (0 2)) ++2 1 0
case-end 3
:test (∏ (+1) → (+3) | ++‣) ((+24))
# greatest common divisor
-gcd z [[[(1 =? 0) case-eq ((1 >? 0) case-gre case-les)]]] ⧗ Number → Number → Number
+gcd z [[[1 =? 0 case-eq (1 >? 0 case-gre case-les)]]] ⧗ Number → Number → Number
case-eq 1
case-gre 2 (1 - 0) 0
case-les 2 1 (0 - 1)
@@ -136,7 +136,7 @@ powertower z [[[rec]]] ⧗ Number → Number → Number
arrow z [[[[rec]]]] ⧗ Number → Number → Number → Number
rec =?2 case-end case-rec
case-end 1 ⋅ 0
- case-rec foldr (3 --2) 1 (replicate --0 1)
+ case-rec L.foldr (3 --2) 1 (L.replicate --0 1)
:test ((arrow (+1) (+1) (+1)) =? (+1)) (true)
:test ((arrow (+1) (+2) (+4)) =? (+16)) (true)
@@ -144,9 +144,9 @@ arrow z [[[[rec]]]] ⧗ Number → Number → Number → Number
# fibonacci sequence
# TODO: faster fib?
-fibs head <$> (iterate &[[0 : (1 + 0)]] ((+0) : (+1))) ⧗ (List Number)
+fibs L.map L.head (L.iterate &[[0 : (1 + 0)]] ((+0) : (+1))) ⧗ (List Number)
-fib [fibs !! ++0] ⧗ Number
+fib [L.index ++0 fibs] ⧗ Number
:test (fib (+5)) ((+8))
@@ -194,7 +194,7 @@ log* [z [[rec]] --0] ⧗ Number → Number
# pascal triangle
# TODO: something is wrong in here
-pascal iterate [zip-with …+… ({}(+0) ++ 0) (0 ; (+0))] ({}(+1))
+pascal L.iterate [L.zip-with …+… (L.{}(+0) ++ 0) (0 ; (+0))] (L.{}(+1))
# characteristic prime sequence by Tromp
characteristic-primes ki : (ki : (sieve s0)) ⧗ (List Bool)
@@ -204,10 +204,10 @@ characteristic-primes ki : (ki : (sieve s0)) ⧗ (List Bool)
s0 [[[ki : (0 2)]]]
# prime number sequence
-primes map fst (filter snd (zip (iterate ++‣ (+0)) characteristic-primes)) ⧗ (List Number)
+primes L.map fst (L.filter snd (L.zip (L.iterate ++‣ (+0)) characteristic-primes)) ⧗ (List Number)
# slower but cooler prime number sequence
-primes* nub ((…≠?… (+1)) ∘∘ gcd) (iterate ++‣ (+2)) ⧗ (List Number)
+primes* L.nub ((…≠?… (+1)) ∘∘ gcd) (L.iterate ++‣ (+2)) ⧗ (List Number)
# π as a list of decimal digits
# translation of unbounded spigot algorithm by Jeremy Gibbons
diff --git a/std/Meta.bruijn b/std/Meta.bruijn
index e9ddea0..bf55b86 100644
--- a/std/Meta.bruijn
+++ b/std/Meta.bruijn
@@ -3,8 +3,8 @@
:import std/Logic .
:import std/Combinator .
-:import std/Number/Unary .
:import std/List .
+:import std/Number/Unary .
:import std/Number/Pairing .
:import std/Number/Conversion .
:import std/Pair P
diff --git a/std/Number/Binary.bruijn b/std/Number/Binary.bruijn
index 77ce09c..064889d 100644
--- a/std/Number/Binary.bruijn
+++ b/std/Number/Binary.bruijn
@@ -359,7 +359,7 @@ geq? \leq? ⧗ Binary → Binary → Boolean
:test ((+2b) ≥? (+2b)) (true)
:test ((+3b) ≥? (+2b)) (true)
-# returns eq, lt, gt depending on comparison of two numbers
+# returns eq, gt, lt depending on comparison of two numbers
# TODO: remove ternary conversion
compare-case [[[(T.compare-case 2 1 0) ⋔ binary→ternary]]] ⧗ a → b → c → Binary → Binary → d
@@ -367,12 +367,15 @@ compare-case [[[(T.compare-case 2 1 0) ⋔ binary→ternary]]] ⧗ a → b → c
# also: spaceship operator
compare compare-case (+0) (+1) (-1) ⧗ Binary → Binary → Number
+…<=>… compare
+
+<=>‣ &compare
+
:test (compare (+2b) (+2b)) ((+0))
:test (compare (+2b) (+1b)) ((+1))
:test (compare (+1b) (+2b)) ((-1))
-…<=>… compare
-
+# prefix for comparing functions
# returns max number of two
max [[(1 ≤? 0) 0 1]] ⧗ Binary → Binary → Binary
diff --git a/std/Number/Bruijn.bruijn b/std/Number/Bruijn.bruijn
index 744d9a1..131a1a3 100644
--- a/std/Number/Bruijn.bruijn
+++ b/std/Number/Bruijn.bruijn
@@ -4,8 +4,6 @@
# Wadsworth, Christopher. "Some unusual λ-calculus numeral systems."
# very sad indeed
-:import std/Combinator .
-
# increments De Bruijn numeral
inc [[[2 1]]]
diff --git a/std/Number/Ternary.bruijn b/std/Number/Ternary.bruijn
index 8cf891f..5e93c66 100644
--- a/std/Number/Ternary.bruijn
+++ b/std/Number/Ternary.bruijn
@@ -348,7 +348,7 @@ geq? \leq? ⧗ Number → Number → Boolean
:test ((+2) ≥? (+2)) (true)
:test ((+3) ≥? (+2)) (true)
-# returns eq, lt, gt depending on comparison of two numbers
+# returns eq, gt, lt depending on comparison of two numbers
compare-case [[[[[go (1 - 0)]]]]] ⧗ a → b → c → Number → Number → d
go [=?0 5 (>?0 4 3)]
@@ -356,11 +356,22 @@ compare-case [[[[[go (1 - 0)]]]]] ⧗ a → b → c → Number → Number → d
# also: spaceship operator
compare compare-case (+0) (+1) (-1) ⧗ Number → Number → Number
+…<=>… compare
+
+<=>‣ &compare
+
:test (compare (+2) (+2)) ((+0))
:test (compare (+2) (+1)) ((+1))
:test (compare (+1) (+2)) ((-1))
-…<=>… compare
+# returns true if comparison result is equal (EQ)
+c-eq? eq? (+0) ⧗ Number → Number
+
+# returns true if comparison result is less than (LT)
+c-les? eq? (-1) ⧗ Number → Number
+
+# returns true if comparison result is greater than (GT)
+c-gre? eq? (+1) ⧗ Number → Number
# negates a balanced ternary number if <0
abs [<?0 -0 0] ⧗ Number → Number
diff --git a/std/Number/Unary.bruijn b/std/Number/Unary.bruijn
index 81af007..5041614 100644
--- a/std/Number/Unary.bruijn
+++ b/std/Number/Unary.bruijn
@@ -136,6 +136,8 @@ compare compare-case (+0) (+1) (-1) ⧗ Unary → Unary → Number
…<=>… compare
+<=>‣ &compare
+
# muls two unary numbers
mul …∘… ⧗ Unary → Unary → Unary
diff --git a/std/String.bruijn b/std/String.bruijn
index 645eda3..e1f4f4b 100644
--- a/std/String.bruijn
+++ b/std/String.bruijn
@@ -17,6 +17,59 @@ eq? eq? B.eq? ⧗ String → String → Boolean
# prefix for comparing functions
?‣ &eq?
+# returns eq, gt, lt depending on comparison of two numbers
+compare-case B.<=>compare-case ⧗ a → b → c → String → String → d
+
+# returns 1 if a>b, -1 if a<b and 0 if a=b
+# also: spaceship operator
+compare compare-case (+0) (+1) (-1) ⧗ String → String → Number
+
+…<=>… compare
+
+<=>‣ &compare
+
+:test (compare "2" "2") ((+0))
+:test (compare "2" "1") ((+1))
+:test (compare "1" "2") ((-1))
+:test (compare "12" "1") ((-1))
+:test (compare "1" "12") ((+1))
+
+# returns true if string is lexically less than other string
+les? c-les? ∘∘ compare ⧗ String → String → Boolean
+
+…<?… les?
+
+:test ("1" <? "2") (true)
+:test ("2" <? "2") (false)
+:test ("3" <? "2") (false)
+
+# returns true if string is lexically greater than other string
+gre? \les? ⧗ String → String → Boolean
+
+…>?… gre?
+
+:test ("1" >? "2") (false)
+:test ("2" >? "2") (false)
+:test ("3" >? "2") (true)
+
+# returns true if string is lexically less than or equal to other string
+leq? not! ∘∘ gre? ⧗ String → String → Boolean
+
+…≤?… leq?
+
+:test ("1" ≤? "2") (true)
+:test ("2" ≤? "2") (true)
+:test ("3" ≤? "2") (false)
+
+# returns true if number is greater than or equal to other string
+geq? \leq? ⧗ String → String → Boolean
+
+…≥?… geq?
+
+:test ("1" ≥? "2") (false)
+:test ("2" ≥? "2") (true)
+:test ("3" ≥? "2") (true)
+
# returns true if character is part of a string
in? B.?in? ⧗ Char → String → Boolean