393
Rule (lemmy.blahaj.zone)
submitted 2 weeks ago by Persona3Reload to c/196
you are viewing a single comment's thread
view the rest of the comments
[-] gerryflap@feddit.nl 23 points 2 weeks ago

Using Haskell you can write it way more concise:

iseven :: Int -> Bool
iseven 0 = True
iseven 1 = False
iseven 2 = True
iseven 3 = False
iseven 4 = True
iseven 5 = False
iseven 6 = True
iseven 7 = False
iseven 8 = True
...

However, we can be way smarter by only defining the 2 base cases and then a recursive definition for all other numbers:

iseven :: Int -> Bool
iseven 0 = True
iseven 1 = False
iseven n = iseven (n-2)

It's having a hard time with negative numbers, but honestly that's quite a mood

[-] luciferofastora@lemmy.zip 12 points 2 weeks ago

Recursion is its own reward

this post was submitted on 31 Oct 2024
393 points (100.0% liked)

196

16534 readers
1953 users here now

Be sure to follow the rule before you head out.

Rule: You must post before you leave.

^other^ ^rules^

founded 1 year ago
MODERATORS