Random Access Machine written in Haskell
This Random Access Machine has 5 instructions.
Set 0
to register R(i)
Increment register R(i)
by 1
Assign value of R(j)
to R(i)
Jump to k
th instruction if value of R(i)
and R(j)
are equal
(NOTE: instruction is one origin)
Halt program
module Main where
import RandomAccessMachine
import Control.Monad.State
sub1Prog = Program
[ J(1, 2, 5)
, S(2)
, S(0)
, J(0, 0, 1)
, E
]
main :: IO ()
main = do
let (res, env) = execProgram sub1Prog [7, 4] -- 7 - 4
print res
-- => 3
let (res, env) = execProgram sub1Prog [87, 23] -- 87 - 23
print res
-- 64
stack build && stack exec random-access-machine-examples