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

m
Fixed syntax highlighting.
No edit summary
m (Fixed syntax highlighting.)
 
(2 intermediate revisions by 2 users not shown)
Line 1:
{{implementation|Brainf***}}{{collection|RCBF}}[[Category:Haskell]]
Quick implementation of a [[Brainfuck]] interpreter in [[Haskell]].
 
Pairs of lists are used to implement both the two-side infinite band of cells, and the program storage. This means that it can also work on infinite Brainfuck programs (which could be generated lazily).
Line 8:
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="haskell">module BF where
 
moveLeft (x:l,r) = (l,x:r)
Line 46:
dialog :: String -> IO ()
dialog s = mapM_ print . run s . map read . lines =<< getContents
</langsyntaxhighlight>(This version compiles with GHC and will run if loaded into ghci using ':load BF')
 
Example session:
9,482

edits