0% found this document useful (0 votes)
11 views2 pages

csc330h1-4w25

The document outlines a homework assignment for a programming languages course, specifically focusing on writing recursive ML functions using pattern matching. It includes 36 exercises that cover various tasks such as calculating factorials, Fibonacci numbers, list manipulations, and implementing custom map and reduce functions. The exercises are designed to reinforce concepts of recursion and functional programming in ML.

Uploaded by

gotoh32
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

csc330h1-4w25

The document outlines a homework assignment for a programming languages course, specifically focusing on writing recursive ML functions using pattern matching. It includes 36 exercises that cover various tasks such as calculating factorials, Fibonacci numbers, list manipulations, and implementing custom map and reduce functions. The exercises are designed to reinforce concepts of recursion and functional programming in ML.

Uploaded by

gotoh32
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CSC330 W25 Programming Languages – Homework §1.

4 Page 1/2

Homework §1.4 – ML Recursion

In Exercises 1-18, write a recursive ML function using pattern matching that will do the given
task. You may assume the function is given valid inputs.
1. (Factorial) Inputs an integer 𝑛𝑛 ≥ 0 and returns the value 𝑛𝑛!
2. (Fibonacci) Inputs an integer 𝑛𝑛 ≥ 0 and returns the value of the function:
1, 𝑛𝑛 = 0
𝑓𝑓(𝑛𝑛) = � 1, 𝑛𝑛 = 1
𝑓𝑓(𝑛𝑛 − 1) + 𝑓𝑓(𝑛𝑛 − 2), 𝑛𝑛 ≥ 2
3. Inputs an integer 𝑛𝑛 ≥ 1 and returns the value of the function:
1, 𝑛𝑛 = 1
𝑓𝑓(𝑛𝑛) = � 1, 𝑛𝑛 = 2
𝑓𝑓�𝑓𝑓(𝑛𝑛 − 1)� + 𝑓𝑓�𝑛𝑛 − 𝑓𝑓(𝑛𝑛 − 1)�, 𝑛𝑛 ≥ 3
4. Inputs a list of integers and returns the sum of all integers in the list.
5. Inputs a list of integers and returns the product of all integers in the list. You may
assume an empty list returns 1.
6. (Power) Inputs a 2-tuple of a real number 𝑥𝑥 and integer 𝑘𝑘 ≥ 0, and returns 𝑥𝑥 𝑘𝑘 .
7. (GCD) Inputs a 2-tuple of non-negative integers and returns their greatest common
divisor.
8. Inputs a 2-tuple of a list and an integer 𝑘𝑘 ≥ 0 and returns the list cycled 𝑘𝑘 times. You
should use the cycling function from the previous homework.
9. Inputs a list [𝑎𝑎1 , 𝑎𝑎2 , ⋯ , 𝑎𝑎𝑛𝑛 ] and return the list [𝑎𝑎1 , 𝑎𝑎1 , 𝑎𝑎2 , 𝑎𝑎2 , ⋯ , 𝑎𝑎𝑛𝑛 , 𝑎𝑎𝑛𝑛 ] where each
element has been duplicated.
10. Inputs a list and returns the list with all elements in reverse order. You may not use the
built-in function rev.
11. Inputs a list and returns the last element of the list. You may assume the list has at least
one element.
12. Inputs two integers 𝑚𝑚 and 𝑛𝑛, where 𝑚𝑚 ≤ 𝑛𝑛. Returns the list of all integers between and
including 𝑚𝑚 and 𝑛𝑛, otherwise returns an empty list.
13. (Maximum) Inputs a list of integers and return the largest element. You may assume the
list is nonempty.
14. (Membership) Inputs a list and a value. Returns true if the value appears in the list,
otherwise returns false.
15. (Removal) Inputs a list and a value. Returns the list after every given value that appears
in the list has been removed.
16. (Replacement) Inputs a list and two values. Returns the list after every element in the
list that matches the first value is replaced with the second value.
17. (SortedMerge) Inputs a 2-tuple of sorted integer lists and returns a single list with their
elements combined and sorted.
18. (Linear Search) Inputs a 2-tuple of a list of integers and an integer 𝑥𝑥, and returns the first
position/index where 𝑥𝑥 is found, otherwise returns ~1 if 𝑥𝑥 is not found in the list.
(Assume the index of the first position is 1.)
CSC330 W25 Programming Languages – Homework §1.4 Page 2/2

In Exercises 19-22, write mutually recursive ML functions described below using the and
construct to do the given task succinctly.
19. The functions isodd and iseven will be given a non-negative integer. They should
return the correct bool value corresponding to the parity of the integer.
20. The functions oddalts and evenalts will be given a list of elements. They should
return a list with elements in the odd positions only and in the even positions only,
respectively. For example, oddalts([2,3,5,7,11]) should return [2,5,11] and
evenalts([2,3,5,7,11]) should return [3,7].
21. The functions:
2, 𝑛𝑛 = 0 4, 𝑛𝑛 = 0
𝑓𝑓(𝑛𝑛) = � and 𝑔𝑔(𝑛𝑛) = �
𝑛𝑛 + 𝑔𝑔(𝑛𝑛 − 1), 𝑛𝑛 ≥ 1 𝑛𝑛 + 𝑓𝑓(𝑛𝑛 − 1), 𝑛𝑛 ≥ 1
22. The functions:
1, 𝑛𝑛 = 0 0, 𝑛𝑛 = 0
𝑓𝑓(𝑛𝑛) = � and 𝑔𝑔(𝑛𝑛) = �
𝑛𝑛 − 𝑔𝑔�𝑓𝑓(𝑛𝑛 − 1)�, 𝑛𝑛 ≥ 1 𝑛𝑛 − 𝑓𝑓�𝑔𝑔(𝑛𝑛 − 1)�, 𝑛𝑛 ≥ 1

In Exercises 23-24, write an ML function using the let construct to do the given task succinctly.
23. Inputs a real number 𝑥𝑥 and returns 𝑥𝑥100 . Do not use any built-in functions.
24. Inputs a list of integers and returns the largest element by computing the maximum of
the tail first. You may assume the list is nonempty.

In Exercises 25-28, use the built-in function map to perform the given task.
25. Given a list of integers, return the list where each element is squared.
26. Given a list of ordered pairs of integers, returns the list where each element is the sum
of each ordered pair.
27. Given a list of strings, return the list where each element is concatenated with "ed".
28. Given a list of characters from the alphabet, return the list where each character has
been shifted to the previous character (#"a" can be shifted to #"Z").

In Exercises 29-34, use the built-in functions foldl or foldr to perform the given task.
29. Given a list of integers, return the sum of each integer in the list.
30. Given a list of integers, return the product of each integer in the list.
31. Given a list of integers, return the list in reverse order. (Do not use rev.)
32. Given a list of positive integers, return largest integer in the list, or else 0 for empty list.
33. Given a list of strings, return the concatenation of all strings in the list.
34. Given a list of elements, return the list where each element has been duplicated.

35. Write your own map function, i.e., write a function named mymap that inputs a function
and a list as a 2-tuple. It returns the list after the function has been applied to each
element of the list.

36. Write your own reduce function, i.e., write a function named reduce that inputs a
two-argument accumulation function, an initial value, and a list as a 3-tuple. It returns
the value when the function is applied starting with the initial value and successively to
all values in the list (either from the front or the end of the list).

You might also like