10

Elm supports [1..100], but if I try ['a'..'z'], the compiler gives me a type mismatch (expects a number, gets a Char). Is there any way do make this work?

rausch
  • 3,148
  • 2
  • 18
  • 27

1 Answers1

21

Just create a range of numbers and map it to chars:

List.map Char.fromCode [97..122]

Edit, or as a function:

charRange : Char -> Char -> List Char
charRange from to =
  List.map Char.fromCode [(Char.toCode from)..(Char.toCode to)]

charRange 'a' 'd' -- ['a','b','c','d'] : List Char

Edit, from elm 0.18 and up, List.range is finally a function:

charRange : Char -> Char -> List Char
charRange from to =
  List.map Char.fromCode <| List.range (Char.toCode from) (Char.toCode to)
Andreas Hultgren
  • 14,763
  • 4
  • 44
  • 48
  • 7
    The `[ .. ]` syntax was removed in Elm 0.18, so now you need to replace any instances of `[a .. b]` with `List.range a b`. Note that the type of `List.range` is `Int -> Int -> List Int`, so it also will not work with characters. – Conrad Parker Aug 24 '17 at 05:48