## Posts Tagged ‘Scheme’

### Mutable pairs in Scheme: a brainteaser

This one really confused me for a bit, and then it all made sense. See if you can figure it out. Remember that, for cons pairs, eq? tests pointer equality. (I’m using Chez Scheme 8.0, but that shouldn’t matter.)

> (define ls (iota 5)) > ls (0 1 2 3 4) > (define ls2 (cons -1 ls)) > ls2 (-1 0 1 2 3 4) > (eq? (cdr ls2) ls) #t > (set-cdr! ls '(1)) > ls (0 1) > ls2 (-1 0 1) > (eq? (cdr ls2) ls) #t 

On the other hand,

> (define ls (iota 5)) > ls (0 1 2 3 4) > (define ls2 (cons -1 ls)) > ls2 (-1 0 1 2 3 4) > (eq? (cdr ls2) ls) #t > (set-cdr! ls2 '(0)) > ls (0 1 2 3 4) > ls2 (-1 0) > (eq? (cdr ls2) ls) #f 

Why is it that the first example mutates both ls and ls2, and the second example only mutates ls2?

### Amending vim syntax

In Compilers this semester, we’re making heavy use of a powerful Scheme match macro which includes a ,[Cata -> Id] syntax for inline calls to helper functions. However, the default vim syntax highlighting for Scheme flags -> as a syntax error, because it is an invalid identifier. It also fails to syntax-highlight match itself, and a number of other helpful macros that we’re using, like define-who.

To get around this problem without write access to the actual syntax file itself (located at /usr/share/vim/vim72/syntax/scheme.vim in the burrow) you can simply create an additional syntax file called ~/.vim/after/syntax/scheme.vim. This file will be automatically loaded after the default Scheme syntax, whenever a Schemely buffer is loaded. I put the following lines in my file:

syn keyword schemeSyntax define-who match -> syntax region schemeMultilineComment start=/#|/ end=/|#/ contains=schemeMultilineComment

This also fixes the problem that, in the default Scheme mode, #| and |# are not treated as comment delimiters. Of course, you can add any personal macros to the list of syntax keywords.

One last problem: I’m using Aaron Hsu’s ChezWEB framework, and he uses .w as the extension for ChezWEB source files, which ought to be highlighted as Scheme source. Add this line to ~/.vimrc to load Scheme syntax whenever a .w buffer is loaded:

au BufNewFile,BufRead *.w set filetype=scheme