[SICP 4] Setting Up a Scheme Development Environment on Windows

TL;DR: Now that on previous posts I found an actual reason to legitimize my studies as prototyping new languages, it’s time to setup a Scheme developer environment on my computer! This post is about how to prepare a Scheme devbox via Dr. Racket or via MIT Scheme on a Windows machine to be coded in Visual Studio Code.

Setup - Racket

AFAIK, the easiest way for Scheme development is to use Racket. It comes with a cross-platform IDE DrRacket. Also has some picture drawing mechanisms for the drawing section from SICP too. You just put #lang sicp at the top of a file, and it’ll be able to run most of the Scheme code from the book.

How to import definitions from other files?

  • Save “library” and “application” files in the same directory. (In my case c:\Users\veliu\Dropbox\SICP)
  • Library module has to have #lang scheme otherwise exports won’t work.
  • Define names in the library module, then export them using (provide). Example common.scm #lang scheme (define (square x) (* x x)) (define (cube x) (* x x x)) (provide square cube)
  • Put application file in the same directory. Require the “library” file via (#%require)
  • The feature file needs to be saved, because an interpreter running on a non-saved file searches user’s home path to find the “library” file.
  • Example application file that uses “common” library: #lang sicp (#%require "common.scm") (display (square 3)) (newline) (display (cube 3))
  • ref: racket - How do I include files in DrScheme? - Stack Overflow

Setup - MIT Scheme

However, if you have a more purist approach, you can use the original MIT Scheme. I installed it by following the Unix instructions given at https://www.gnu.org/software/mit-scheme/

  • In order to run it on Windows, I compiled it from source code in Windows Subsystem for Linux (WSL) environment.
  • Also installed NCurses library via sudo apt-get install libncurses-dev before ./configure
  • To access Windows files from WSL go mount folder /mnt/c
  • To access WSL files from Windows put \\wsl$ in Windows Explorer address bar, or start menu search bar.
  • VS Code with added vscode-scheme extension can be used to edit Scheme files. Definitely not as good as Dr. Racket when it comes to indentations, but having the highlighting ability is better than nothing.
  • The way to run Scheme “scripts” was not as easy as running a Python script which are run usually with a command like following: python file.py. (See mit-scheme -- run a script and exit - Stack Overflow)
  • Shortest command to run a file is via piping: scheme < file.scm. However, this displays everything the interpreter does.
  • Use quiet mode to suppress them scheme --quiet < file.scm.
  • There is a nicer way via -load argument but that does not return back to the command line. And the trick was piping it to echo
  • Here is the bash function I settled down for easy usage of above trick:
    function mitscm () {
      echo | mit-scheme --quiet --load $1;
      echo
    }
    export -f mitscm
  • source above file in .bashrc and then mitscm file.scm will run the file in quiet mode and returns to command line.
  • Note that it’s possible to connect to WSL from VS Code via remote development. This way when you open a terminal in VS Code, it runs commands in WSL. (Remote VS Code requires to install extensions separately) See: Developing in the Windows Subsystem for Linux with Visual Studio Code
  • One last trick to distribute code into multiple files is the load function. Put your common definitions in common.scm, and start your exercise file that is with (load "common.scm").
  • Some keyboard shortcuts
    • To exit the interpreter call (exit) command or Ctrl+d.
    • To stop debugger Ctrl+g (not knowing this can make the development a very frustrating experience)

Documentation

  • User Manual at GNU page
  • Reference at GNU page
  • Revised(5) Scheme RSR5 standard at MIT SAIL’s (Computer Science & Artificial Intelligence Lab) Scheme page
    • Note that Scheme language evolved further and has two more standard, last one being RSR7. But MIT Scheme stayed at RSR5. See R7RS versus R6RS - weinholt.se for everything you don’t need to know about comparisons of Scheme standards.

Now I’m ready to experience SICP in its full glory using its original interpreter!

published at: 2020-08-25 03:00 edited at: 2020-09-05 20:39 UTC-5
tags: