# Conference: Week 3

### ML!

• What are the big ideas?

### 1. List Reverse

Here is some start code for the problems this week: week-3-starter.sml

Without using any built-in SML functions, write an SML function

rev : 'a list -> 'a list

that takes a list l as an argument and returns a list of the elements of l in the reversed order. Once you have implemented the function, you should get the following behavior at the SML prompt:

- rev [1,2,3,4];
val it = [4,3,2,1] : int list
- rev ["a","b","c","d"];
val it = ["d","c","b","a"] : string list

### 2. Palindromes

A palindrome is a word that reads the same from left-to-right and right-to-left. Write an SML function

isPalindrome : string -> bool

that takes a string w and returns true if the string is a palindrome and false otherwise. You may want to use the SML function explode, which takes a string and returns a char list containing the individual characters of the string. Once you have implemented the function, you should get the following behavior at the SML prompt:

- isPalindrome "malayalam";
val it = true : bool
- isPalindrome "myxomatosis";
val it = false : bool

### 3. Digital Roots and Additive Persistence

Write an SML function

digits : int -> int list

that takes a non-negative integer $$n$$ as an argument and returns the list of digits of $$n$$ in the order in which they appear in $$n$$:

- digits 3124;
val it = [3,1,2,4] : int list
- digits 352663;
val it = [3,5,2,6,6,3] : int list

Now write an SML function

listAdd : int list -> int

that takes an integer list $$l$$ and returns the sum of the elements of $$l$$:

- listAdd [1,2,3,4];
val it = 10 : int
val it = 7 : int

Consider the process of taking a number, adding its digits, then adding the digits of the number derived from it, etc., until the remaining number has only one digit. The number of additions required to obtain a single digit from a number $$n$$ is called the additive persistence of $$n$$, and the digit obtained is called the digital root of $$n$$. For example, the sequence obtained from the starting number $$9876$$ is $$9876 \rightarrow 30 \rightarrow 3$$, so $$9876$$ has an additive persistence of $$2$$ and a digital root of $$3$$. Write two SML functions

additivePersistence : int -> int
digitalRoot : int -> int

that take integer arguments $$n$$ and return respectively the additive persistence and the digital root of $$n$$:

- additivePersistence 9876;
val it = 2 : int
- digitalRoot 9876;
val it = 3 : int

There are two ways to do this. Write two completely separate functions, or since they will be very similar, write one function that computes both the additivePersistance and digitalRoot and returns those two values as a tuple. Then additivePersistance and digitalRoot are one-line functions that just call the helper and extract the appropriate value.

### 4. List Cleaning

Without using any built-in SML functions, write a function

clean: ''a list -> ''a list

that takes a list l and returns the list of elements of l with the duplicates, i.e. second, third, etc. occurrences, removed, and where the remaining elements appear in the same order as in l . Once you have implemented the function, you should get the following behavior at the SML prompt:

- clean [1,6,2,4,12,2,13,6,9];
val it = [1,6,2,4,12,13,9] : int list

You’ll probably want to use our contains function from lecture:

fun contains x nil = false
| contains x (y::ys) = x = y orelse contains x ys;