Skip to content

Instantly share code, notes, and snippets.

@tonetheman
Created October 11, 2021 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonetheman/78d7d2de1a01ba5081d8ffcde74b7cf0 to your computer and use it in GitHub Desktop.
Save tonetheman/78d7d2de1a01ba5081d8ffcde74b7cf0 to your computer and use it in GitHub Desktop.
crazy recursive define of is_even and is_odd in nim
# predicate zero or not
proc zero(n : int) : bool =
return n==0
# simple subtract 1
proc sub1(n : int) : int =
return n-1
# forward defs since this is recursive
proc is_odd(n : int) : bool;
proc is_even(n : int) : bool;
proc is_even(n : int) : bool =
# echo("is even called",n)
return zero(n) or is_odd(sub1(n))
proc is_odd(n : int) : bool =
# echo("is odd called",n)
return not zero(n) and is_even(sub1(n))
echo( is_even(10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment