CSE 130
Homework #4 (Due 5/30)


The ML assignments should use PolyML if it all possible. If SML NJ must be used please indicate as such upon turn-in

  1. Write the smallest form of an ML function called del3 which takes a list and returns the list without the third item. Make sure you address all input cases of lists.

  2. Define a function less of type int * int list -> int list so that less(e,L) returns a list of all the integers in L that are less than e.

  3. Define a function repeats of type ''a list -> bool so that repeats(L) is true if and only if the list L has two equal elements next to each other.

  4. In the following exercises use one-line solutions using map, foldr, or foldl. You may also use other predefined ML function but you cannot write other named functions and do not use explicit recursion. Any helper functions should be anonymous. Exaple: add2 takes a list of integers and returns a list with 2 added to each element the answer would be: fun add2 x = map (fn a => a + 2) x;

    1. Write a function ordlist of type char list -> int list that takes a list of characters and returns the list of the integer codes for those characters.

    2. Write a function sqsum of type int list -> int that takes a list of integers and returns the sum of the squares of those integers. Ex. sqsum[1,2,3] = 15

    3. Write a function dupList of type 'a list -> 'a list whose output is the same as the input list repeated twice in a row. Ex. dupList [1,2,3] = [1,1,2,2,3,3]. Note dupList [ ] = [ ]

    4. Write a function called myimplode that works just like the predefined implode. myImplode char list -> string

    5. Write a function evens of type int list -> int list that takes a list of integers and returns the list of all the even elements from the original list (in the original order). Ex. evens [1,2,3,4] -> [2,4]

    6. Define a function mappy with the same functionality as map, but without using map. This should be a one liner as well -- use foldl or foldr.

  5. Define ML functions mymap, myfoldr, and myfoldl that have the same functionality as map,foldr,and foldl (without using said functions). Make sure your functions work by testing your answers from #4 with your new functions - name your new solutions with a 5 after them (ex. ordlist5, sqsum5, etc). Note mymap, myfoldr, and myfoldl will not be one line solutions though the reimplementations of #4 would be.

  6. Implement the primary ML functions hd, tl, (::) cons, map, foldl, and foldr in JavaScript. Use the Array type as the list type and you should be using prototypes properly. Once implemented do problems 1-4 again this time in JavaScript attempting to make them as close as possible to the ML syntax. Note: given special character limitations in JavaScript you would not be able to make your own operators like :: or @ easily so instead we will use cons or other alpha names for FP constructs as needed.

Homework should be submitted as single zip/tar.gz/tar.bz2 archive containing all needed files. The archive should be submitted on ieng6.ucsd.edu using the following command:
turnin -c cs130s -p hw4 my_hw4_code.zip

where the archive file can be named anything, but the -p hw# part should have the correct number for the assignment.




Back to Main CSE 130 Page