Created
October 11, 2021 14:52
-
-
Save tonetheman/78d7d2de1a01ba5081d8ffcde74b7cf0 to your computer and use it in GitHub Desktop.
crazy recursive define of is_even and is_odd in nim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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