Skip to content

Commit

Permalink
More samples
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinborner committed Mar 14, 2024
1 parent 004f853 commit 0b77800
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
12 changes: 12 additions & 0 deletions samples/rosetta/determine_sentence_type.bruijn
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
:import std/List .
:import std/Char .

determine [∅?0 '?' ([(0 =? '?' 'Q' (0 =? '.' 'S' (0 =? '!' 'E' 'N')))] _0)]

:test (determine empty) ('?')
:test (determine "hi there, how are you today?") ('Q')
:test (determine "I'd like to present to you the washing machine 9001.") ('S')
:test (determine "You have been nominated to win one of these!") ('E')
:test (determine "Just make sure you don't break it") ('N')

main [[0]]
8 changes: 8 additions & 0 deletions samples/rosetta/greatest_common_divisor.bruijn
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
:import std/Combinator .
:import std/Number .

gcd y [[[=?0 1 (2 0 (1 % 0))]]]

:test ((gcd (+2) (+4)) =? (+2)) ([[1]])
:test ((gcd (+10) (+5)) =? (+5)) ([[1]])
:test ((gcd (+3) (+8)) =? (+1)) ([[1]])
20 changes: 20 additions & 0 deletions samples/rosetta/mutual_recursion.bruijn
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
:import std/Combinator .
:import std/Number .
:import std/List .

f' [[[=?0 (+1) (0 - (1 (2 --0)))]]]

m' [[[=?0 (+0) (0 - (2 (1 --0)))]]]

f ^(y* (f' : {}m'))

m _(y* (f' : {}m'))

:test ((f (+0)) =? (+1)) ([[1]])
:test ((m (+0)) =? (+0)) ([[1]])
:test ((f (+4)) =? (+3)) ([[1]])
:test ((m (+4)) =? (+2)) ([[1]])
:test ((f (+15)) =? (+9)) ([[1]])
:test ((m (+15)) =? (+9)) ([[1]])

main [[0]]
7 changes: 5 additions & 2 deletions std/Math.bruijn
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,15 @@ product L.foldl mul (+1) ⧗ (List Number) → Number

:test (∏ (+1) → (+3) | ++‣) ((+24))

# greatest common divisor
gcd z [[[1 =? 0 case-eq (1 >? 0 case-gre case-les)]]] ⧗ Number → Number → Number
# greatest common divisor using repeated subtraction
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)

# greatest common divisor using modulo (mostly faster than gcd*)
gcd z [[[=?0 1 (2 0 (1 % 0))]]] ⧗ Number → Number → Number

:test ((gcd (+2) (+4)) =? (+2)) (true)
:test ((gcd (+10) (+5)) =? (+5)) (true)
:test ((gcd (+3) (+8)) =? (+1)) (true)
Expand Down

0 comments on commit 0b77800

Please sign in to comment.