summaryrefslogtreecommitdiff
path: root/Fractran/Interpreter.hs
blob: b62f2794ae53fb2a0cf6f4d1e8bcf3ef820de7be (plain)
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
module Interpreter (
    fractran
	) where


import Parser




fractran :: FractranProgram -> [Int]
fractran program =
    let prog = map (\(x,y) -> (fromIntegral x, fromIntegral y)) (fractions program)
        f = (\p v -> if (p == [])
                     then []
                     else let (curX, curY) = head p
                              newV = v * curX / curY
                          in if (isInt newV)
                             then newV : (f prog newV)
                             else f (tail p) v)
        result = map round (f prog (fromIntegral (initialValue program)))
    in (initialValue program) : result



isInt :: (RealFrac a) => a -> Bool
isInt x =
    x == fromInteger (round x)