Created
May 19, 2018 10:13
-
-
Save fdjones/c496b0ca1765d3ea1bb209201ffc879c to your computer and use it in GitHub Desktop.
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
(* | |
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) = | |
let fun sum_aux (limit, ys, acc, count) = | |
if acc >= limit | |
then count - 1 | |
else | |
sum_aux (limit, tl ys, acc + hd ys, count + 1); | |
in | |
sum_aux(sum, xs, 0, 0) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment