Question: Standard ML code Write a function firstN that takes a list and an int n and returns the first n elements in the list. The
Standard ML code
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*/ []
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
