Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use these function definitions for the problems below: fun map (f,xs) = case xs of [] => [] | first::rest => (f first)::(map(f, rest)) fun

Use these function definitions for the problems below:

fun map (f,xs) =

case xs of

[] => []

| first::rest => (f first)::(map(f, rest))

fun filter (f,xs) =

case xs of

[] => []

| first::rest => if f first

then first::(filter (f, rest))

else filter (f, rest)

1. Write ML expressions that do the following:

1. Defines a datatype investment with two subtypes:

Stock - a tuple consisting of a symbol (string), price (int), shares (int) and date (int*int*int) of purchase

Cash - a tuple consisting of an amount (int) of money and date (int*int*int) of deposit

2. Defines a function valueOf that takes one investment as its argument and:

if the investment is a Stock, returns the value of price x shares

if the investment is Cash, returns the amount of money in cash

3. Binds a Stock investment with values ("AAPL", 100, 5, (2015, 5, 10)) to the variable appleStock 4. Binds a Cash investment with the value (200, (2014, 3, 7)) to the variable myCash

5. Binds the value ("GOOG", 70, 15, (2014, 8, 3)) to googleStock 6. Binds the value ("FB", 20, 8, (2015, 2, 9)) to facebookStock

7. Binds the value (IBM, 30, 10, (2010, 6, 20)) to ibmStock 8. Creates a list of all five investments (appleStock, ibmStock, googleStock, facebookStock and myCash) bound to investmentList 9. Uses the map function above to bind a list with the value of all the investments in investmentList to the variable ans

2. Write ML expressions that do the following:

1. Defines a function olderThan that takes a date and an investment as arguments and returns true if the investment is older than the given date, and false otherwise. 2. Defines a function olderThanJan_01_205 that takes an investment as an argument and returns true if the investment is older than January 1, 2015, and false otherwise. 3. Uses the function filter above to bind a list of all investments older than January 1, 2015 to the variable ans2

3. Write ML expressions that do the following:

1. Uses the function filter above with an anonymous function to bind a list of all investments with a value greater than 250 to the variable ans3

4. Write ML expressions that do the following:

1. Uses the map and filter functions to bind a list of the values of all investments older than January 1, 2015 to the variable ans4

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_step_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

=+impact member states and MNEs?

Answered: 1 week ago