Skip to content

Instantly share code, notes, and snippets.

@TheSeamau5
Created April 22, 2015 03:07
Show Gist options
  • Save TheSeamau5/c062c213b63f2d28fd1e to your computer and use it in GitHub Desktop.
Save TheSeamau5/c062c213b63f2d28fd1e to your computer and use it in GitHub Desktop.
Extract digits from a float
import String exposing (toList)
import Maybe
import List exposing (member)
import Graphics.Element exposing (show)
toChar x =
if | x == 0 -> '0'
| x == 1 -> '1'
| x == 2 -> '2'
| x == 3 -> '3'
| x == 4 -> '4'
| x == 5 -> '5'
| x == 6 -> '6'
| x == 7 -> '7'
| x == 8 -> '8'
| x == 9 -> '9'
| otherwise -> ' '
fromChar x =
if | x == '0' -> 0
| x == '1' -> 1
| x == '2' -> 2
| x == '3' -> 3
| x == '4' -> 4
| x == '5' -> 5
| x == '6' -> 6
| x == '7' -> 7
| x == '8' -> 8
| x == '9' -> 9
| otherwise -> -1
digits : Float -> List Int
digits float =
let chars : List Char
chars = toList (toString float)
fromChars : List Char -> List (Maybe Int)
fromChars chars = case chars of
[] -> [ ]
x :: xs ->
if | member x (List.map toChar [0..9]) -> Just (fromChar x) :: fromChars xs
| x == '.' -> fromChars xs
| otherwise -> [ Nothing ]
in
List.map (Maybe.withDefault -1) (fromChars chars)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment