0

How do i check if 2 items are in the same list in sml? I tried changing a member function but i could not make it work.

val routeList1 = ["Princes Street", "Haymarket", "Craiglockhart", "Musselburgh", "Stoneybank"]

if want to check if both "Princes Street" and "Haymarket" are in routeList1

I am new to this language, so any help would be nice

John Coleman
  • 51,337
  • 7
  • 54
  • 119
  • If you know how to check if one thing is in a list why is it hard to check if two things are? Just use the `andalso` Boolean operator. – John Coleman Jan 15 '17 at 21:18

2 Answers2

1

To check if "Princes Street" is in routeList1, you could write:

List.exists (fn s => s = "Princes Street") routeList1

(which uses List.exists [see doc] to check for an element s such that s = "Princes Street" is true).

To check for two strings, just check for one, then the other:

List.exists (fn s => s = "Princes Street") routeList1
  andalso List.exists (fn s => s = "Haymarket") routeList1
ruakh
  • 175,680
  • 26
  • 273
  • 307
1

You might call a function that checks for membership

fun elem x ys = List.exists (fn y => x = y) ys

And a function that checks for membership of many things

fun allElems xs ys = List.all (fn x => elem x ys) xs

assuming that elements can only be compared for equality. Then

val areThey = allElems ["Princes Street", "Haymarket"] routeList1
sshine
  • 15,635
  • 1
  • 41
  • 66