diff options
Diffstat (limited to 'samples')
-rw-r--r-- | samples/rosetta/de_bruijn_sequence.bruijn | 15 | ||||
-rw-r--r-- | samples/rosetta/halt_and_catch_fire.bruijn | 3 | ||||
-rw-r--r-- | samples/rosetta/higher_order_functions.bruijn | 7 | ||||
-rw-r--r-- | samples/rosetta/luhn_test_of_credit_card_numbers.bruijn | 14 | ||||
-rw-r--r-- | samples/rosetta/sorting_quicksort.bruijn | 4 | ||||
-rw-r--r-- | samples/rosetta/sum_and_product_of_an_array.bruijn | 6 | ||||
-rw-r--r-- | samples/rosetta/validate_isin.bruijn | 30 |
7 files changed, 77 insertions, 2 deletions
diff --git a/samples/rosetta/de_bruijn_sequence.bruijn b/samples/rosetta/de_bruijn_sequence.bruijn new file mode 100644 index 0000000..1012223 --- /dev/null +++ b/samples/rosetta/de_bruijn_sequence.bruijn @@ -0,0 +1,15 @@ +# TODO: Too slow to be published. Probably needs Maps/Sets/Vectors/sth. + +:import std/Combinator . +:import std/Char C +:import std/Math . +:import std/List . + +# very slow but elegant +de-bruijn y [[[C.?infix? (take 0 1) ~1 case-end case-rec]]] + case-rec max-by (compare ⋔ length) ([3 (0 : 2) 1] <$> (C.?nub 1)) + case-end drop 0 1 + +:test (de-bruijn "abcd" (+2)) ("dccaadbbacbdabcd") + +main [[0]] diff --git a/samples/rosetta/halt_and_catch_fire.bruijn b/samples/rosetta/halt_and_catch_fire.bruijn new file mode 100644 index 0000000..e053451 --- /dev/null +++ b/samples/rosetta/halt_and_catch_fire.bruijn @@ -0,0 +1,3 @@ +:test ([[0]]) ([[1]]) + +main [[0 0] [0 0]] diff --git a/samples/rosetta/higher_order_functions.bruijn b/samples/rosetta/higher_order_functions.bruijn new file mode 100644 index 0000000..952d08f --- /dev/null +++ b/samples/rosetta/higher_order_functions.bruijn @@ -0,0 +1,7 @@ +first [0 [[0]]] + +second [first [[1]]] + +:test (second) ([[[[0]]]]) + +main [[0]] diff --git a/samples/rosetta/luhn_test_of_credit_card_numbers.bruijn b/samples/rosetta/luhn_test_of_credit_card_numbers.bruijn new file mode 100644 index 0000000..e52011e --- /dev/null +++ b/samples/rosetta/luhn_test_of_credit_card_numbers.bruijn @@ -0,0 +1,14 @@ +:import std/Combinator . +:import std/Math . +:import std/List . + +luhn number→list → reverse → check → (\mod (+10)) → zero? + check y [[[[0 [[[6 \5 (4 + (5 odd even)) 1]]] 1]]]] k (+0) + odd 2 + even digit-sum (2 ⋅ (+2)) + +:test (luhn (+61789372994)) ([[1]]) +:test (luhn (+49927398716)) ([[1]]) +:test (luhn (+49927398717)) ([[0]]) +:test (luhn (+1234567812345678)) ([[0]]) +:test (luhn (+1234567812345670)) ([[1]]) diff --git a/samples/rosetta/sorting_quicksort.bruijn b/samples/rosetta/sorting_quicksort.bruijn index f55f128..0413024 100644 --- a/samples/rosetta/sorting_quicksort.bruijn +++ b/samples/rosetta/sorting_quicksort.bruijn @@ -4,8 +4,8 @@ sort y [[0 [[[case-sort]]] case-end]] case-sort (4 lesser) ++ (2 : (4 greater)) - lesser 1 <#> (\les? 2) - greater 1 <#> (\geq? 2) + lesser (\les? 2) <#> 1 + greater (\geq? 2) <#> 1 case-end empty :test (sort ((+3) : ((+2) : {}(+1)))) ((+1) : ((+2) : {}(+3))) diff --git a/samples/rosetta/sum_and_product_of_an_array.bruijn b/samples/rosetta/sum_and_product_of_an_array.bruijn new file mode 100644 index 0000000..70b146b --- /dev/null +++ b/samples/rosetta/sum_and_product_of_an_array.bruijn @@ -0,0 +1,6 @@ +:import std/List . +:import std/Math . + +arr (+1) : ((+2) : ((+3) : {}(+4))) + +main [∑arr : ∏arr] diff --git a/samples/rosetta/validate_isin.bruijn b/samples/rosetta/validate_isin.bruijn new file mode 100644 index 0000000..7608ed5 --- /dev/null +++ b/samples/rosetta/validate_isin.bruijn @@ -0,0 +1,30 @@ +:import luhn_test_of_credit_card_numbers . + +:import std/Number/Conversion . +:import std/Combinator . +:import std/String . +:import std/Char . +:import std/Logic . +:import std/Number . + +# verifies ISIN format +format? [len ⋀? country ⋀? security ⋀? checksum] + len (length 0) =? (+12) + country all? uppercase? (take (+2) 0) + security all? (φ or? uppercase? numeric?) (take (+9) (drop (+2) 0)) + checksum numeric? _0 + +# performs luhn test +checksum? (map (from-base36 → number→string)) → concat → string→number → luhn + from-base36 binary→ternary → [(0 - (0 ≥? (+65) ((+65) - (+10)) (+48)))] + +# performs format and checksum test +validate φ and? format? checksum? + +:test (validate "US0378331005") (true) +:test (validate "US0373831005") (false) +:test (validate "U50378331005") (false) +:test (validate "US03378331005") (false) +:test (validate "AU0000XVGZA3") (true) +:test (validate "AU0000VXGZA3") (true) +:test (validate "FR0000988040") (true) |