diff options
Diffstat (limited to 'std')
-rw-r--r-- | std/Box.bruijn | 2 | ||||
-rw-r--r-- | std/Char.bruijn | 3 | ||||
-rw-r--r-- | std/Combinator.bruijn | 2 | ||||
-rw-r--r-- | std/Float.bruijn | 4 | ||||
-rw-r--r-- | std/Number/Ternary.bruijn | 3 | ||||
-rw-r--r-- | std/Number/Unary.bruijn | 4 | ||||
-rw-r--r-- | std/Tree/Balanced.bruijn | 9 |
7 files changed, 16 insertions, 11 deletions
diff --git a/std/Box.bruijn b/std/Box.bruijn index 3e3cbfa..1bdac0a 100644 --- a/std/Box.bruijn +++ b/std/Box.bruijn @@ -29,7 +29,7 @@ store! [[empty? 1 (box 0) 1]] ⧗ (Box a) → a → (Box a) :test (store! empty [[1]]) (box [[1]]) # sets/overrides the value of a box -set! [[(box 0)]] ⧗ (Box a) → a → (Box a) +set! [[box 0]] ⧗ (Box a) → a → (Box a) :test (set! (box [[0]]) [[1]]) (box [[1]]) :test (set! empty [[1]]) (box [[1]]) diff --git a/std/Char.bruijn b/std/Char.bruijn index cdab0bf..a10f304 100644 --- a/std/Char.bruijn +++ b/std/Char.bruijn @@ -1,5 +1,6 @@ # MIT License, Copyright (c) 2023 Marvin Borner -:input std/Number/Binary . +:input std/Number/Binary +# converts a char to a balanced ternary number number! [ternary! (0 - '0')] ⧗ Char → Number diff --git a/std/Combinator.bruijn b/std/Combinator.bruijn index 9cafbce..667f0bb 100644 --- a/std/Combinator.bruijn +++ b/std/Combinator.bruijn @@ -120,8 +120,6 @@ o [[0 (1 0)]] ⧗ ((a → b) → a) → (a → b) → b # queer bird combinator: reverse function composition: (f , g) x = g (f x) q [[[1 (2 0)]]] ⧗ (a → b) → (b → c) → a → c -…,… q - # quixotic bird combinator q' [[[2 (0 1)]]] ⧗ (b → c) → a → (a → b) → c diff --git a/std/Float.bruijn b/std/Float.bruijn index 3d88d6e..d2ed080 100644 --- a/std/Float.bruijn +++ b/std/Float.bruijn @@ -2,10 +2,14 @@ # Arbitrary-precision floating-point arithmetic implementation using # (+3.14) = pair (+3) (+14) +# This is completely WIP and only intended as a proof of concept + :import std/Combinator . :import std/Number . :import std/Pair P +pi (+3) : (+14159) + # generates a float from a normal balanced ternary number float! \(P.…:…) (+0) diff --git a/std/Number/Ternary.bruijn b/std/Number/Ternary.bruijn index 9fb075e..f115349 100644 --- a/std/Number/Ternary.bruijn +++ b/std/Number/Ternary.bruijn @@ -327,12 +327,15 @@ compare-case [[[[[go (1 - 0)]]]]] ⧗ a → b → c → Number → Number → d go [=?0 5 (>?0 4 3)] # returns 1 if a>b, -1 if a<b and 0 if a=b +# also: spaceship operator compare compare-case (+0) (+1) (-1) ⧗ Number → Number → Number :test (compare (+2) (+2)) ((+0)) :test (compare (+2) (+1)) ((+1)) :test (compare (+1) (+2)) ((-1)) +…<=>… compare + # 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 edc9dcd..33127f4 100644 --- a/std/Number/Unary.bruijn +++ b/std/Number/Unary.bruijn @@ -86,7 +86,7 @@ les? \gre? ⧗ Unary → Unary → Boolean :test ((+2u) <? (+2u)) (false) :test ((+3u) <? (+2u)) (false) -# returns true if two balanced ternary numbers are equal +# returns true if two unary numbers are equal eq? [[(0 ≥? 1) â‹€? (0 ≤? 1)]] ⧗ Unary → Unary → Boolean …=?… eq? @@ -95,7 +95,7 @@ eq? [[(0 ≥? 1) â‹€? (0 ≤? 1)]] ⧗ Unary → Unary → Boolean :test ((+1u) =? (+1u)) (true) :test ((+42u) =? (+42u)) (true) -# returns true if two balanced ternary numbers are not equal +# returns true if two unary numbers are not equal not-eq? not! ∘∘ eq? ⧗ Unary → Unary → Boolean …≠?… not-eq? diff --git a/std/Tree/Balanced.bruijn b/std/Tree/Balanced.bruijn index ab0a921..39d5bc8 100644 --- a/std/Tree/Balanced.bruijn +++ b/std/Tree/Balanced.bruijn @@ -1,6 +1,7 @@ # MIT License, Copyright (c) 2023 Marvin Borner # Balanced AVL tree for numerical leafs, inspired by Rudy Matela's implementation # TODO: Extend for arbitrary orderable types? +# TODO: Analyze performance bottlenecks # TODO: More tests :import std/Combinator . @@ -10,12 +11,10 @@ :import std/Option . :import std/Pair . -# tree ⧗ (height : (lbranch? : (label : rbranch?))) - # error return that can't happen (in theory) error Ω -# unwrap tree from option (only use if not empty!) +# unwraps tree from option (only use if not empty!) unwrap unwrap-or error ⧗ (Option BalancedTree) → BalancedTree !‣ unwrap @@ -68,9 +67,9 @@ rotate-lr [rotate-ll (node (some (rotate-rr !(//0))) ?0 \\0)] ⧗ BalancedTree â rotate-rl [rotate-rr (node //0 ?0 (some (rotate-ll !(\\0))))] ⧗ BalancedTree → BalancedTree -# balance a tree +# balances a tree balance [go (factor 0)] ⧗ (Option BalancedTree) → (Option BalancedTree) - go [(0 >? (+1)) left ((0 <? (-1)) right else)] + go [=?0 else ((0 >? (+1)) left ((0 <? (-1)) right else))] left some (((factor //(!1)) =? (-1)) (rotate-lr !1) (rotate-ll !1)) right some (((factor \\(!1)) =? (+1)) (rotate-rl !1) (rotate-rr !1)) else 1 |