aboutsummaryrefslogtreecommitdiffhomepage
path: root/std/Number.bruijn
blob: a99bc19cdb910093f9946519f7659b2e3e9a0d9a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# MIT License, Copyright (c) 2023 Marvin Borner
# this is just a reference to the ternary implementation
# read the wiki 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 number to list of its digits
number→list [=?0 {}(+0) (z [[[rec]]] empty 0)] ⧗ Number → (List Number)
	rec =?0 case-end case-rec
		case-rec &[[4 (0 : 3) 1]] (quot-rem 0 (+10))
		case-end 1

:test (number→list (+0)) ({}(+0))

# converts a list of digits into a balanced ternary number
list→number foldl [[(+10) ⋅ 1 + 0]] (+0) ⧗ (List Number) → Number

:test (list→number ((+4) : ((+2) : {}(+0)))) ((+420))
:test (list→number empty) ((+0))