Silly/fun/cool Lisp code snippets!!

Started by
445 comments, last by Tron3k 19 years, 3 months ago
Earlier in the year I had to write a Scheme program to cross reference Scheme code.

It's not really crazy, or a few lines but I thought it was pretty cool since I didn't have a computer when I wrote it and was pretty impressed with myself when I was able to transcribe it onto a computer and have it run perfectly.
Advertisement
Quote:Original post by twix
Quote:Original post by Tron3k
BTW, what I'm wondering about that Quest code is: Is it the appropriate thing to write a macro for? Is it really just a data structure, rather than code? I think the macro way would be more flexible. It's just that I haven't thought of how it would be more flexible yet - I'm sure it will work out though.

That's not something you need to worry about, if you're planning to follow the bottom-up construction paradigm. In the end almost everything important in Lisp ends up in a tree-like data structure of some sort. All you really have to worry about is, "how can I express a quest in a way that would be understandable to a three-year-old?", and then write the appropriate macros that allow Lisp to understand it as well. [smile]

Then as your quest-language requires more complex features to express more complex ideas, you can capitalize on Lisp's amenability to change.
Oh man, you're right! I'm still thinking in C++, lol!
“[The clergy] believe that any portion of power confided to me, will be exerted in opposition to their schemes. And they believe rightly: for I have sworn upon the altar of God, eternal hostility against every form of tyranny over the mind of man” - Thomas Jefferson
is there "Thinking in Lisp"?

Beginner in Game Development?  Read here. And read here.

 

Quote:Original post by Alpha_ProgDes
is there "Thinking in Lisp"?

Yes.
One can write games in Lisp:
http://www.franz.com/success/customer_apps/animation_graphics/naughtydog.lhtml
maybe. but how does one write a game in Lisp? [grin]
is the real question.

Beginner in Game Development?  Read here. And read here.

 

Quote:Original post by Alpha_ProgDes
maybe. but how does one write a game in Lisp? [grin]
is the real question.

Pretty much the same way you write a game in any other language, only cooler. [wink]

I think there's already a SDL/OpenGL interface for Common Lisp floating around somewhere, so there's nothing particularly hard about it as long as you don't mind starting without a pre-built engine.
FFI needn't be that scary.

Suppose your implementation's FFI looks like this:
(def-function glVertexPointer"   ((size    :GLint)   (type    :GLenum)   (stride  :GLsizei)   (pointer (* :GLvoid :const)))  :returning :void)(

The beauty of Lisp is that you can make macros that make them look like this:
(extern "C" void glVertexPointer (GLint size) (GLenum type) (Glsizei stride) (const GLVoid * pointer)

In fact, you can do something that is, from a certain point of view, even better.

Lisp lets you redefine the syntax of the language in your source. You could redefine s/<pattern>/<replacement>/ to be a function that does what s// does in Perl, for example. You could also augment the parser so that you could do, e.g:
#{cdef{void glVertexPointer( GLint size,                      GLenum type,                      GLsizei stride,                      const GLvoid *pointer );}}

You could even solve the problem many opponents of Lisp have with its paranthesis:
(defun fac (n)    (do ((i 1 (1+ i))         (x 1))        ((> i n) x)        (setf x (* x i))))

Could be written as:
(load "sintax.lisp")defun fac (n)  do ((i 1 (++ i)      (x 1))    ((i > n) x)    (setf x (x * i))

Here the rule is that a line followed by an indent is taken as beginning with a (, and a line followed by an outdent is taken as ending with a ). Also, we've had the reader do a bit of magic with operators, and added a ++ operator.

As a slightly less extreme example:
(set-macro-character #\/  (let ((old-readtable (copy-readtable *readtable*)))       #'(lambda (stream char)           (declare (ignore char))           (case (peek-char nil stream)                 (#\/ (peek-char #\Newline stream))                 (#\* (read-char stream)                      (peek-char #\* stream)                      (do () ((eq #\/ (peek-char nil stream)) (values))                             (peek-char #\* stream)                             (read-char stream))                      (read-char stream))                 (otherwise   (let ((*readtable* old-readtable))                    (read (make-concatenated-stream (make-string-input-stream "/")                                                    stream))))))))

C/C++ style singleline and multiline comments.

[Edited by - Mayrel on December 1, 2004 10:21:49 PM]
CoV
(aftermath (aftermath aftermath) aftermath!)    (aftermath->(aftermath, aftermath)(aftermath()())        aftermath'aftermath()
Rate me up.
Another example of messing with the parser:
(set-syntax-from-character #\{ #\{)(set-syntax-from-character #\} #\))(set-syntax-from-character #\[ #\()(set-syntax-from-character #\] #\))

This simply defines {} and [] to mean the same thing as ().
{defun fact [n]  {if (= 1 n)      n      (* n (fact n))  }}

Which might help you if you're used to C syntax.
CoV

This topic is closed to new replies.

Advertisement