Number.bruijn


# 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))