Chez Scheme Setup

October 3, 2017

We continue today with the recent series of exercises about how I configured my new computer. Chez Scheme is a remarkable Scheme compiler/interpreter, very fast, and remarkably bug-free, but it includes limited libraries, so users must create their own. This note describes some of the libraries I use.

The primary library is my Standard Prelude, described at https://programmingpraxis.com/standard-prelude, loaded each time Chez Scheme starts up. Chez Scheme resides in two executable files in /usr/local/bin called scheme and petite. I provide a shell script /usr/local/bin/chez that loads scheme (the full compiler) and the Standard Prelude as part of the interactive-system startup:

    #! /usr/local/bin/scheme

    ; standard prelude
    (load "~/scheme/lib/prelude.ss")

    ; literate programming
    (load "~/scheme/lib/tangle.ss")

    ; interactive editing
    (define *edfile* #f)
    (define (lload . args)
      (when (pair? args) (set! *edfile* (car args)))
      (when (not *edfile*) (error 'lload "file not found"))
      (when (not (file-exists? *edfile*) (error 'lload "file not found"))
      (let* ((ss (tangle *edfile*)) (i (open-input-string ss)))
        (let loop ((obj (read i)))
          (if (eof-object? obj) (close-input-port i)
            (begin (eval obj (interaction-environment))
                   (loop (read i))))))))
    (define (ed . args)
      (when (pair? args) (set! *edfile* (car args)))
      (when (not *edfile*) (error 'ed "file not found"))
      (system (string-append "ed " *edfile*))
      (lload *edfile*))
    (define (vi . args)
      (when (pair? args) (set! *edfile* (car args)))
      (when (not *edfile*) (error 'vi "file not found"))
      (system (string-append "vi " *edfile*))
      (lload *edfile*))
    (define (sh . args)
      (system (if (null? args) "sh -l" (string-join #\space args)))
      (if #f #f))

    ; go home
    (cd "~/scheme")

Documentation of the Standard Prelude is provided on the web page noted above. I defer to Chez Scheme for hash tables, structures and random numbers, rather than using the versions in my Standard Prelude, and also defer to Chez Scheme for the Standard Prelude functions that Chez Scheme provides natively. The only part of the Standard Prelude that is not loaded is avl trees, which are removed to their own library. Also loaded at each run of Chez Scheme are my literate programming environment described at https://programmingpraxis.com/2010/08/10/literate-programming and my interactive editing environment described at https://programmingpraxis.com/2017/04/07/edit-files-in-a-repl.

The only thing that hasn’t been discussed previously in my blog is the sh command. On my desktop computer, the screen is large enough that I can have both a Scheme REPL and a system shell window open at the same time, and move between them as needed. On my tablet, the screen is simply too small, so it is convenient to have a way to quickly exit to the system shell, execute some command, and return to the REPL. The sh command, called with no arguments, opens a login shell, and returns to the REPL when it is exited; called with arguments, it executes the command specified by the arguments, then returns to the REPL immediately. The (if #f #f) that ends the function is the standard way to return void from a function; it is used here to suppress the return code that sh normally returns.

Other libraries that I commonly use appear on the pages that follow; all are stored in ~/scheme/lib, and loaded by (load "~/scheme/lib/xxx.ss"):

    avl.ss      -- avl trees
    priqueue.ss -- priority queues
    textfile.ss -- input, processing and output of text file databases
    pregexp.ss  -- Dorai Sitaram's portable regular expression matcher
    streams.ss  -- SRFI-41 streams - lazy lists (I am the author)
    match.ss    -- Kent Dybvig's pattern matcher

My normal habit is to work in the interpreter with the current directory set to ~/scheme. I create a directory for each project, then use (lload "project/xxx.ss") to load project file xxx.ss into the interpreter and either (ed) or (vi) to edit the file. Directory ~/scheme has no files, only project directories.

Your task is to implement this environment on your system, if you are a Scheme user, or to describe your own programming environment. When you are finished, you are welcome to discuss the exercise in the comments below.

Pages: 1 2 3 4 5 6 7 8 9 10

3 Responses to “Chez Scheme Setup”

  1. programmingpraxis said

    My apologies; the formatting got really messed up. I’ll work on fixing through the day today.

  2. chaw said

    I think the pairing heap implementation will not scale to beyond stack limits since it uses non-tail recursion. An easy fix would be to replace the recursive calls in pq-merge-pairs with a fold of pq-merge on pre-paired arguments.

  3. […] There are several pattern-matching libraries available for Scheme, but they are rather heavy (the one I use, by Friedman, Hilsdale and Dybvig, is over six hundred lines of code). Our Standard Prelude has a […]

Leave a comment