forked from granule-project/granule
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parse.gr
38 lines (29 loc) · 985 Bytes
/
Parse.gr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import Char
import Maybe
-- takeWhile : (a -> (Bool, a)) -> VecX a -> (VecX a, VecX a)
parseInt : String -> (Maybe Int, String)
parseInt str = case stringUnsnoc str of
None -> (None, ""); -- fail on empty string
Some (init,c) -> parseIntInner (stringSnoc init c)
parseIntInner : String -> (Maybe Int, String)
parseIntInner str = case stringUnsnoc str of
None -> (0, "");
Some (str,c) -> case digitToInt c of
Left c -> (None, stringSnoc str c);
Right n -> case parseIntInner str of
parseIntInner init c
case [digitToInt [c]] of
[None] -> None;
[Some n] -> case parseInt [init] of
None -> None;
Some m -> Some (n + (m * 10))
parseInt : String [0..1] -> Maybe Int
parseInt [str] = case [stringUnsnoc str] of
[None] -> Some 0;
[Some (init,c)] -> case [digitToInt [c]] of
[None] -> None;
[Some n] -> case parseInt [init] of
None -> None;
Some m -> Some (n + (m * 10))
-- main : Maybe Int
-- main = parseInt ["123456"]