mirror of
https://github.com/RGBCube/nu_scripts
synced 2025-08-01 06:37:46 +00:00

Since 0.101.0 we will finally catch more illegal `def` signatures. As the grammar for input/output types is rather restricted, this would error otherwise
30 lines
986 B
Text
30 lines
986 B
Text
# Mutually recursive versions of even and odd commands
|
|
|
|
|
|
# even returns true if passed in 0. odd returns returns true if passed in 1
|
|
# Else, they subtract 2 and call the other fn: even calls odd ($n - 2)
|
|
#
|
|
|
|
|
|
# These functions are meant to be used with the tramp module which implements
|
|
# a trampoline wrapper closure. Thus, for each even, odd command, the
|
|
# normal recursive case will actually return a thunk..
|
|
|
|
# Return true if number is even. Calls mutually recursive odd function
|
|
# if number is greater than 1.
|
|
def even [n: int, acc=true]: nothing -> any {
|
|
if $n == 0 { return $acc } else if $n == 1 {
|
|
return (not $acc) } else {
|
|
{|| odd ($n - 2) (not $acc) }
|
|
}
|
|
}
|
|
|
|
|
|
# Returns true if number is odd. Will cooperate with even in a mutually recursive fashion.
|
|
# Warning: do not pass any numbers less than 0
|
|
def odd [n: int, acc=true]: nothing -> bool {
|
|
if $n == 0 { return (not $acc) } else if $n == 1 {
|
|
return $acc } else {
|
|
{|| even ($n - 2) (not $acc) }
|
|
}
|
|
}
|