Range Expressions in Elm

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

+8
elm
source share
1 answer

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

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

Change 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 higher, 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) 
+17
source share

All Articles