2022-03-03 23:13:48 +00:00
#+TITLE : Programming in Scheme for SICP
#+AUTHOR : Howard X. Abrams
#+DATE : 2022-03-01
#+FILETAGS : :emacs:
A literate programming file configuring Emacs.
#+BEGIN_SRC emacs-lisp :exports none
2022-03-09 18:45:37 +00:00
;;; ha-programming-scheme --- Configuration for Scheme. -*- lexical-binding: t; -* -
2022-03-03 23:13:48 +00:00
;;
2022-03-09 18:45:37 +00:00
;; © 2022 Howard X. Abrams
;; This work is licensed under a Creative Commons Attribution 4.0 International License.
;; See http://creativecommons.org/licenses/by/4.0/
2022-03-03 23:13:48 +00:00
;;
;; Author: Howard X. Abrams <http://gitlab.com/howardabrams >
;; Maintainer: Howard X. Abrams
;; Created: March 1, 2022
;;
;; This file is not part of GNU Emacs.
;;
;; *NB:* Do not edit this file. Instead, edit the original literate file at:
;; /Users/howard.abrams/other/hamacs/ha-programming-scheme.org
;; And tangle the file to recreate this one.
;;
;;; Code:
#+END_SRC
* Introduction
2022-03-15 16:20:11 +00:00
First, install MIT-Scheme, the Lisp dialect used throughout the SICP book:
2022-03-03 23:13:48 +00:00
=brew install mit-scheme= or =sudo apt install mit-scheme= .
#+BEGIN_SRC sh
2022-03-15 16:20:11 +00:00
brew install mit-scheme
2022-03-03 23:13:48 +00:00
#+END_SRC
2022-03-15 16:20:11 +00:00
Or better yet, let’ s use Guile:
2022-03-03 23:13:48 +00:00
#+BEGIN_SRC sh
2022-03-15 16:20:11 +00:00
brew install guile
2022-03-03 23:13:48 +00:00
#+END_SRC
2022-03-15 16:20:11 +00:00
* Geiser (Scheme Interface)
The [[https://www.nongnu.org/geiser/ ][geiser project ]] attempts to be the interface between Emacs and various Schemes. Since I can’ t decide which to use, I’ ll install/configure them all.
2022-03-03 23:13:48 +00:00
#+BEGIN_SRC emacs-lisp
(use-package geiser
:init
(setq geiser-mit-binary "/usr/local/bin/scheme"
2022-03-15 16:20:11 +00:00
geiser-racket-binary "/usr/local/bin/racket"
geiser-guile-binary "/usr/local/bin/guile"
geiser-active-implementations '(guile)
geiser-default-implementations '(guile))
2022-03-03 23:13:48 +00:00
:config
2022-03-15 16:20:11 +00:00
(use-package geiser-mit)
(use-package geiser-guile)
(use-package geiser-racket)
2022-03-03 23:13:48 +00:00
(add-to-list 'org-babel-load-languages '(scheme . t)))
#+END_SRC
2022-03-15 16:20:11 +00:00
Do we need a Scheme work for Org Babel? According to [[https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-scheme.html ][this document ]], we just need to make sure we add the =:session= variable to start the REPL.
** Try it Out
:PROPERTIES:
:header-args:scheme: :session *scheming* :results value
:END:
This should work?
#+name : hello-world
#+header : :var message="Hello World! What?"
#+begin_src scheme
message
#+end_src
However, it doesn’ t output the results /back into/ this buffer. What is the point of connecting it to =ob= ?
** Install SICP
:PROPERTIES:
:header-args:scheme: :session sicp :results value replace
:END:
2022-03-03 23:13:48 +00:00
Let’ s get the book available as an Info page:
#+BEGIN_SRC emacs-lisp
(use-package sicp)
#+END_SRC
2022-03-15 16:20:11 +00:00
And does this work?
#+BEGIN_SRC scheme
(inc 42)
2022-03-03 23:13:48 +00:00
#+END_SRC
2022-03-15 16:20:11 +00:00
Still having difficulty getting the Scheme REPL to output the results back into this document. Let’ s try Racket...
2022-03-03 23:13:48 +00:00
2022-03-15 16:20:11 +00:00
If we want to read the [[file:~/.emacs.d/straight/build/sicp/sicp.info ][sicp.info ]] file, looks like we need this, at least, temporarily:
2022-03-03 23:13:48 +00:00
#+BEGIN_SRC emacs-lisp
(add-to-list 'auto-mode-alist '("\\.info\\'" . Info-mode))
#+END_SRC
2022-03-15 16:20:11 +00:00
* Racket
Actually, let’ s do this with [[https://racket-lang.org/ ][Racket ]]:
#+BEGIN_SRC emacs-lisp
brew install racket
#+END_SRC
While Racket, as a Scheme, should work with Geiser (below), let’ s also get [[https://racket-mode.com/ ][racket-mode ]] working:
#+BEGIN_SRC emacs-lisp
(use-package racket-mode
:config (setq racket-program "/usr/local/bin/racket"))
#+END_SRC
Can we get Racket working with Org?
#+BEGIN_SRC emacs-lisp
(use-package ob-racket
:straight (:type git :protocol ssh :host github :repo "DEADB17/ob-racket")
:after org
:config
(add-to-list 'org-babel-load-languages '(racket . t)))
#+END_SRC
2022-03-03 23:13:48 +00:00
2022-03-15 16:20:11 +00:00
** Try It Out
:PROPERTIES:
:header-args:racket: :session racketeering :results value replace :lang racket
:END:
Working for values?
#+BEGIN_SRC racket
(* 6 7)
#+END_SRC
#+RESULTS :
: 42
Working for output?
#+BEGIN_SRC racket :results output replace
(define str-1 "hello")
(define str-2 "world")
(define all (string-join (list str-1 str-2) ", "))
(display (string-titlecase all))
#+END_SRC
#+RESULTS :
: Hello, World
The interface is horrendously slow, as the =:session= doesn’ t seem to work, and starting up a Racket REPL takes a long time.
** SICP and Racket
:PROPERTIES:
:header-args:racket: :session *rsicp* :results value replace :lang sicp
:END:
If using [[https://docs.racket-lang.org/sicp-manual/SICP_Language.html ][Racket for SICP ]], install the SICP language:
#+BEGIN_SRC sh
raco pkg install --auto --update-deps sicp
#+END_SRC
We now can give it a =#lang sicp= (or better yet, use the =:lang= header) to define certain SICP-specify features:
Let’ s try this now:
#+BEGIN_SRC racket
(inc 42)
#+END_SRC
#+RESULTS :
: 43
* Technical Artifacts :noexport:
2022-03-03 23:13:48 +00:00
Let's =provide= a name so we can =require= this file:
#+BEGIN_SRC emacs-lisp :exports none
(provide 'ha-programming-scheme)
;;; ha-programming-scheme.el ends here
#+END_SRC
#+DESCRIPTION : A literate programming file configuring Emacs.
#+PROPERTY : header-args:sh :tangle no
#+PROPERTY : header-args:emacs-lisp :tangle yes
#+PROPERTY : header-args :results none :eval no-export :comments no mkdirp yes
#+OPTIONS : num:nil toc:nil todo:nil tasks:nil tags:nil date:nil
#+OPTIONS : skip:nil author:nil email:nil creator:nil timestamp:nil
#+INFOJS_OPT : view:nil toc:nil ltoc:t mouse:underline buttons:0 path:http://orgmode.org/org-info.js