Working through SICP in Jupyter Notebooks

Working through SICP in Jupyter Notebooks

I’m reading Structure and Interpretation of Computer Programs and want to write Racket in VSCode in Jupyter notebooks in a dialect of Scheme closest to the book. This requires this declaration at the top of each Racket file:

#lang r5rs

HOWEVER, Jupyter doesn’t like this …

Jupyter Doesn't Like This

The Jupyter kernel for Racket doesn't directly support changing the language with #lang declarations in individual cells. Claude to the rescue, though … eventually … what a pain to finally get to this!

  1. We need to find our where iracket is installed.
 find ~/.local -name iracket.rkt
/home/captivus/.local/share/racket/8.2/pkgs/iracket/iracket.rkt
  1. Now, we edit this file (home/captivus/.local/share/racket/8.2/pkgs/iracket/iracket.rkt) as follows:
#lang racket/base

;; ============================================================
;; Enabling R5RS mode for compatibility with SICP code
(require racket/sandbox)

(define orig-current-namespace (current-namespace))
(define r5rs-namespace (make-base-namespace))
(parameterize ([current-namespace r5rs-namespace])
  (namespace-require 'r5rs))

(define (switch-to-r5rs)
  (current-namespace r5rs-namespace))

(define (switch-to-orig)
  (current-namespace orig-current-namespace))

(switch-to-r5rs)

;; The rest of the original iracket.rkt content should follow here ...
;; ============================================================
  1. Now, restart your VSCode and create a Jupyter notebook as previously described in your TIL article … and you’re up!

links