diff options
author | Marvin Borner | 2024-02-29 11:37:56 +0100 |
---|---|---|
committer | Marvin Borner | 2024-02-29 11:37:56 +0100 |
commit | 1f36d2506288f2039707b843f203bfa19176a1e3 (patch) | |
tree | 4caa82317b6c0e2397a40ffc7f893bf82f152b7d /std/Number | |
parent | 9af35d2f796d5952fc290b22b057614a0e9db9ad (diff) |
Encourage use of combinators
Diffstat (limited to 'std/Number')
-rw-r--r-- | std/Number/Binary.bruijn | 16 | ||||
-rw-r--r-- | std/Number/Ternary.bruijn | 24 | ||||
-rw-r--r-- | std/Number/Unary.bruijn | 22 |
3 files changed, 31 insertions, 31 deletions
diff --git a/std/Number/Binary.bruijn b/std/Number/Binary.bruijn index 6445d9f..fac9ba1 100644 --- a/std/Number/Binary.bruijn +++ b/std/Number/Binary.bruijn @@ -59,8 +59,8 @@ binary! foldr up (+0b) ⧗ (List Bit) → Binary # strips leading 0s from a binary number strip [^(0 z a¹ a⁰)] ⧗ Binary → Binary z (+0b) : true - a¹ [0 [[↑¹1 : false]]] - a⁰ [0 [[(0 (+0b) ↑⁰1) : 0]]] + a¹ &[[↑¹1 : false]] + a⁰ &[[(0 (+0b) ↑⁰1) : 0]] %‣ strip @@ -162,8 +162,8 @@ not-eq? not! ∘∘ eq? ⧗ Binary → Binary → Boolean # adds 1 to a binary number (can introduce leading 0s) inc [~(0 z a¹ a⁰)] ⧗ Binary → Binary z (+0b) : (+1b) - a¹ [0 [[↑¹1 : ↑⁰0]]] - a⁰ [0 [[↑⁰1 : ↑¹1]]] + a¹ &[[↑¹1 : ↑⁰0]] + a⁰ &[[↑⁰1 : ↑¹1]] ++‣ inc @@ -173,8 +173,8 @@ inc [~(0 z a¹ a⁰)] ⧗ Binary → Binary # subs 1 from a binary number (can introduce leading 0s) dec [~(0 z a¹ a⁰)] ⧗ Binary → Binary z (+0b) : (+0b) - a¹ [0 [[↑¹1 : ↑⁰1]]] - a⁰ [0 [[↑⁰1 : ↑¹0]]] + a¹ &[[↑¹1 : ↑⁰1]] + a⁰ &[[↑⁰1 : ↑¹0]] --‣ dec @@ -268,8 +268,8 @@ sub [[(0 =? 1) (+0b) -((pad 1 0) + -(pad 0 1))]] ⧗ Binary → Binary → Binar # rshifts least significant bit of a binary number div² [~(0 z a¹ a⁰)] ⧗ Binary → Binary z (+0b) : (+0b) - a¹ [0 [[↑¹1 : 1]]] - a⁰ [0 [[↑⁰1 : 1]]] + a¹ &[[↑¹1 : 1]] + a⁰ &[[↑⁰1 : 1]] /²‣ div² diff --git a/std/Number/Ternary.bruijn b/std/Number/Ternary.bruijn index e1b3893..f63d9ac 100644 --- a/std/Number/Ternary.bruijn +++ b/std/Number/Ternary.bruijn @@ -90,9 +90,9 @@ list! [0 z a⁻ a⁺ a⁰] ⧗ Number → List # strips leading 0s from a balanced ternary number strip [^(0 z a⁻ a⁺ a⁰)] ⧗ Number → Number z (+0) : true - a⁻ [0 [[↑⁻1 : false]]] - a⁺ [0 [[↑⁺1 : false]]] - a⁰ [0 [[(0 (+0) ↑⁰1) : 0]]] + a⁻ &[[↑⁻1 : false]] + a⁺ &[[↑⁺1 : false]] + a⁰ &[[(0 (+0) ↑⁰1) : 0]] %‣ strip @@ -218,9 +218,9 @@ not-eq? not! ∘∘ eq? ⧗ Number → Number → Boolean # adds (+1) to a balanced ternary number (can introduce leading 0s) inc [~(0 z a⁻ a⁺ a⁰)] ⧗ Number → Number z (+0) : (+1) - a⁻ [0 [[↑⁻1 : ↑⁰1]]] - a⁺ [0 [[↑⁺1 : ↑⁻0]]] - a⁰ [0 [[↑⁰1 : ↑⁺1]]] + a⁻ &[[↑⁻1 : ↑⁰1]] + a⁺ &[[↑⁺1 : ↑⁻0]] + a⁰ &[[↑⁰1 : ↑⁺1]] ++‣ inc @@ -233,9 +233,9 @@ inc [~(0 z a⁻ a⁺ a⁰)] ⧗ Number → Number # subs (+1) from a balanced ternary number (can introduce leading 0s) dec [~(0 z a⁻ a⁺ a⁰)] ⧗ Number → Number z (+0) : (-1) - a⁻ [0 [[↑⁻1 : ↑⁺0]]] - a⁺ [0 [[↑⁺1 : ↑⁰1]]] - a⁰ [0 [[↑⁰1 : ↑⁻1]]] + a⁻ &[[↑⁻1 : ↑⁺0]] + a⁺ &[[↑⁺1 : ↑⁰1]] + a⁰ &[[↑⁰1 : ↑⁻1]] --‣ dec @@ -366,9 +366,9 @@ mul [[1 z a⁻ a⁺ a⁰]] ⧗ Number → Number → Number # WARNING: Not necessarily equivalent to (/ (+3)): e.g. /³(+5) == (+2)! div³ [~(0 z a⁻ a⁺ a⁰)] ⧗ Number → Number z (+0) : (+0) - a⁻ [0 [[↑⁻1 : 1]]] - a⁺ [0 [[↑⁺1 : 1]]] - a⁰ [0 [[↑⁰1 : 1]]] + a⁻ &[[↑⁻1 : 1]] + a⁺ &[[↑⁺1 : 1]] + a⁰ &[[↑⁰1 : 1]] /³‣ div³ diff --git a/std/Number/Unary.bruijn b/std/Number/Unary.bruijn index 6ace440..0c65c41 100644 --- a/std/Number/Unary.bruijn +++ b/std/Number/Unary.bruijn @@ -9,9 +9,9 @@ # id for church numerals # generic base for dec/fib/fac/etc. uid [[[extract (2 inc init)]]] ⧗ Unary → Unary - extract [0 [0]] - inc [[0 (1 3)]] - init [0 1] + extract &i + inc [&(0 2)] + init &0 :test (uid (+0u)) ((+0u)) :test (uid (+1u)) ((+1u)) @@ -36,8 +36,8 @@ inc [[[1 (2 1 0)]]] ⧗ Unary → Unary # subs 1 from a unary number dec [[[extract (2 inc const)]]] ⧗ Unary → Unary - extract [0 [0]] - inc [[0 (1 3)]] + extract &i + inc [&(0 2)] const [1] --‣ dec @@ -142,7 +142,7 @@ mul …∘… ⧗ Unary → Unary → Unary :test ((+2u) ⋅ (+3u)) ((+6u)) # divs two unary numbers -div [[[[3 [[0 1]] [1] (3 [3 [[0 1]] [3 (0 1)] [0]] 0)]]]] ⧗ Unary → Unary → Unary +div [[[[3 t [1] (3 [3 t [3 (0 1)] i] 0)]]]] ⧗ Unary → Unary → Unary …/… div @@ -156,7 +156,7 @@ div* [z rec ++0] ⧗ Unary → Unary → Unary rec [[[[[[=?0 ((+0u) 2 1) (2 (5 0 3 2 1))] (3 - 2)]]]]] # returns remainder of integer division -mod [[[[3 [0 [[1]]] (3 [3 [[[0 (2 (5 1)) 1]]] [1] 1] [1]) [[0]]]]]] ⧗ Unary → Unary → Unary +mod [[[[3 &k (3 [3 [[[0 (2 (5 1)) 1]]] [1] 1] [1]) ki]]]] ⧗ Unary → Unary → Unary …%… mod @@ -164,7 +164,7 @@ mod [[[[3 [0 [[1]]] (3 [3 [[[0 (2 (5 1)) 1]]] [1] 1] [1]) [[0]]]]]] ⧗ Unary � :test ((+3u) % (+5u)) ((+3u)) # slower mod (more obvious impl) -mod* [[1 [0 [[(0 ⋀? (3 ≤? 1)) case-rec case-end]]] (1 : true) [[1]]]] ⧗ Unary → Unary → Unary +mod* [[1 &[[(0 ⋀? (2 ≤? 1)) case-rec case-end]] (1 : true) k]] ⧗ Unary → Unary → Unary case-rec (1 - 3) : true case-end 1 : false @@ -173,7 +173,7 @@ mod* [[1 [0 [[(0 ⋀? (3 ≤? 1)) case-rec case-end]]] (1 : true) [[1]]]] ⧗ Un pow* [[1 0]] ⧗ Unary → Unary → Unary # exponentiates two unary numbers -pow [[0 [[3 (1 0)]] [[1 0]]]] ⧗ Unary → Unary → Unary +pow [[0 [[3 (1 0)]] pow*]] ⧗ Unary → Unary → Unary …^… pow @@ -187,11 +187,11 @@ fib [0 [[[2 0 [2 (1 0)]]]] [[1]] [0]] ⧗ Unary → Unary :test (fib (+6u)) ((+8u)) # factorial function -fac [[1 [[0 (1 [[2 1 (1 0)]])]] [1] [0]]] ⧗ Unary → Unary +fac [[1 [[0 (1 [[2 1 (1 0)]])]] [1] i]] ⧗ Unary → Unary :test (fac (+3u)) ((+6u)) # hyperfactorial function -hyperfac [[1 [[(0 0) (1 [[2 1 (1 0)]])]] [1] [0]]] ⧗ Unary → Unary +hyperfac [[1 [[(0 0) (1 [[2 1 (1 0)]])]] [1] i]] ⧗ Unary → Unary :test (hyperfac (+3u)) ((+108u)) |