Answered step by step
Verified Expert Solution
Link Copied!

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#

image text in transcribed

(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) xs

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management Systems Designing And Building Business Applications

Authors: Gerald V. Post

1st Edition

0072898933, 978-0072898934

More Books

Students also viewed these Databases questions

Question

8. Do the organizations fringe benefits reflect diversity?

Answered: 1 week ago

Question

7. Do the organizations social activities reflect diversity?

Answered: 1 week ago