aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMarvin Borner2023-02-24 16:52:27 +0100
committerMarvin Borner2023-02-24 16:53:38 +0100
commit4d482c44df180625a64e34019abf7796399038b0 (patch)
treea27f9d26f28d5ca843993e6f19891c28f6dcf0f4
parentc6b97c4c0b8624f13034e7160b7992f94dd37cc0 (diff)
Added more string/char functions
-rw-r--r--bruijn.cabal2
-rw-r--r--readme.md2
-rw-r--r--std/Char.bruijn5
-rw-r--r--std/List.bruijn2
-rw-r--r--std/Number.bruijn11
-rw-r--r--std/Number/Binary.bruijn15
-rw-r--r--std/String.bruijn7
7 files changed, 40 insertions, 4 deletions
diff --git a/bruijn.cabal b/bruijn.cabal
index 81655ab..183f8fd 100644
--- a/bruijn.cabal
+++ b/bruijn.cabal
@@ -18,8 +18,8 @@ extra-source-files:
readme.md
data-files:
config
+ std/Char.bruijn
std/Combinator.bruijn
- std/Float.bruijn
std/List.bruijn
std/Logic.bruijn
std/Math.bruijn
diff --git a/readme.md b/readme.md
index 332b75f..d94d993 100644
--- a/readme.md
+++ b/readme.md
@@ -140,6 +140,8 @@ The script uses the dependencies `rg`, `jq`, `sed` and `awk`.
### Examples
+You can find more “real world” examples here: [samples](/samples).
+
You can try these by experimenting in the REPL or by running them as a
file. You should pipe something into the stdin to receive stdout:
`cat /dev/null | bruijn test.bruijn` should work for now.
diff --git a/std/Char.bruijn b/std/Char.bruijn
new file mode 100644
index 0000000..8351142
--- /dev/null
+++ b/std/Char.bruijn
@@ -0,0 +1,5 @@
+# MIT License, Copyright (c) 2023 Marvin Borner
+
+:import std/Number/Binary .
+
+number! [ternary! (0 - '0')] ⧗ Char → Number
diff --git a/std/List.bruijn b/std/List.bruijn
index 986b6bc..4758660 100644
--- a/std/List.bruijn
+++ b/std/List.bruijn
@@ -4,7 +4,7 @@
:import std/Combinator .
:import std/Pair P
:import std/Logic .
-:import std/Number .
+:import std/Number/Ternary .
# empty list element
empty false ⧗ (List a)
diff --git a/std/Number.bruijn b/std/Number.bruijn
index e17d7d6..daefbb7 100644
--- a/std/Number.bruijn
+++ b/std/Number.bruijn
@@ -2,4 +2,15 @@
# this is just a reference to the ternary implementation
# read the readme for the reasoning of using balanced ternary by default
+:import std/List .
+
:input std/Number/Ternary .
+
+# the following functions are only here because of recursive imports of list/ternary
+
+# converts a list of digits into a balanced ternary number
+from-digits foldl [[(+10) ⋅ 1 + 0]] (+0)
+
+:test (from-digits ((+4) : ((+2) : ((+0) : empty)))) ((+420))
+:test (from-digits empty) ((+0))
+
diff --git a/std/Number/Binary.bruijn b/std/Number/Binary.bruijn
index ec62e60..2d36c5f 100644
--- a/std/Number/Binary.bruijn
+++ b/std/Number/Binary.bruijn
@@ -4,6 +4,7 @@
:import std/Combinator .
:import std/List .
:import std/Logic .
+:import std/Number/Ternary T
# bit indicating a one, compatible with std/Logic
b¹ true ⧗ Bit
@@ -180,6 +181,16 @@ dec [~(0 z a¹ a⁰)] ⧗ Binary → Binary
:test (--(+1b)) ([[[0 2]]])
:test (--(+3b)) ((+2b))
+# converts a binary number to a balanced ternary number
+# TODO: find a better solution
+ternary! z [[[rec]]] (+0t) ⧗ Binary → Ternary
+ rec =?0 case-end case-inc
+ case-inc 2 (T.inc 1) --0
+ case-end 1
+
+:test (ternary! (+0b)) ((+0t))
+:test (ternary! (+42b)) ((+42t))
+
# flips the bits of a binary number (1's complement)
complement [[[[3 2 0 1]]]] ⧗ Binary → Binary
@@ -242,8 +253,8 @@ sub* [[abs 1 →^0]] ⧗ Binary → Binary → Binary
# subs two binary numbers
# uses addition but with two's complement
# TODO: isn't very performant ⇒ replace with sub*
-# TODO: gives wrong results if b>=a in a-b
-sub [[-((pad 1 0) + -(pad 0 1))]] ⧗ Binary → Binary → Binary
+# TODO: gives wrong results if b>a in a-b
+sub [[(0 =? 1) (+0b) -((pad 1 0) + -(pad 0 1))]] ⧗ Binary → Binary → Binary
…-… sub
diff --git a/std/String.bruijn b/std/String.bruijn
index db526fe..9267f20 100644
--- a/std/String.bruijn
+++ b/std/String.bruijn
@@ -1,5 +1,7 @@
# MIT License, Copyright (c) 2022 Marvin Borner
+:import std/Char C
+:import std/Number .
:import std/Number/Binary B
:input std/List
@@ -26,6 +28,11 @@ ni? \in?
:test ("ab" ∋ 'b') (true)
:test ("ab" ∋ 'c') (false)
+# converts a string of digits into a number
+number! from-digits ∘ (map C.number!)
+
+:test ((number! "123") =? (+123)) (true)
+
# splits string by newline character
lines z [[rec]]
rec ∅?(~broken) (^broken : empty) (^broken : (1 ~(~broken)))