Check for differences of two sets in Elm

356 views Asked by At

I am trying to check if my set of chars in elm contains only valid chars. If a char is found that is invalid, I return those chars, separated by a comma. What I currently have tries to convert the string to a list of chars, then that list to a set of chars. Then diffs the set with a set of valid chars, not sure where to go after that, I have not written much in Elm before. If no diffs are found then return nothing.

validChars : Set.Set Char
validChars =
Set.fromList <| String.toList " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"


validString : String -> Maybe String
validString x =
let
  list = 
    String.toList x
  set1 = 
    Set.fromList list
  diffs= 
    Set.diff set1 validChars

  if Set.isEmpty diffs == True
    Nothing

The if statement doesn't work in Elms online compiler, it says it is looking for "in"

1

There are 1 answers

0
Charles Maria On

Your code has a few things wrong with it, here's a version that compiles.

import Set

validChars : Set.Set Char
validChars = Set.fromList <| String.toList " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"


validString : String -> Maybe String
validString string =
  let
    diff = 
      Set.diff validChars <| Set.fromList <| String.toList string
  in
    if not <| Set.isEmpty diff then
      Just string
    else
      Nothing

Basically when it said it was looking for in, it wasn't wrong, in is a necessary part of a function when you use let statements.