CSCI305.github.io

CSCI 305 Programming Languages

ML Part 1

Reading: Webster Ch. 5

Instructions

  1. Watch This Video
  2. Review the Lecture Slides
  3. Review the Example Code
  4. Complete the Out of Class Exercise
  5. Check Your Learning
  6. Attend Class and Complete the In Class Exercises, Be sure you finish and turn in Ruby Lab!
  7. Check Your Learning

Out of Class Exercise

Exercise 1:

Write a function max of type int list -> int that returns the largest element of a list of integers. Your function need not behave well if the list is empty. Hint: Write a helper function maxhelper that takes as a second parameter the largest element seen so far. Then you can complete the exercise by defining:

fun max x = maxhelper (tl x, hd x);

Exercise 2:

Write a function fourth of type 'a list -> 'a that returns the fourth element of a list. Your function need not behave well on lists with less than four elements.

Check Your Learning:

Solution Video - (:)
Solution:

Exercise 1:

fun maxhelper ([], n) = n
 |  maxhelper ((x::xs), n) =
      if x > n then maxhelper (xs, x)
      else maxhelper (xs, n);

fun max x = maxhelper (tl x, hd x);

Exercise 2:

fun fourth x = hd (tl (tl (tl x)));

In Class Exercises

Exercise 1:

Write a function cuber of type real -> real that returns the cube of its parameter.

Exercise 2:

Write a function thirds of type string -> char that returns the third character of a string. Your function need not behave well on strings with lengths less than 3.

Exercise 3:

Write a function del3 of type 'a list -> 'a list whose output list is the same as the input list, but with the third element deleted. Your function need not behave well on lists with lengths less than 3.

Exercise 4:

Write a function max3 of type int * int * int -> int that returns the max value of a set of three values.