Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It's called Lisp. I hate to be Yet Another Hacker News Lisp Punditparrot, but it's true.

For example, take a look at how easy it is to implement BrainF inside Racket, a lisp focused on language experimentation. http://hashcollision.org/brainfudge/ I bet you could walk through these steps in maybe half an hour with little to no prior experience.



How would I go about setting up Racket as a backend? Ie something for my JS or whatever to send requests to in my custom DSL.


eh easy? that's one of the longest pages on the Internet! Here's a BrainF interpreter in 1 line of Python: http://www.cs.princeton.edu/~ynaamad/misc/bf.htm

Granted it expands to more than one line, but I'm not seeing that as a good example of lisp's superiority.


It's absolutely not the same thing : the Racket example is not a Brainfuck interpreter, it's an actual DSL inside Racket, which benefits from it's whole infrastructure (JIT compilation etc.). It's not the Racket language running a BF interpreter. It's the BF language running.


> Granted it expands to more than one line

Does it? It's a single semicolon-free Python expression. Looks like one line to me.

Besides, that is a terrible example of a "good" Brainfuck interpreter. The Scheme example is easily 100x easier to understand (/debug/troubleshoot/extend/etc).


I hold that it's far easier to read and step through Danny Yoo's BrainF interpreter than to try and piece together your one-line pythonic monstrosity.

Of course, if you're arguing that your Python version is somehow better than the longer idiomatic Racket implementation, I think we both agree that leveling the playing field would create a more fair comparison. Heavens, wouldn't it make more sense to compare a one-line BF interpreter written in Racket to your one-line BF interpreter written in Python? ;)

Two can play this game:

  #lang racket (require racket/match) (let* ([data "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."] [move-left (λ (left cur right) (let ([right (if (null? right) '(0) right)]) (list (cons cur left) (car right) (cdr right))))] [move-right (λ (left cur right) (let ([left (if (null? left) '(0) left)]) (list (cdr left) (car left) (cons cur right))))] [inc (λ (left cur right) (list left (add1 cur) right))] [dec (λ (left cur right) (list left (sub1 cur) right))] [consume-one-char (λ (char state) (match char [#\+ (apply inc state)] [#\- (apply dec state)] [#\< (apply move-left state)] [#\> (apply move-right state)] [#\. (begin (display (integer->char (cadr state))) state)] [#\, (list (car state) (char->integer (read-char)) (caddr state))] [else state]))]) (let loop ([counter 0] [prev-counters '()] [state (list '() 0 '())]) (if (= counter (string-length data)) state (let ([char (string-ref data counter)]) (match char [#\[ (loop (add1 counter) (cons counter prev-counters) state)] [#\] (if (zero? (cadr state)) (loop (add1 counter) (cdr prev-counters) state) (loop (car prev-counters) (cdr prev-counters) state))] [else (loop (add1 counter) prev-counters (consume-one-char char state))])))))
Both this example and your one-line python example are similarly incomprehensible. While we're on the topic of meaningless benchmarks, please allow me to point out that my racket version is 163 bytes shorter than your python version, without trying to abbreviate variable names and without smashing whitespace. That's even including the "Hello World" BF program near the beginning of the source.

Ok, yes, you have a point -- the article I cited earlier is long and focuses on Racket's awkward tooling. However, my point is that we're comparing apples to oranges here. It would be better to compare Danny's article to a Python article that implements a BF interpreter, covers some optimizations, and describes how to package it as an Egg and upload it to PyPi. None of these topics are trivial, especially to a beginner, hence Danny's thurough coverage.


Nice. Is it possible to cross-compile it to other languages?


I... don't think so. Not in the way you're thinking of, anyway. In this BrainF example, your language "compiles" (translates, really) to Racket expressions; then, the Racket VM "compiles" it to Racket bytecode just before running it.

For something as simple as BrainF, it's not hard to write your own translator. Just replace every [ with while (* ptr) {, replace + with (* ptr)++; replace < with ptr--; and so on. Then it's essentially a BrainF-to-C translator.

It's not so simple for racket in the general case. Danny Yoo is working on "Whalesong", a javascript interpreter that interprets racket bytecode so you can run racket in your web browser. http://hashcollision.org/whalesong/ It's still not a Racket->Javascript translator though.

I'm not aware of similar projects for other languages.

(Edit: formatting ate my asterisks)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: