1

Working through SICP using Emacs, Geiser, and MIT Scheme, I decided to switch over to Racket in order to properly do the exercises in section 2.2.4 involving the Picture Language.

Configuration

I got a setup together that works for MIT Scheme and Racket using the following ~/.emacs configuration:

(require 'package)
(add-to-list 'package-archives
         '("melpa" . "https://melpa.org/packages/"))
(package-initialize)

(setq geiser-mit-binary "/usr/bin/scheme")
(setq geiser-racket-binary "/usr/bin/racket")
(setq geiser-active-implementations '(mit racket))
(add-to-list 'auto-mode-alist '("\\.rkt\\'" . geiser-mode))

Workflow with MIT Scheme

Here's my workflow for working with MIT Scheme:

First, I open a file (fib.scm) using emacs fib.scm. I'm asked to pick the Scheme implementation, which I answer with mit.

Second, I write my fib function, press C-c C-z to open a REPL.

Third, I switch back to the code buffer with C-x o and evaluate it using C-c C-b.

Fourth, I switch to the REPL buffer with C-x o and evaluate some expressions.

This it how it looks like:

MIT Scheme with Geiser in Emacs

Workflow with Racket

Here's my (intended) workflow for working with the SICP language of Racket:

First, I open a file (fib.rkt) using emacs fib.rkt. Interestingly, I'm not asked to pick a Scheme implementation.

Second, I write my fib function, but use #lang sicp in the first line. Then I open the REPL using C-c C-z.

Third, I switch back to the code buffer with C-x o and evaluate it using C-c C-b, which gives no error message, but prints => #<void> at the very bottom.

Fourth, I switch back to the REPL buffer with C-x o, where I fail to evaluate an expression like (fib 3):

  2 racket@> (fib 3)                                                                       
  3 fib: undefined;                                                                        
  4  cannot reference an identifier before its definition                                  
  5   in module: top-level                                                                 
  6   context...:                                                                          
  7    body of top-level                                                                   
  8    /usr/share/racket/collects/racket/repl.rkt:11:26

This it how it looks like:

Racket with Geiser in Emacs

What am I doing wrong? Is it the configuration, is it the way I use it?

Example with sicp-pict

When I run the following code using the Racket workflow from above, the picture of Einstein is properly displayed:

#lang sicp
(#%require sicp-pict)
(paint einstein)

So my setup is not entirely broken…

Addendum

Having re-installed the geiser-racket package, I'm now able to start a REPL. I'm also able to evaluate the entire buffer using C-c C-b, which prints => #<void> to the bottom of the screen.

However, when I try to actually invoke a function, I get this error:

  1 Welcome to Racket v8.6 [cs].                                                                          
  2 racket@> (fib 3)                                                                                      
  3 +: contract violation                                                                                 
  4   expected: number?                                                                                   
  5   given: #<procedure:fib>                                                                             
  6   context...:                                                                                         
  7    /usr/share/racket/collects/racket/private/norm-define.rkt:52:83: body of top-level                 
  8    /usr/share/racket/collects/racket/repl.rkt:11:26

The error in the Racket REPL

Edit: Oh dear, it was a simple syntax error... my bad! Everything is fine now!

working fib implementation

Patrick Bucher
  • 1,302
  • 2
  • 14
  • 36
  • You need one of the options. See https://stackoverflow.com/questions/19875182/racket-execute-file-and-stay-in-interactive-mode – soegaard Oct 23 '22 at 22:25
  • @soegaard I created a file `~/.local/bin/racket-interactive`, which calls `racket -i`, and use this `racket-interactive` as the `geiser-racket-binary`. Now I can't even switch between the (slowly starting) REPL and the editor buffer. – Patrick Bucher Oct 24 '22 at 05:18
  • Time to look at racket-mode.com instead. – soegaard Oct 24 '22 at 08:43
  • It seems there is a geiser-racket package. But I recommend racket-mode. It is the most used Emacs mode for Racket. https://github.com/emacsmirror/geiser-racket/ – soegaard Oct 24 '22 at 09:20
  • 1
    @soegaard Interesting; I thought that I already had installed `geiser-racket`, but it wasn't listed as installed. So I re-installed it and also removed the line `(add-to-list 'auto-mode-alist '("\\.rkt\\'" . geiser-mode))` from my `~/.emacs`. How the REPL works! – Patrick Bucher Oct 24 '22 at 13:56
  • 1
    I mean: "_Now_ the REPL works!" – Patrick Bucher Oct 24 '22 at 14:10

1 Answers1

1

To finish things up, I just post the steps I have taken to get everything to run:

Use melpa instaed of melpa-stable (~/emacs):

(require 'package)
(add-to-list 'package-archives
         '("melpa" . "https://melpa.org/packages/"))
(package-initialize)

Refresh the packages after re-opening Emacs:

M-x package-refresh-contents

Install racket-mode:

M-x package-install RET racket-mode

Install the package geiser-racket:

M-x package-install RET geiser-racket

Extend your Geiser config (~/emacs):

(setq geiser-mit-binary "/usr/bin/scheme")
(setq geiser-racket-binary "/usr/bin/racket")
(setq geiser-active-implementations '(mit racket))

Start using the picture language:

#lang sicp
(#%require sicp-pict)
(paint einstein)

Open it in Emacs:

emacs ../examples/einstein.rkt

Start a Racket REPL and evaluate the whole buffer:

C-c C-z
C-x o
C-c C-b

An image of Einstein should appear.

Patrick Bucher
  • 1,302
  • 2
  • 14
  • 36