Posts Tagged ‘Scheme’

Continuations for curmudgeons

April 24, 2005 Leave a comment

Sam Ruby has a much praised blog entry entitled “Continuations for Curmudgeons“.

I didn’t like the tone of the article which seem to treat the C programmers as some kind of dinasaur with no interest in other languages (or language features).

There was no mention of Scheme. When learning about continuations I think it’s best to learn from the source. That source appears to be the Scheme programming language. R5RS includes call-with-current-continuation function usually shortened to call/cc. A really good introduction to Scheme with coverage of continuations for non-trivial applications is contained in Dorai Sitaram’s Teach Yourself Scheme in Fixnum Days (TYSiFD).

Sam seems to have some confusion about continuations and there relationship to closures, iterators, generators and coroutines. Just because a language has a “yield” keyword doesn’t mean it has first class continuations. Yield is usually used for generators. One of the highlights of TYSiFD is implementing coroutines with continuations.

Ruby supports first class continuations. The callcc method is available from the Kernel class. I played around with the code from the Ruby documentation of the Continuation class. You really don’t need call/cc for the contrived example there:

original_guys = ["Freddie", "Herbie", "Ron", "Max", "Ringo"]

puts "method 1: (first-class) continuation"
guys = original_guys.clone
puts(message = guys.shift)
$ unless message =~ /^R/;

puts "method 2: continuation with different way of setting global"
guys = original_guys.clone
callcc{|k| $k = k}
puts (message = guys.shift)
$ unless message =~ /^R/;

puts "method 3: just a loop"
guys = original_guys.clone
loop {
  message = guys.shift
  puts message
  if (message =~ /^R/)

puts "method 4: (tail) recursion using methods/functions"
puts "          Ugly - needs guys to be global variable."
$guys = original_guys.clone
def myloop
  message = $guys.shift
  puts message
  unless message =~ /^R/

puts "method 5: (tail) recursion using blocks"
guys = original_guys.clone
myloop ={
  message = guys.shift
  puts message
  unless message =~ /^R/

puts "method 6: using each and catch/raise"
guys = original_guys.clone
catch (:early_exit) {
  guys.each {|item|
    puts item
    if item =~ /^R/
      throw :early_exit

Movitz – a Lisp OS platform

September 27, 2004 Leave a comment

Wow. You hear alot of rumours about Lisp-based operating systems but you rarely come across a project that appears active. Movitz looks like a promising *active* LispOS project! It’s goal is to provide a development platform to run CL-based kernels on x86 PCs “on the metal”. I guess these restrictions make it doable. I’d prefer to see CL or Scheme combined with Linux or FreeBSD kernel so that you could still have a viable usable system and also experiement with OS ideas using lisp. In this respect, Schemix looks interesting. It’s a Scheme interpreter (based on TinyScheme) patched into the Linux kernel. I haven’t looked at it closely but imagine that it’s interpreted nature means that it is probably only useful for early prototyping of ideas and certainly for exploring and learning about the kernel.

Abelson and Sussman lectures

August 30, 2004 Leave a comment

I finished the SICP videos last weekend (although I need to return to the last 4). I wanted to get back to finish watching a lecture webcast series for the subject Operating Systems and Systems Programming from Berkeley. I’ve seen a previous years lecture series but I’m pretty sure I didn’t finish. It’s all in Real media format so you need RealPlayer. It’s not so good on dialup – I hardly get a frame but the audio is ok.

Pairs as lambdas

August 18, 2004 Leave a comment

The SICP video lectures talk about how data is not intrinsic. That is, once you have lambda, you have enough to create all the data structures you want. Of course that is without caring for efficiency.

Here’s some code I was mucking around with the other night:

; Implement pairs (cons, car, cdr) in terms of lambda.

(define cons
  (lambda (a b)
    (lambda (x)
      (cond ((eq? x 'car) a)
            ((eq? x 'cdr) b)
            (else (error "bad dispatch"))))))

(define car
  (lambda (pair) (pair 'car)))

(define cdr
  (lambda (pair) (pair 'cdr)))

; Example to type into the scheme repl.
(define a (cons 3 4))
a                 ; => a procedure
(car a)           ; => 3
(cdr a)           ; => 4
(a 1)             ; error "bad dispatch"

; The following attempts to work out what "error" does. This example will
; probably display "1" and then exit to the repl with "bad dispatch" error.
; It may not as Scheme doesn't define the order of evaluation for arguments to
; a combination.
(+ (display "1\n") (a 99) (display "2\n"))
Categories: Programming Tags:

The Python Paradox

August 14, 2004 Leave a comment

I learnt Python afew years ago. I’ve moved onto Ruby for day to day tasks and am studying Scheme and Common Lisp when I can spare a moment.

Today I was watching another couple of videos from the Abelson and Sussman collection. It’s about 9G of divx movies. I’m pretty dedicated to my lisp education – I’m only on 32k dialup connection. I watched 6a&b about streams (again) and then got onto the very amusing lectures on meta circular evaluation (7a&b). Very funny – a must see for any lisp enthusiast.

I was also reviewing multithreading issues in Common Lisp implementations via the slides from Roger Corman’s talk on the subject.

Categories: Programming Tags: , ,