Skip to content

Instantly share code, notes, and snippets.

@fdjones
Created May 14, 2018 21:01
Show Gist options
  • Save fdjones/c918d571c2143738d3f34d039de22c01 to your computer and use it in GitHub Desktop.
Save fdjones/c918d571c2143738d3f34d039de22c01 to your computer and use it in GitHub Desktop.
(* Write a function number_before_reaching_sum that takes an int called sum, which you can assume
is positive, and an int list, which you can assume contains all positive numbers, and returns an int.
You should return an int n such that the first n elements of the list add to less than sum, but the first
n + 1 elements of the list add to sum or more. Assume the entire list sums to more than the passed in
value; it is okay for an exception to occur if this is not the case. *)
fun number_before_reaching_sum (sum : int, xs : int list) =
if null xs then 0
else
hd xs + number_before_reaching_sum(sum, tl xs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment