Write in SML: 1. numbersTosum and numbersTosumTail - 15% (a) (7%) write a functi
ID: 3757123 • Letter: W
Question
Write in SML:
1. numbersTosum and numbersTosumTail - 15% (a) (7%) write a function numbersToSum that takes can assume is positive), and an int list (called L which you can assume contains positive numbers) and returns an int list. The returned list should include the first n elements from the input list L 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 sum value. The type of numbersToSum should be int -> int list -> int list. an int value (called sum - which you Examples numbersToSum 100 [10, 20, 30, 40] [10, 20, 30] numbersToSum 30 [5, 4, 6, 10, 4, 2, 1, 5] [5, 4, 6, 10, 4] numbersToSum 1 [2] [1 numbersToSum 1 [ (b) (896) Re-write the numbersTosum function from part (a) as a tail-recursive function. Name your function numbersToSumTail. The type of numbersToSumTail should be int -> int list ->int list.Explanation / Answer
program:
function main()
function lst = numbersToSum(sum, L)
lst = []
total = 0
for i = 1:length(L)
total = total + L(i)
if total <= sum
lst(i)=L(i)
end
end
end
L = [5,4,6,10,4,2,1,5]
sum = 30
numbersToSum(sum, L)
end
main()
Output:
L =
5 4 6 10 4 2 1 5
sum = 30
lst = [](0x0)
total = 0
total = 5
lst = 5
total = 9
lst =
5 4
total = 15
lst =
5 4 6
total = 25
lst =
5 4 6 10
total = 29
lst =
5 4 6 10 4
total = 31
total = 32
total = 37
ans =
5 4 6 10 4
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.