aboutsummaryrefslogtreecommitdiffhomepage
path: root/samples/rosetta/binary_search.bruijn
diff options
context:
space:
mode:
Diffstat (limited to 'samples/rosetta/binary_search.bruijn')
-rw-r--r--samples/rosetta/binary_search.bruijn20
1 files changed, 20 insertions, 0 deletions
diff --git a/samples/rosetta/binary_search.bruijn b/samples/rosetta/binary_search.bruijn
new file mode 100644
index 0000000..87f01ba
--- /dev/null
+++ b/samples/rosetta/binary_search.bruijn
@@ -0,0 +1,20 @@
+:import std/Combinator .
+:import std/Math .
+:import std/List .
+:import std/Option .
+
+binary-search [y [[[[[2 <? 3 none go]]]]] (+0) --(∀0) 0]
+ go [compare-case eq lt gt (2 !! 0) 1] /²(3 + 2)
+ eq some 0
+ lt 5 4 --0 2 1
+ gt 5 ++0 3 2 1
+
+# example using sorted list of x^3, x=[-50,50]
+find [[map-or "not found" [0 : (1 !! 0)] (binary-search 0 1)] lst]
+ lst take (+100) ((\pow (+3)) <$> (iterate ++‣ (-50)))
+
+:test (find (+100)) ("not found")
+:test ((head (find (+125))) =? (+55)) ([[1]])
+:test ((head (find (+117649))) =? (+99)) ([[1]])
+
+main [[0]]