diff options
Diffstat (limited to 'std/Math.bruijn')
-rw-r--r-- | std/Math.bruijn | 42 |
1 files changed, 21 insertions, 21 deletions
diff --git a/std/Math.bruijn b/std/Math.bruijn index 62d04e0..78e3006 100644 --- a/std/Math.bruijn +++ b/std/Math.bruijn @@ -3,14 +3,14 @@ :input std/Number -:import std/List . +:import std/List L # adds all values in list -sum foldl add (+0) ⧗ (List Number) → Number +sum L.foldl add (+0) ⧗ (List Number) → Number ∑‣ sum -:test (∑((+1) : ((+2) : {}(+3)))) ((+6)) +:test (∑((+1) : ((+2) : L.{}(+3)))) ((+6)) # digit sum of all values digit-sum sum ∘ number→list ⧗ Number → Number @@ -20,48 +20,48 @@ digit-sum sum ∘ number→list ⧗ Number → Number :test ((digit-sum (+19)) =? (+10)) (true) # returns max value of list -lmax foldl1 max ⧗ (List Number) → Number +lmax L.foldl1 max ⧗ (List Number) → Number -:test (lmax ((+1) : ((+3) : {}(+2)))) ((+3)) +:test (lmax ((+1) : ((+3) : L.{}(+2)))) ((+3)) # returns min value of list -lmin foldl1 min ⧗ (List Number) → Number +lmin L.foldl1 min ⧗ (List Number) → Number -:test (lmin ((+2) : ((+1) : {}(+0)))) ((+0)) +:test (lmin ((+2) : ((+1) : L.{}(+0)))) ((+0)) # list from num to num {…→…} z [[[rec]]] ⧗ Number → Number → (List Number) - rec (1 =? ++0) case-end case-list + rec 1 =? ++0 case-end case-list case-list 1 : (2 ++1 0) - case-end empty + case-end L.empty -:test ({ (+0) → (+2) }) ((+0) : ((+1) : {}(+2))) +:test ({ (+0) → (+2) }) ((+0) : ((+1) : L.{}(+2))) # equivalent of mathematical sum function ∑…→…|… z [[[[[rec]]]]] (+0) ⧗ Number → Number → (Number → Number) → Number - rec (2 =? ++1) case-end case-sum + rec 2 =? ++1 case-end case-sum case-sum 4 (3 + (0 2)) ++2 1 0 case-end 3 :test (∑ (+1) → (+3) | ++‣) ((+9)) # multiplies all values in list -product foldl mul (+1) ⧗ (List Number) → Number +product L.foldl mul (+1) ⧗ (List Number) → Number ∏‣ product -:test (∏((+1) : ((+2) : {}(+3)))) ((+6)) +:test (∏((+1) : ((+2) : L.{}(+3)))) ((+6)) # equivalent of mathematical product function ∏…→…|… z [[[[[rec]]]]] (+1) ⧗ Number → Number → (Number → Number) → Number - rec (2 =? ++1) case-end case-sum + rec 2 =? ++1 case-end case-sum case-sum 4 (3 ⋅ (0 2)) ++2 1 0 case-end 3 :test (∏ (+1) → (+3) | ++‣) ((+24)) # greatest common divisor -gcd z [[[(1 =? 0) case-eq ((1 >? 0) case-gre case-les)]]] ⧗ Number → Number → Number +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) @@ -136,7 +136,7 @@ powertower z [[[rec]]] ⧗ Number → Number → Number arrow z [[[[rec]]]] ⧗ Number → Number → Number → Number rec =?2 case-end case-rec case-end 1 ⋅ 0 - case-rec foldr (3 --2) 1 (replicate --0 1) + case-rec L.foldr (3 --2) 1 (L.replicate --0 1) :test ((arrow (+1) (+1) (+1)) =? (+1)) (true) :test ((arrow (+1) (+2) (+4)) =? (+16)) (true) @@ -144,9 +144,9 @@ arrow z [[[[rec]]]] ⧗ Number → Number → Number → Number # fibonacci sequence # TODO: faster fib? -fibs head <$> (iterate &[[0 : (1 + 0)]] ((+0) : (+1))) ⧗ (List Number) +fibs L.map L.head (L.iterate &[[0 : (1 + 0)]] ((+0) : (+1))) ⧗ (List Number) -fib [fibs !! ++0] ⧗ Number +fib [L.index ++0 fibs] ⧗ Number :test (fib (+5)) ((+8)) @@ -194,7 +194,7 @@ log* [z [[rec]] --0] ⧗ Number → Number # pascal triangle # TODO: something is wrong in here -pascal iterate [zip-with …+… ({}(+0) ++ 0) (0 ; (+0))] ({}(+1)) +pascal L.iterate [L.zip-with …+… (L.{}(+0) ++ 0) (0 ; (+0))] (L.{}(+1)) # characteristic prime sequence by Tromp characteristic-primes ki : (ki : (sieve s0)) ⧗ (List Bool) @@ -204,10 +204,10 @@ characteristic-primes ki : (ki : (sieve s0)) ⧗ (List Bool) s0 [[[ki : (0 2)]]] # prime number sequence -primes map fst (filter snd (zip (iterate ++‣ (+0)) characteristic-primes)) ⧗ (List Number) +primes L.map fst (L.filter snd (L.zip (L.iterate ++‣ (+0)) characteristic-primes)) ⧗ (List Number) # slower but cooler prime number sequence -primes* nub ((…≠?… (+1)) ∘∘ gcd) (iterate ++‣ (+2)) ⧗ (List Number) +primes* L.nub ((…≠?… (+1)) ∘∘ gcd) (L.iterate ++‣ (+2)) ⧗ (List Number) # π as a list of decimal digits # translation of unbounded spigot algorithm by Jeremy Gibbons |