SICP: Evaluate Racket into REPL with Emacs and Geiser - emacs

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:
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:
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
Edit: Oh dear, it was a simple syntax error... my bad! Everything is fine now!

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.

Related

Evaluating Elisp in emacs

I am an hour new into programming in Emacs lisp. I have a little experience with scheme so I understand the big picture of lisps in general. However, I have only used the "pure functional" subset of scheme and do not know how to do practical tasks.
Write now, I know that C-x C-e will evaluate the code enclosed by the parentheses' by the current cursor position.
I wish to loop from i = 1 to 10 and print the values of i out. How is this done? I tried the following:
(defvar i 1)
(while (< i 11)
(print "i: " i)
(setq i (+ i 1)))
Emacs tells me: invalid function 0.
How do I do this correctly?
Why is emacs telling me invalid function 0
Feel free to give me tips about how to use the scratch buffer (all I know is C-x C-e evaluates) in emacs. Thanks for all the help!
EDIT1: Could someone tell me how to print out sequential values of i using a while loop?
EDIT2: When I evaluate the code, it opens up another tiny buffer showing each value of i one at a time. However, it is not a large buffer and only shows values of i from 13 to 19. When I try to get into that buffer, it closes immediately. How do I "scroll" through that tiny buffer? Note that I use emacs 24.3 through the terminal
EDIT3: I figured out that the tiny buffer is the Messages buffer. Is there a better way to view the output of my elisp code? The Messages buffer is full of other junk from evaluating things in emacs.
First and foremost, enable "Enter debugger on error" from the Options menu now and add (setq debug-on-error t) or (custom-set-variables '(debug-on-error t)) to your ~/.emacs.el.
Then you will get a *Backtrace* buffer on C-x C-e:
Debugger entered--Lisp error: (invalid-function 1)
1(10)
print("i: " 1)
(while (< i 11) (print "i: " i) (setq i (+ i 1)))
eval((while (< i 11) (print "i: " i) (setq i (+ i 1))) nil)
eval-last-sexp-1(nil)
eval-last-sexp(nil)
call-interactively(eval-last-sexp nil nil)
command-execute(eval-last-sexp)
which shows that the error comes from print.
C-h f print RET will tell you why, but the upshot is that you want to use insert instead of print here.
Just as an added note, since you mentioned knowing some scheme -- if you like the interactive REPL that you can use in typical scheme environment, you might like ielm -- I think it probably stands for Interactive Emacs Lisp mode. Not sure. Anyway, M-x ielm RET will open up an emacs lisp REPL. Sometimes it is actually useful -- for example, when you want to inspect the content of a variable with a lot of data in it, ielm will print the whole thing out. Ielm is built in to my Emacs. Not sure when it was added to the standard distribution, but the earliest copyright in the source says 1994, so it is probably in your Emacs.
You can evaluate Emacs-Lisp sexps in *scratch* or in any other buffer in the same mode or (my preference) in mode emacs-lisp-mode.
In *scratch* you need only hit C-j (newline) after a sexp to evaluate it. In an emacs-lisp-mode buffer you can, as you said, use C-x C-e after a sexp. Or you can use M-x evaluate-region after selecting one or more sexps. As always, C-h m in any mode tells you about it, and usually lists important key bindings.
You can also check a global variable value using C-h v SOME-VAR. And you can evaluate any sexp on the fly from the minibuffer, using M-:. For example: M-: (setq foo (+ 42 (length bar)))
Wrt the debugger:
As #sds mentioned, debug-on-error puts you in the debugger when an error is raised. You can also set debug-on-quit and then enter the debugger using C-g to quit (e.g., during a loop).
If you know the function you want to debug, you can use M-x debug-on-entry.
Step through the debugger using d, or skip to the end of a step using c. Use q to quit the debugger.
You can also insert calls to function debug in source code, as debugger entry points: (debug).
The backtrace in the debugger is always more informative if you load the relevant source file e.g., foo.el, instead of the byte-compiled file, e.g., foo.elc. So before you use M-x debug-on-entry use C-h f to find out which file the function is defined in, and then load that file using M-x load-file /path/to/the/file.el.
There is also another debugger, besides debug -- look for edebug in the Elisp manual. Some people prefer edebug; I prefer debug.

How to highlight current argument in SLIME

I am using sbcl with GNU Emacs 24.3.1 and the 2012-04-14 release of SLIME, on Arch Linux to write some Common Lisp code. When writing an expression, if I type, for example
(if
the minibuffer will display
(if TEST THEN &OPTIONAL ELSE)
Is there a mode or SLIME setting that can make the argument that I'm currently editing be highlighted in the minibuffer? For example, if I type
(if (> x y)
it would be great if
(if TEST *THEN* &OPTIONAL ELSE)
or something similar was displayed in the minibuffer.
The strange thing is that you have documentation in minibuffer with this configuration. Maybe your distribution also loads it from a different location.
Please try this config:
(setq inferior-lisp-program "/usr/bin/sbcl")
(add-to-list 'load-path "~/.emacs.d/slime-2012-04-14/")
(require 'slime)
(require 'slime-autoloads)
(slime-setup '(slime-autodoc))
It tells Emacs to load and use slime-autodoc module that displays documentation and in minibuffer and highlights it as you'd like it to be.
Maybe you'd also like to update to a more recent SLIME version (the current one in ELPA is 20130402).

Emacs Init File Line Numbering

I am trying to display line numbers in emacs on the left hand side. I know it is the linum-mode. When I do M-x linum-mode, my emacs can't find linum-mode. It only finds line-numbers-mode which displays it on the mode bar at the bottom. Why does it not have linum-mode?
Also, when I put (menu-bar-mode t) or even (menu-bar-mode 1), the menu bar does not start at start-up.
When I put (global-linum-mode t) I get the following init error (using --debug-init):
Debugger entered--Lisp error: (void-function global-linum-mode)
(global-linum-mode t)
eval-buffer(#<buffer *load*> nil "/Users/nayef/.emacs.d/init.el" nil t) ; Reading at buffer position 68
load-with-code-conversion("/Users/nayef/.emacs.d/init.el" "/Users/nayef/.emacs.d/init.el" t t)
load("/Users/nayef/.emacs.d/init" t t)
This is extremely extremely frustrating. It works on one computer over SSH but when I do it on my laptop it does not work. I am starting to understand that even though Emacs is more powerful than VIM, VIM is at least 10 times easier to use, less keystrokes and requires WAY less modifications.
Which version of Emacs do you have? linum-mode is included with Emacs 23 and Emacs 24. If you can, you should probably upgrade to those version. If you can't upgrade and you have Emacs 22, you'll need to do the following steps:
Download linum.el on your system (let's say it's in /home/nayefc/).
Add the line (add-to-list 'load-path "/home/nayefc/") to your .emacs
Add the line (require 'linum) to your .emacs
Restart Emacs, and you should be able to call M-x linum-mode. Good luck.
Vincent.
You're having problems because linum-mode is not a standard part of any Emacs distribution; it has to be installed. You can get it here.

Naive question on how to type-annotated my ocaml prog. in emacs

I heard we can annotate ocaml prog. by their types. An older thread in the forum suggested using ocaml mode of
http://cristal.inria.fr/~remy/poly/emacs/index.html
I have been using Tuareg mode, in which it suggested using "c-c c-t" to retrieve types, cf. this piece of codes in tuareg.el
(when tuareg-with-caml-mode-p
;; Trigger caml-types
(define-key map [?\C-c ?\C-t] 'caml-types-show-type)
;; To prevent misbehavior in case of error during exploration.
(define-key map [(control mouse-2)] 'caml-types-mouse-ignore)
(define-key map [(control down-mouse-2)] 'caml-types-explore)
I got "c-c c-t" undefined although everything seems to be well configured.
Here is the .emacs file
(setq auto-mode-alist
(cons '("\\.ml[iyl]?$" . caml-mode) auto-mode-alist))
(autoload 'caml-mode "ocaml"
"Major mode for editing Caml code." t)
(autoload 'camldebug "camldebug"
"Call the camldebugger on FILE" t)
;; adjust paths for emacs source code
(add-to-list 'load-path "~/my-emacs-config/caml-mode")
;; adjust paths for emacs ocaml info sources
(require 'info)
(add-to-list 'Info-directory-list "~/my-emacs-config/caml-mode")
Here is the files in caml-mode (which contains ocaml.el)
bash-3.2$ ls ~/my-emacs-config/caml-mode/
caml-compat.el caml-emacs.el caml-font.el caml-help.el caml-hilit.el caml-types.el caml.el camldebug.el inf-caml.el ocaml.el
I did the following
--write an factorial func. in ocaml, called "annot.ml"
let rec f n =
if n = 1 then 0 else n * f(n-1)
--ocamlc -annot annot.ml
--open annot.ml by emacs and press "c-c c-t" while the cursor is under "n"
I got in the minibuffer of emacs
c-c c-t undefined
Conclusion, I still cannot retrieve types. Why??? Thank you for your ideas.
More info: when I try M-x caml-[tab] I get the following list, which does not contain caml-types-show-types
Possible completions are:
caml-mode camldebug
camldebug-backtrace camldebug-break
camldebug-close camldebug-complete
camldebug-delete camldebug-display-frame
camldebug-down camldebug-finish
camldebug-goto camldebug-kill
camldebug-last camldebug-mode
camldebug-next camldebug-open
camldebug-print camldebug-refresh
camldebug-reverse camldebug-run
camldebug-step camldebug-up
You're autoloading caml-mode from ocaml.el or ocaml.elc. But there is no such file! The official Caml mode is in a file called caml.el, and Tuareg mode is in a file called tuareg.el. This explains why opening your .ml file doesn't put you in Ocaml mode and doesn't load the Caml support. Change your autoload to either this to use the official mode
(autoload 'caml-mode "caml"
"Major mode for editing Caml code." t)
or this to use Tuareg mode
(autoload 'caml-mode "tuareg"
"Major mode for editing Caml code." t)

can I use two different lisp+slime/swanks from the same emacs?

can I use common lisp and Clojure from within emacs at the same time?
I would like to have each lisp-REPL in its own buffer, and If i did this how could I controll which buffer sent its data to which lisp?
Yes. In the documentation to Slime you will find slime-lisp-implementations. Here is how I have it defined in my .emacs:
(setq slime-lisp-implementations
'((cmucl ("/usr/local/bin/lisp") :coding-system iso-8859-1-unix)
(sbcl ("/usr/local/bin/sbcl" "--core" "/Users/pinochle/bin/sbcl.core-with-swank") :init (lambda (port-file _) (format "(swank:start-server %S :coding-system \"utf-8-unix\")\n" port-file)))
(clozure ("/Users/pinochle/bin/ccl"))
(clojure ("/Users/pinochle/bin/clojure") :init swank-clojure-init)))
You start up your lisps using M-- M-x Slime. It will ask you which Lisp to start up, and you use the name you defined in slime-lisp-implementations. In this example, I would use cmucl, sbcl, clozure or clojure.
You can switch the "active" REPL using the command C-c C-x c. For more info, see the Slime Documentation on controlling multiple connections.