Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please solve these two USING F# (i) Write directly by recursion a function findSum : int -> int list -> int that takes a tar-
Please solve these two USING F#
(i) Write directly by recursion a function findSum : int -> int list -> int that takes a tar- get sum and a list xs, and returns the smallest n such that the sum of the first n elements of the list is the target sum. Your function should return the length if there is no such n. > findSumo [] ;; val it : int = 0 > findSum 2 (1;-1;3;-1] ;; val it : int = 4 > findSum 2 [1;-1;3;-1;1;5] ; ; val it : int = 4 > findSum 5 [1;5; -2;3; -7]; ; val it : int = 5 (ii) Reimplement findSum using fold, by filling in the ... in the following declaration: let findSum2 sum xs = let (, n) = List.fold (fun (r, i) -> fun x -> ...) (sum, 0) xs (i) Write directly by recursion a function findSum : int -> int list -> int that takes a tar- get sum and a list xs, and returns the smallest n such that the sum of the first n elements of the list is the target sum. Your function should return the length if there is no such n. > findSumo [] ;; val it : int = 0 > findSum 2 (1;-1;3;-1] ;; val it : int = 4 > findSum 2 [1;-1;3;-1;1;5] ; ; val it : int = 4 > findSum 5 [1;5; -2;3; -7]; ; val it : int = 5 (ii) Reimplement findSum using fold, by filling in the ... in the following declaration: let findSum2 sum xs = let (, n) = List.fold (fun (r, i) -> fun x -> ...) (sum, 0) xsStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started