2
votes

i want to write a function that checks for equality of lists in SML for instance : [1,2,3]=[1,2,3]; val it = true : bool

So instead of writing down the whole thing, i want to make a function that takes two predefined lists, and compare them, so that if list01 = [1,2,3] and list09 = [1,2,3] then fun equal (list01,list09); will return -val it = true : bool;

Thanx in advance for any ideas/hints and help :)

2
What do you mean by "writing down the whole thing"? Writing equal (list01, list09) is actually longer than writing list01 = list09, so what's the "whole thing" you want to avoid writing? - sepp2k
i assumed it would be easier to create a function compare ([list01,list09]); than actually compare the two lists manually, if they were very! long. - user457142

2 Answers

1
votes

Here is a not checked sample:

fun compare ([], []) = true # both empty
    |   compare (x::xs, y::ys) = (x = y) and compare(xs,ys)
    |   compare (_, _) = false # different lengths
7
votes

You seem to be aware that = works on lists, so (as I already said in my comment) I don't see why you need to define an equal function.

That being said, you can just write:

fun equal (a, b) = (a = b);