Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Thinking in Lisp

Name: Anonymous 2012-02-04 20:31

SQL, Lisp, and Haskell are the only programming languages that I've seen where one spends more time thinking than typing.
That's because it takes forever to think of the solution in Lisp and Haskell as opposed to a decent language. Faggot lispers will spend most of their time figuring out how best to abuse recursion because they think it makes them leet programmers or some shit.

Name: Anonymous 2012-02-05 12:01

>>19
When a macro expansion contains an expression passed to the macro, you usually only want it to be evaluated once. If the expansion contains (f x x), for example, where x is (g y), instead of expanding to (f (g y) (g y)), you need (let ((x (g y))) (f x x)). The problem with that is that the name x might already be used, so the new binding will shadow the old one and break your code.

The usual way to get around this is to generated a unique name for the temporary variable using gensym, so instead of

(defmacro m (x)
  `(... x ... x ...))


you would write

(defmacro m (x)
  (let ((y (gensym)))
    `(let ((,y ,x))
       (... ,y ... ,y ...))))


That's a lot of boilerplate, and repeated code is usually a bad thing, so once-only lets you do this:

(defmacro m (x)
  (once-only (x)
    `(... x ... x ...)))

Name: Anonymous 2012-02-05 12:03

>>20
Shit, x should be ,x in the first and third examples.

Name: not >>17 2012-02-05 12:08

>>19
It's a macro which is usually used when writing code generating code (that is, other macros). It generates code that bins some symbols of your choosing to only be evaluated once (this works by generating a let form and doing a very clever form of substition of the bound variables with gensyms), without it, if your macro was passed some state-changing code, or context-dependent code (such as by dynamic bindings and whatnot), it could end up being evaluated more than once and generate the wrong code.
I'll try to give a C example if you're not familiar with Lisp, you have a macro which takes one argument, this argument is supposed to be something that can be evaluated, but cannot be expected to always return the same value, such as i++, yet you only want to use the value of that expression once, not subtitute a i++ in each place you've used that macro argument. You can fix this in C by just creating a local variable, assigning your i++ to it and hoping the macro doesn't use your local variable in special ways (such as some symbol conflict) - it's not really possible to do it right with just text substition, you'd need access to the AST to even have a chance of getting it ``right'' and not just a ``very low chance of failure''.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List