Blog-o! Notes from latte.ca

Wed, 09 Apr 2008

As I mentioned before, I’ve ported Gambit Scheme to my iTouch, and have been playing around with it a little. It’s pretty nice all in all, but I recently ran into a small problem while I was trying to play around with macros. The problem? There’s no way to enter a backtick (`) on the iTouch! That means that I don’t really have a way to write code like

1
`( a b ,(+ 1 2) d)

which makes writing macros a lot more painful. Fortunately, I got a lot of help from the people on IRC and on the Gambit mailing list. Specifically, Marc Feeley, the author of Gambit, posted a snippet of code that I could put into my .gambcini file that would add $ as a synonym for `. The code looked like this:

1
2
3
4
5
6
7
(begin
  (##readtable-char-class-set!
    (current-readtable)
    #\$  ;; the character to dispatch on
    #t   ;; this character is a delimiter
    (lambda (re c) (##read-quotation re #\`)))  ;; handler
  #f)

and the example, which works, is:

1
2
$(1 ,(+ 2 3) 4)
output: (1 5 4)

If you’re trying to write that code on your iTouch, you might notice that it includes the forbidden `, and so you’re once again out of luck. Except that in this case, you can replace #\` with #\u0060, which you can type in on the iTouch, and then it’ll all work.

[Posted at 10:20 by Blake Winton] link
Wed, 09 May 2007

I mentioned in the previous entry that I was missing Python's List Comprehensions. Well, I've gotten a little closer to having them. For some reason, LispMe doesn't come with a zip method, and you can only get map by importing a "Standard Library" memo. So, I had to write my own version of zip, and here it is, for anyone else who might find it useful.

1
2
3
4
(define (zip s1 s2)
  (if (or (null? s1) (null? s2)) '()
    (cons (list (car s1) (car s2))
      (z (cdr s1) (cdr s2)))))

Along with my halfway function, redefined to be (define (halfway x) (/ (+ (car x) (cadr x)) 2)), I can now write (map halfway (zip current-point next-point)) to get the point halfway between where I am, and where I am going to.

I also came to another realization. I was planning on defining a current-point, and using set! to update it to the new halfway point, but when I think about it, I don't really care what the current point is at any time other than processing, so there's no particular need to set it, and instead I should just pass it around as a parameter, making my iter function (and I just made up the name "iter function", based on the <a href= "http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-10.html#%_sec_1.1.7"

sqrt-iter function in "Structure and Interpretation of Computer Programs") look something like this:

1
2
3
4
5
6
(define (triangle-iter start num-iters)
  (if (positive? num-iters)
    (begin (draw-point start)
    (triangle-iter
      (map halfway (zip current-point next-point))
      (- num-iters 1)))))

Does that look appropriately Scheme-ish, do you think?

[Posted at 17:29 by Blake Winton] link

(Posted to the LispMe@YahooGroups.com list) Hello.

Let me start with a brief introduction. My name is Blake Winton, and I’ve owned a PalmPilot since back when they were called PalmPilots and made by USRobotics. I’ve been programming for them for years, mostly in C and C++, with brief excursions into Lua, Python, and Forth. My day job consists mainly of Java (J2EE), with some Python when I get the chance, and some JavaScript when I shouldn’t be using Python. On my off hours, I read a lot about other languages (recently Objective C, OCaml, and Ruby). Now I want to learn Scheme, and thought that LispMe would be a good way to do it. I’m finding some things are tripping me up, mainly due to my attempts to transfer my knowledge from other areas into LispMe. (One piece I miss in particular are something like Python’s List Comprehensions, which I believe they sole from Haskell.)

So, I’m going through SICP, but while I’m doing that, I thought I would try to do a "Real World" (tm) task, and write a program that drew the Serpinsky Triangle, using an iterative, random, approach, (Details available upon request,) and I’ve run into some small questions about best practices, or basically how to do some simple things. In return for this help, I’ll create a sort of Tutorial document for LispMe that will be able to help other people get up to speed. (If such a document already exists, please, someone, point me to it!)

So, here’s what I’ve got so far. It doesn’t work at all, but it’s starting to take shape.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
; Triangle
(define points
  #((80,10) (10,150) (150,150)))

(define (next-point p) (
  (vector-ref points
    (random (vector-length points)))
  ))

(define current-point '(80 80) )
(define (halfway a b) (/ (+ a b) 2))

And we’re done. Any comments on it, from spacing to indentation to whether I should use a vector or a list for the points themselves, would be greatly appreciated. If it matters, I plan on extending the point to include red, green, and blue data as well, possibly with accessors, looking something like this:

1
2
3
4
5
6
(define current-point '(80 80 255 0 0))
(define (x pt) (car pt))
(define (y pt) (cadr pt))
(define (r pt) (caddr pt))
(define (g pt) (cadddr pt))
(define (b pt) (caddddr pt))

Oh, and I suspect I’ll be blogging my progress at http://weblog.latte.ca/blake/tech/scheme/ (Nothing exists there at the moment, though. Give it time.)

Thanks,
Blake.

[Posted at 17:28 by Blake Winton] link