Skip to content

Instantly share code, notes, and snippets.

@shehaaz
Created December 12, 2013 00:42
Show Gist options
  • Save shehaaz/7921325 to your computer and use it in GitHub Desktop.
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)
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;
@shehaaz
Copy link
Author

shehaaz commented Dec 12, 2013

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment