Execute Brain****/OCaml: Difference between revisions

Content added Content deleted
m (<lang>)
m (Categorization now in master page)
Line 1: Line 1:
{{implementation|Brainf***}}{{collection|RCBF}}[[Category:OCaml]]
{{implementation|Brainf***}}{{collection|RCBF}}
Quick implementation of a [[Brainfuck]] interpreter in OCaml.
Quick implementation of a [[Brainfuck]] interpreter in [[OCaml]].


Like the Haskell version but without the lazy lists:
Like the [[Haskell]] [[RCBF/Haskell|version]] but without the lazy lists:


Pairs of lists are used to implement both the two-side infinite band of cells, and the program storage.
Pairs of lists are used to implement both the two-side infinite band of cells, and the program storage.
Line 10: Line 10:
A more efficient implementation could for example only admit well-bracketed brainfuck programs, and parse bracket blocks first, to replace the ''match_left'' and ''match_right'' which need linear time.
A more efficient implementation could for example only admit well-bracketed brainfuck programs, and parse bracket blocks first, to replace the ''match_left'' and ''match_right'' which need linear time.


<lang ocaml>
<lang ocaml>let move_left (x::l, r) = (l, x::r)
let move_left (x::l, r) = (l, x::r)
let move_right (l, x::r) = (x::l, r)
let move_right (l, x::r) = (x::l, r)


Line 67: Line 66:
exec (match_left (move_left p)) d
exec (match_left (move_left p)) d


let run s = exec ([], s) ([0], [0])
let run s = exec ([], s) ([0], [0])</lang>
</lang>


Example output:
Example output: