Simplify list compare logic

I’m sure this could be done better, so just wondering how you would compare all values in two lists, returning true if all values are the same.

My current solution:

secret = [3,4,5,1,3,6]
challenge = [3,4,5,1,3,6]

List.map2 (==) secret challenge |> List.all (\c -> c == True)
--> True

I’d like to just use the core, but examples using List.Extra or something else would be interesting for my education. Specifically, I think this may be a time I could use the >> operator, but I have trouble conceptualising its use.

Isn’t comparing the lists directly the same?

In elm repl:

> secret = [3,4,5,1,3,6]
[3,4,5,1,3,6]
    : List number
> challenge = [3,4,5,1,3,6]
[3,4,5,1,3,6]
    : List number
> (secret == challenge)
True : Bool

> challenge = [3,4,5,1,3,5]
[3,4,5,1,3,5]
    : List number
> (secret == challenge)
False : Bool

(Note that parentheses are needed because of a bug in elm repl if I remember correctly)

Or did I miss something? Maybe you want another way as an exercise?

Also your solution won’t work if lists have a different length as List.map2 ignores extra values:

> challenge = [3,4,5,1,3,6,7]
[3,4,5,1,3,6,7]
    : List number
> List.map2 (==) secret challenge |> List.all (\c -> c == True)
True : Bool

Hah! Of course it is! Sorry, I was looking for complexity where there shouldn’t have been.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.