tag:blogger.com,1999:blog-978780246348321177.post2659372621696326944..comments2010-02-13T09:55:50.602+01:00Comments on my sabbatical year diary: Brainfuck interpreter in HaskellUnknownnoreply@blogger.comBlogger4125tag:blogger.com,1999:blog-978780246348321177.post-68485584577943996772010-02-13T09:55:50.602+01:002010-02-13T09:55:50.602+01:00Amiable brief and this mail helped me alot in my c...Amiable brief and this mail helped me alot in my college assignement. Thank you on your information.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-978780246348321177.post-9235113662309618882008-01-09T21:31:00.000+01:002008-01-09T21:31:00.000+01:00Hi lasts!Thanks for comments. Your english is not ...Hi lasts!<BR/><BR/>Thanks for comments. Your english is not bad at all, especially that my mother's tongue is polish :)<BR/><BR/>I did some experiments (they yet have to be posted on this blog) with zipper, but it turned out to be slower than using IOUArray, probably mostly due to IOUArray items being unboxed.<BR/>It is still a great technique and definitely worth remembering.<BR/><BR/>As for preparsing, I made some esperiment (also not part of the blog yet) with preparsing and compacting code (for example "+++++" is translated to Add 5, "<<<" translated to "Move -3" etc) and using pattern matching. To my disappointment it turned out to be twice slower than current solution (with ByteString for program and parsing char-by-char).<BR/><BR/>Sometimes Haskell gives some unintuitive results...brzozanhttps://www.blogger.com/profile/13083180718801055848noreply@blogger.comtag:blogger.com,1999:blog-978780246348321177.post-34045670874215318112008-01-09T05:59:00.000+01:002008-01-09T05:59:00.000+01:00It looks like someone has already told you about t...It looks like someone has already told you about the zipper on reddit a way better than me :] .<BR/><BR/>I was a bit curious about how to handle the side-effect of brainfuck properly. So here's my version :<BR/><BR/>http://hpaste.org/4874<BR/><BR/>$ ghc brainfuck.hs -o bf<BR/><BR/>$ time (echo 10 | ./bf PRIME.BF) <BR/>Primes up to: 2 3 5 7 <BR/>(; echo 10 | ./bf PRIME.BF; ) 0,02s user 0,00s system 84% cpu 0,019 total<BR/><BR/>$ time (echo 20 | ./bf PRIME.BF) <BR/>Primes up to: 2 3 5 7 11 13 17 19 <BR/>(; echo 20 | ./bf PRIME.BF; ) 0,13s user 0,00s system 95% cpu 0,134 total<BR/><BR/>$ time (echo 30 | ./bf PRIME.BF) <BR/>Primes up to: 2 3 5 7 11 13 17 19 23 29 <BR/>(; echo 30 | ./bf PRIME.BF; ) 0,45s user 0,02s system 99% cpu 0,472 total<BR/><BR/>I really like that kind of articles, thanks for sharing your experiments because that's always a source of inspiration :)Unknownhttps://www.blogger.com/profile/03002119606530759639noreply@blogger.comtag:blogger.com,1999:blog-978780246348321177.post-2827198942107154532008-01-09T04:09:00.000+01:002008-01-09T04:09:00.000+01:00First of all, I apologize for my english, but that...First of all, I apologize for my english, but that's an interesting topic :)<BR/><BR/>I think you should use a zipper in order to handle the memory - and parse the code before the execution ^_^<BR/><BR/>I can't find a good link for zipper list, but the basic idea looks like :<BR/><BR/>> data Zipper a = Zipper [a] [a]<BR/><BR/>> get (Zipper _ (y:_)) = y<BR/>> update (Zipper xs (_:ys)) newY = Zipper xs (newY:ys)<BR/>> incrPointer (Zipper xs (y:ys)) = Zipper (y:xs) ys<BR/>> decrPointer (Zipper (x:xs) ys) = Zipper xs (x:ys)<BR/><BR/>> listToZipper li = Zipper [] li<BR/>> zipperToList (Zipper xs ys) = (reverse xs) ++ ys<BR/><BR/>The zipper offer you a O(1) access to the cell (while (!!) is O(N)).<BR/><BR/>Parsing the code before you a more handy way to handle the loops.<BR/><BR/>> type BFCode = [BFWord]<BR/>> data BFWord = IncrValue<BR/>> | DecrValue<BR/>> | IncrPointer<BR/>> | DecrPointer<BR/>> | Print<BR/>> | Read<BR/>> | Loop BFCode<BR/><BR/>As such, most of the remaining functions are just one-one with the zipper's functions ^_^<BR/><BR/>I wonder how this can influence on your benchmarks - because you're actually solving the performance problem with other tools... so please continue your experiments :)Unknownhttps://www.blogger.com/profile/03002119606530759639noreply@blogger.com