-
Notifications
You must be signed in to change notification settings - Fork 4
/
paper.hs
executable file
·103 lines (81 loc) · 2.02 KB
/
paper.hs
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
read_arg1 = Act (#(cline_arg) " ")
str_elem :: String -> Int -> Int
str_elem s i = #(__Elem) s i
strlen :: String -> Int
strlen s = #(__Len) s
numbers :: [Int]
numbers =
let num n = n : num (n + 1)
in num 0
take :: Int -> [a] -> [a]
take n l =
if n == 0 then []
else
case l of
[] -> []
h:t -> h : take (n - 1) t
factA :: Int -> Int -> Int
factA a n =
if n < 2 then a
else factA (a * n) (n - 1)
map :: (a -> b) -> [a] -> [b]
map f l =
case l of
[] -> []
h:t -> f h : map f t
factorials :: [Int]
factorials = map (factA 1) numbers
fromStringI :: Int -> Int -> Int -> String -> Int
fromStringI i limit acc s =
if limit == i then acc
else if limit < i then acc
else
fromStringI (i + 1) limit (acc * 10 + (str_elem s i - 48)) s
fromString :: String -> Int
fromString s = fromStringI 0 (strlen s) 0 s
-- delete this, and defs of div and mod below it when
-- new binary built
x < y =
let testi i = if x + i == y then True
else if y + i == x then False
else testi (i + 1)
in
if x == y then False else testi 1
div m n = if m < n then 0 else 1 + div (m - n) n
mod m n = if m < n then m else mod (m - n) n
f $ x = f x
s1 ++ s2 = #(__Concat) s1 s2
implode l =
case l of
[] -> ""
h:t -> #(__Implode) h ++ implode t
toString0 :: Int -> [Int]
toString0 i =
if i == 0 then []
else (mod i 10 + 48) : toString0 (div i 10)
reverse :: [a] -> [a]
reverse l =
let revA a l =
case l of [] -> a
h:t -> revA (h:a) t
in
revA [] l
toString :: Int -> String
toString i =
if i < 0 then "-" ++ (implode $ reverse $ toString0 (0-i))
else if i == 0 then "0"
else implode $ reverse $ toString0 i
print s = Act (#(stdout) (s ++ "\n"))
return v = Ret v
app :: (a -> IO b) -> [a] -> IO ()
app f l =
case l of
[] -> return ()
h:t -> do f h
app f t
main = do
arg1 <- read_arg1
-- fromString == 0 on malformed input
let i = fromString arg1
facts = take i factorials
app (\i -> print $ toString i) facts