Created
December 12, 2013 00:42
-
-
Save shehaaz/7921325 to your computer and use it in GitHub Desktop.
OCaml Question: Write a function to sum all the ODD numbers between a & b (a is guaranteed to be smaller than b)
This file contains 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
let sumOdd a b = | |
let rec inner a b = | |
if a>=b then b else a + (inner (a+2) b) | |
in | |
let (new_a,new_b) = | |
( | |
(if ((a mod 2) = 0) then (a+1) else a), | |
(if ((b mod 2) = 0) then (b-1) else b) | |
) | |
in | |
inner new_a new_b; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Important lesson I learnt:
When writing a "let" there has to be an "in" following it. I ran into trouble when trying to set the new_a and new_b after checking if they were EVEN.