Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Range expressions in Elm

Tags:

elm

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?

like image 252
rausch Avatar asked Jul 05 '16 06:07

rausch


Video Answer


1 Answers

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)
like image 162
Andreas Hultgren Avatar answered Sep 18 '22 05:09

Andreas Hultgren