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

Solve in Standard Meta Language of New Jersey (SML) and use only recursive defin

ID: 3748311 • Letter: S

Question

Solve in Standard Meta Language of New Jersey (SML) and use only recursive definitions built up from the basic built-in functions.!

Write a function firstN that takes a list and an int n and returns the first n elements in the list. The type of firstN should be string 'a list -> int -> 'a list. If the input list is empty or if the length of the list is less than n, then the function should return the complete list. (You may assume that n is greater than 0.)

Examples:

> firstN ["a", "b", "c", "x", "y"] 3

["a", "b", "c"]

> firstN [1,2,3,4,5,6,7] 4

[1,2,3,4]

> firstN [1,2,3,4,5,6,7] 10

[1,2,3,4,5,6,7]

> firstN [[1,2,3],[4,5],[6],[],[7,8],[]] 4

[[1,2,3],[4,5],[6],[]]

> firstN [] 5 /*may give a warning*/

[]

Explanation / Answer

fun firstN n [] = [] | firstN 0 L = [] | firstN n (x::rest) = (x::(firstN (n - 1) rest));

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