Question
Map
fun map (f,xs) =
case xs of
[] => []
| x::xs’ => (f x)::(map(f,xs’))
Filter
fun filter (f,xs) =
case xs of
[] => []
| x::xs’ => if f x
then x::(filter (f,xs’))
else filter (f,xs’)
Reduce/Fold
fun fold (f,acc,xs) =
case xs of
[] => acc
| x::xs’ => fold (f, f(acc,x), xs’)
Zip
fun zip xs ys =
case (xs,ys) of
([],[]) => []
| (x::xs’,y::ys’) => (x,y) :: (zip xs’ ys’)
1. Complete the symbols function, which returns a list of symbols from a list of stocks. Then complete symbols_map, which uses the map function to accomplish the same thing.
2. Complete the values function, which returns a list of values from a list of stocks. Then complete values_map, which uses the map function to accomplish the same thing.
3. Complete the greater_than_1000 function, which returns a list of stocks with values greater than 1000 from a list of stocks
4. Complete the symbols_and_values function, which returns the symbols and values of all stocks with values greater than 1000
5. Complete the total_value function, which returns total value of all stocks with values greater than 1000
mapreduce1.sml
mapreduce2.sml
Explanation / Answer
fun map f xs = case xs of [] => [] | x::xs' => (f x)::(map f xs') fun filter f xs = case xs of [] => [] | x::xs' => if (f x) then x::(filter f xs') else (filter f xs') fun fold f acc xs = case xs of [] => acc | x::xs' => fold f (f (acc, x)) xs' fun zip xs ys = case (xs,ys) of ([],[]) => [] | (x::xs', y::ys') => (x,y) :: (zip xs' ys') | _ => [] datatype investment_type = Stock of string * int * int | Cash of int val ibm = Stock("IBM", 137, 50) val facebook = Stock("FB", 106, 10) val google = Stock("GOOG", 700, 15) val apple = Stock("AAPL", 100, 25) val microsoft = Stock("MSFT", 51, 25) val yahoo = Stock("YHOO", 33, 25) val oracle = Stock("ORCL", 38, 12) (* -------------------------------------------------------------------- *) (* * 1. Create a Stock for hewlettPackard: symbol = "HPQ", price = 11, shares = 30 *) val hewlettPackard = Stock("HPQ", 11, 30) (* -------------------------------------------------------------------- *) (* -------------------------------------------------------------------- *) (* * 2. Create a Cash value called cashOnHand with an amount of 1200 *) val cashOnHand = Cash(1200) (* -------------------------------------------------------------------- *) val investments = [ibm, facebook, google, apple, microsoft, yahoo, oracle, hewlettPackard, cashOnHand] (* * An example of using map to create a list of stock symbols *) fun symbolOf investment = case investment of Stock(symbol, price, shares) => symbol | Cash(amount) => "cash" fun symbols investments = map symbolOf investments val ans1 = symbols investments (* -------------------------------------------------------------------- *) (* * 3. Complete the definition of valueOf to return price * shares *) fun valueOf investment = case investment of Stock(symbol,price,shares)=> price * shares | Cash(ammount) => ammount (* * 4. Complete the definition of values to use map to return a list of stock values *) fun values investments = map valueOf investments val ans2 = values investments (* -------------------------------------------------------------------- *) (* * An example of using filter to create a list of stocks with values greater than $1000 *) fun greater_than_1000 investments = filter (fn x => (valueOf x) > 1000) investments val ans3 = greater_than_1000 investments (* -------------------------------------------------------------------- *) (* * 5. Complete the definition of less_than_20 to use filter to return a list of stocks with less than 20 shares *) fun shareOf investments = case investments of Stock(_,_,shares) => shares |Cash(ammount) => ammount fun less_than_20 investments = filter(fn x => (shareOf x) < 20) investments val ans4 = less_than_20 investments (* -------------------------------------------------------------------- *) (* * An example of using zip to create a list of tuples: (symbols, values) *) fun symbols_and_values investments = let val tmp = greater_than_1000 investments in zip (symbols tmp) (values tmp) end val ans5 = symbols_and_values investments (* -------------------------------------------------------------------- *) (* * 6. Use zip to create a list of tuples for stocks with less than 20 shares *) fun symbols_and_values2 investments = let val tmp = less_than_20 investments in zip (symbols tmp) (values tmp) end val ans6 = symbols_and_values2 investments (* -------------------------------------------------------------------- *) (* * An example of using fold to add up the value of all stocks *) fun total_value investments = fold (fn (x, i) => x + (valueOf i)) 0 investments val ans7 = total_value investments (* -------------------------------------------------------------------- *) (* * 7. Use fold to add up the value of all stocks with less than 20 shares *) val newInvestments = less_than_20 investments fun total_value2 investments = fold (fn (x,i) => x + (valueOf i)) 0 newInvestments val ans8 = total_value2 investments