Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Solve in Standard Meta Language of New Jersey (SML) and use only recursive defin

ID: 3748312 • Letter: S

Question

Solve in Standard Meta Language of New Jersey (SML) and use only recursive definitions built up from the basic built-in functions!

Pullman Transit offers many bus routes in Pullman. Assume that they maintain the bus stops for their routes as a list of tuples. The first element of each tuple is the bus route and the second element is the list of stops for that route (see below for an example).

val buses = [

("Lentil",["Chinook", "Orchard", "Valley", "Emerald","Providence", "Stadium", "Main", "Arbor", "Sunnyside", "Fountain", "Crestview", "Wheatland", "Walmart", "Bishop", "Derby", "Dilke"]),

("Wheat",["Chinook", "Orchard", "Valley", "Maple","Aspen", "TerreView", "Clay", "Dismores", "Martin", "Bishop", "Walmart", "PorchLight", "Campus"]),

("Silver",["TransferStation", "PorchLight", "Stadium", "Bishop","Walmart", "Shopco", "RockeyWay"]),

("Blue",["TransferStation", "State", "Larry", "TerreView","Grand", "TacoBell", "Chinook", "Library"]),

("Gray",["TransferStation", "Wawawai", "Main", "Sunnyside","Crestview", "CityHall", "Stadium", "Colorado"]) ]

a) Assume that you are creating an application for Pullman Transit. You would like to write an ML function busFinder that takes the list of bus routes and a stop name, and returns the list of the bus routes which stop at the given bus stop. The type of busFinder should be ''a -> ('b * ''a list) list -> 'b list. (Hint: You can make use of exists function you defined earlier.)

Examples:

> busFinder "Walmart" buses

["Lentil","Wheat","Silver"]

> busFinder "Shopco" buses

["Silver"]

> busFinder "Main" buses

["Lentil","Gray"]

> busFinder "Beasley" buses

[]

b) Explain in a comment why the type is ''a -> ('b * ''a list) list -> 'b list and not ''a -> ('a * ''a list) list -> 'a list

Explanation / Answer

fun busFinder _ [] = []
   |busFinder busStop ((x, y)::xs) =                    (*x becomes the bus name, y becomes the list of stops that bus goes to*)  
       if exists (busStop, y) then x::(busFinder busStop xs)   (*if the bus stop exists in the list of bus stops for the current bus, then add that bus to the return list*)
       else busFinder busStop xs;

      
      
fun busFinderTest () =
   let
       val buses =
       [("Lentil",["Chinook", "Orchard", "Valley", "Emerald","Providence", "Stadium",
       "Main", "Arbor", "Sunnyside", "Fountain", "Crestview", "Wheatland", "Walmart",
       "Bishop", "Derby", "Dilke"]),
       ("Wheat",["Chinook", "Orchard", "Valley", "Maple","Aspen", "TerreView", "Clay",
       "Dismores", "Martin", "Bishop", "Walmart", "PorchLight", "Campus"]),
       ("Silver",["TransferStation", "PorchLight", "Stadium", "Bishop","Walmart",
       "Shopco", "RockeyWay"]),
       ("Blue",["TransferStation", "State", "Larry", "TerreView","Grand", "TacoBell",
       "Chinook", "Library"]),
       ("Gray",["TransferStation", "Wawawai", "Main", "Sunnyside","Crestview",
       "CityHall", "Stadium", "Colorado"])]

       val busFinderT1 = ((busFinder "Walmart" buses) = ["Lentil","Wheat","Silver"])
       val busFinderT2 = ((busFinder "Shopco" buses) = ["Silver"])
       val busFinderT3 = ((busFinder "Main" buses) = ["Lentil","Gray"])
   in
       print ("busFinder:-------------------- " ^
       " test1: " ^ Bool.toString(busFinderT1) ^ " " ^
       " test2: " ^ Bool.toString(busFinderT2) ^ " " ^
       " test4: " ^ Bool.toString(busFinderT3) ^ " ")
   end;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote