Execute Brain****/Haskell: Difference between revisions
Content added Content deleted
mNo edit summary |
(unsafePerformIO sounds scary) |
||
Line 4: | Line 4: | ||
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). |
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). |
||
In functional style, ''run'' interprets a Brainfuck program as a function from an Integer list (inputs) to an Integer list (outputs). |
In functional style, ''run'' interprets a Brainfuck program as a function from an Integer list (inputs) to an Integer list (outputs). This can be easily turned into a real dialog with a user via ''stdin'' and ''stdout''. |
||
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. |
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. |
||
import System.IO.Unsafe |
|||
moveLeft (x:l,r) = (l,x:r) |
moveLeft (x:l,r) = (l,x:r) |
||
moveRight (l,x:r) = (x:l,r) |
moveRight (l,x:r) = (x:l,r) |
||
Line 45: | Line 43: | ||
dialog :: String -> IO () |
dialog :: String -> IO () |
||
dialog s = mapM_ print |
dialog s = mapM_ print . run s . map read . lines =<< getContents |
||
Example session: |
Example session: |