Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will write a total of 4 SML functions (not counting local helper functions).

ID: 3669985 • Letter: Y

Question

You will write a total of 4 SML functions (not counting local helper functions). Your solutions must be solved as one to two-line functions involving iterators such as map, filter, and the custom infix operator --.

1. Write a function multiples that takes two integers m and n as arguments and returns a list of the first m multiples of n. For example, multiples(3, 5) should return [5,10,15]. You may assume that m is greater than or equal to 0.

2. Write a function sumTo that takes an integer n as an argument and returns the sum of the first n reciprocals. For example, sumTo(2) should return 1.5. You may assume that n is greater than 0 (handling the 0 case may require an if/else or a better version of one of these functions).

3. Write a function numNegative that takes a list of integers lst as an argument and returns the number of negative values in the list. For example, numNegative([5, ~7, 31, ~14, 2]) should return 2.

4. Write a function allPairs that takes two integers m and n as arguments and that returns a list of all tuples whose first value is between 1 and m and whose second value is between 1 and n. The tuples should appear in nondecreasing order using the first value and within those groups, in increasing order by the second value. For example, allPairs(3, 4) should return: [(1,1),(1,2),(1,3),(1,4),(2,1),(2,2),(2,3),(2,4),(3,1),(3,2),(3,3),(3,4)] You may assume that each of m and n are greater than 0.

Explanation / Answer

Answer 1:

- map (fn x=>x*5, [1,2,3,4,5]); (* "map" sucessor func to list *)

- fun j (x) = [f x, g x]; j (3); j (5); (* functions and lists. *)

val j = fn : int -> int list

- f(3);

val it = 5 : int

- let val a = 5 in

= let fun f(x) = x*a in

= let val a = 5 in

= f(3)

= end

= end

= end;

val it = [x,a]: int list

- fun f (x:int*int) = (#3 x) div (#5 x);

val f = fn : int * int -> int

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote