Execute Brain****/Standard ML: Difference between revisions

m
Fixed syntax highlighting.
(translated from haskell)
 
m (Fixed syntax highlighting.)
 
(2 intermediate revisions by 2 users not shown)
Line 1:
{{implementation|Brainf***}}{{collection|RCBF}}[[Category:Standard ML]]
Quick implementation of a [[Brainfuck]] interpreter in [[Standard ML]].
 
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.
Line 10:
A more efficient implementation could for example only admit well-bracketed brainfuck programs, and parse bracket blocks first, to replace the ''matchLeft'' and ''matchRight'' which need linear time.
 
<langsyntaxhighlight lang="sml">fun moveLeft (x::l, r) = (l, x::r)
fun moveRight (l, x::r) = (x::l, r)
 
Line 43:
| exec (p as (_, #"]"::_), d ) = exec (matchLeft (moveLeft p), d)
 
fun run s = exec (([], s), ([0], [0]))</syntaxhighlight>
</lang>
 
Example output:
9,485

edits