Using scm2perl to understand SICP

statebelt on 2002-04-05T17:14:35

At PerlWhirl '02 I sat in on one of Dominus' lectures on using functional programming techniques in Perl. I asked where I could learn more about this and was recommended the book Structure and Interpretation of Computer Programs.

The examples are in Scheme, which is line-noise to me, so I've been using the scm2perl translator to help me understand the examples in my native perl. It does a decent job of translation, although it doesn't understand lambda functions (anon subs). scm2perl is part of the Gimp CPAN package and handles Gimp's script-fu dialect of Scheme.

Example:

[localhost:~] thomas% less sum.scm
(define (sum term a next b)
  (if (> a b)
      0
      (+ (term a)
         (sum term (next a) next b))))

[localhost:~] thomas% scm2perl sum.scm
creating parser...done
header...reading(sum.scm)...translating...trailer...wrote(sum.pl)
[localhost:~] thomas% less sum.pl
#!/usr/bin/perl

use Gimp qw(:auto);
use Gimp::Fu;

sub sum {
   my ($term, $a, $next, $b) = @_;
   if ($a > $b) {
      0;
   } else {
      (term ($a) + sum ($term, next ($a), $next, $b));
   }
}


exit main;


Line noise?

pdcawley on 2002-04-05T20:06:28

Um... if you follow the book through from the beginning you'll get introduced to scheme as you go. It's worth the effort too, seriously head stretching.

The Little Schemer and The Seasoned Schemer are also worth a go, the stuff in 'seasoned' dealing with continuations, co routines and all that jazz is definitely deep voodoo.

ugh

jhi on 2002-04-05T22:01:26

I seriously suggest trying to wean yourself out of the translator. Grok Scheme. Then move on and grok Smalltalk. Prolog. Forth. (*) That's called "learning", I believe.

(*) No, that's not a progression of "guruness", or recommendations, just a selection of mind-expanding experiences.

Skeem

TorgoX on 2002-04-06T01:33:38

It's a very good book, but I was really happy once I was able to recognize that it has some serious problems and unexamined prejudices. Among other things, it has a definite air of "the perfect language" about it. I wish the authors had gone thru had had a co-author hang "rebuttal" footnotes off of their wackier unexamined statements which they always manage to phrase in totally absolutist terms.