
CHIP-8 in Common Lisp: Graphics - stevelosh
http://stevelosh.com/blog/2016/12/chip8-graphics/
======
mrspeaker
This has been a good intro to Common Lisp, as I've been comparing things to my
JavaScript implementation of CHIP-8 ( _cough_
[http://mrspeaker.github.io/CHIMP-8/](http://mrspeaker.github.io/CHIMP-8/)
_cough_ ) - but I'm having a difficult time parsing the `iterate` and `repeat`
and `for` statements in there: what's the difference between them - and how
does `for` relate to a C/JavaScript `for`?

~~~
stevelosh
If you've never done Common Lisp before, it's better to start by looking at
`loop`, which is a built-in macro that provides a bunch of different ways to
loop/iterate: [http://www.gigamonkeys.com/book/loop-for-black-
belts.html](http://www.gigamonkeys.com/book/loop-for-black-belts.html)

iterate is a Common Lisp library that aims to be a replacement for loop, being
cleaner (subjectively) and portably extensible: [https://common-
lisp.net/project/iterate/](https://common-lisp.net/project/iterate/)

`repeat` is an iterate clause that tells iterate to perform N iterations of
the loop:

    
    
        (iterate (repeat 3) (print 'beep))
        BEEP
        BEEP
        BEEP
    

As for this particular `for`, it's an iterate clause that handles numeric
iteration (among other things). So:

    
    
        (iterate (for x :from 0)
                 (for y :from 10 :downto 7)
                 (print (list x y)))
    

Would give you:

    
    
        (0 10)
        (1 9)
        (2 8)
        (3 7)
    

iterate will terminate the loop the first time any one of its clauses runs out
of values/iterations. So in the post I had something like

    
    
        (iterate (repeat N)
                 (for x :from start)
                 ...)
    

You could write this as

    
    
        (iterate (for x :from start :below (+ start N))
                 ...)
    

But manually calculating the ending value seems ugly to me. I know I want the
loop to happen N times so I just say `(repeat N)` and let the computer figure
out when it's time to be done.

~~~
SixSigma
I cant remember the original or who I'm almost quoting but it goes something
like this :

> `loop` is so complex, LISP programmers invented functional style so they
> didn't have to learn how to use it

------
qwertyuiop924
This, more than any advocacy from CLers, has shown me why I might prefer CL
over Scheme.

~~~
Kinnard
Please elaborate

~~~
qwertyuiop924
Certainly.

Scheme is inarguably a more elegant language than CL (for the most part:
dynamic variables are an ugly hack). However, CL has tremendous practical
benefits: The condition system, the large language, and so on.

CLers usually push the elegance and the cleanness of their language as a major
benefit. And it is: a clean language makes programming easier. but Scheme is
far cleaner than CL. And thus where CL beats Scheme is practicality, which
most CL advocates don't discuss.

OTOH, this article discusses some of the features of CL that make it
immediately practical: a type system that lets you specify sizes if you want
to, so that you can do low-level bit hacking if necessary, a standard lowlevel
macro system (not as nice as synclos, but what can you do...), and a wealth of
useful libraries. This is something that, as of yet, Scheme cannot match, and
may never.

------
inyorgroove
Previous HN discussion:
[https://news.ycombinator.com/item?id=13213304](https://news.ycombinator.com/item?id=13213304)

~~~
adrianm
That's an earlier post in the same series.

~~~
kahrkunne
What a thing to downvote someone over though, posting something useful but
with a minor mistake like this (not saying you downvoted him of course)

~~~
dang
Please just give a corrective upvote and move on. That nearly always works (as
it has above).

~~~
TeMPOraL
Good to know the practice of corrective upvotes is recognized by the mods :).

