|| Sample solutions for Assignment 1
|| CSE 341 Spring 2002
|| The individual solutions are taken from student work.
|| prepared by Harr Chen <harrchen@cs>
|| w/ notes by Ken Yasuhara <yasuhara@cs>


||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|| Problem 2

||-------------------------------------------------------
||Function name: mean
||Computes the mean of a list of numbers.
||If the list is empty, this function returns zero.  
||-------------------------------------------------------

mean::[num]->num  
mean [] = 0
mean x = sum x / (#x)


||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|| Problem 3

||*****************************************************************||
|| 3. Construct a sdev to find the standard deviation.

|| Function sdev - the square root of the mean of the squares of
||  the differences of each number from the mean of the list.
|| Function sdm - take a list and return the squares of the the
||  the different of each number from teh mean of the list
|| Function mean evalates the average - see #2.

sdev [] = 0
sdev nl = sqrt ((sdm nl) / #nl) 
          where
          sdm [] = 0
          sdm (x:xs) = ((x - mean nl)^2 + sdm xs)

|| TA's implementation
stddev [] = 0
stddev list = 
  sqrt (mean (map diff_mean_sqrd list))
  where
  diff_mean_sqrd x = (x - (mean list))^2


||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|| Problem 4 (two implementations)


|| my duplicate function
|| duplicate:: => num->*->[*]

duplicate n x = [], if n = 0 
              = x : duplicate (n-1) x, otherwise


|| duplicate - Duplicates a sequence several times

|| TA's note:  See how this one uses take and repeat to take advantage
||             of lazy evaluation?  (And yes, for the record, I think
||             even the author would agree that the comment above
||             could have been more informative, e.g., by discussing
||             what the parameters n and x are.) 

duplicate n x = [], n=0
              = take n ( repeat x ), otherwise
