WRIGHT STATE UNIVERSITY
Department of Computer Science and Engineering

            CS 776 Spring 2009       Assignment 1 (25 pts) (Due: May 1)              Prasad


1. UNIX ls (8 pts)

This problem explores the logic of the UNIX ls command, which displays the file names in the current directory. Write a list-function unixLs in SML that takes as input a list of strings, sorts them in lexicographical order and then outputs it in a tabular form. Note that you will need to experiment with the ls command, to glean the algorithm it uses to generate "squarish'' tabular outputs. The number of columns in the table output by ls depends on the length of the longest file name and the number of files, for a given window width.

2. Higher-Order Functions (4 pts)

One of the advantages of functional languages is the ability to write high-level functions which capture general patterns. In this exercise, we explore a high-level function to support list abstractions. The language Miranda allows the user to write list abstractions of the form:     
                [f(x) | x <- startlist; cond(x)]
where startlist is a list of type 'a, f : 'a -> 'b (for some type 'b), and
cond : 'a -> bool
. This expression results in a list containing all elements of the form f(x), where x is an element in the list, "startlist", and "cond(x)" is true. For example, if
sqr(x) = x*x
and odd(x) is true iff x is an odd integer, then

    [sqr(x) | x <- [1,2,5,4,3]; odd(x)]

returns the list [1,25,9] (that is, the squares of the odd elements of the list - 1,5,3). Note that the list returned preserves the order of startlist.

You are to write a function
    listcomp : ('a -> 'b) -> ('a list) -> ('a -> bool) -> ('b list)
in SML so that
    listcomp f startlist cond = [f(x) | x <- startlist; cond(x)]. 

3. Analysis of Function Definition (5 pts)

 Consider the following definition of  fast exponentiation using recursive doubling scheme.
 
fun fastexp base power =
            if power = 0 then 1
            else if power = 1 then base
            else let val pmod2 = power mod 2
                     val pdiv2 = power div 2
                 in
                     let
                        val auxprod = (fastexp (base * base) pdiv2)
                     in
                       if pmod2 = 0 then auxprod else base * auxprod
                     end
                  end;

 
Define a function countMul in SML that computes the number of multiplications performed for various input values.

    - List.tabulate (10, fn n => countMul 2 n);
      val it = [0,0,1,2,2,3,3,4,3,4] : int list

 

4. Revisiting Function Definition (4 + 1 + 3 pts)

Rewrite the above definitions in Haskell.


How to turnin the solution?

     Name your solution files asg1.sml and asg1.hs and submit it electronically on unixapps1.wright.edu by executing the following command. Note that sample test cases, and documentation should be included as comments in the turned-in files.

tcsh% /common/public/tkprasad/cs776/turnin-pa1  asg1.sml asg1.hs


T. K. Prasad   (03/31/2009)