How do I type the code to the exercises in SICP? - scheme

I have been using the print eval loop, but should I be using something else?

Use DrRacket. Download it here: http://download.racket-lang.org/

The REPL is the place to do it, yes.
Most people use something on top of the REPL in the command line, like emacs, for example. I use xscheme an emacs library. The alternatives are not very good in my opinion, and using command line only is just intolerable.

If you're using Racket, just use DrRacket.
If you're using MIT Scheme, you may wish to use Edwin, a derivative of Emacs bundled with Mit Scheme, the scheme implementation SICP was originally built for. Edwin is an emacs derivative, so if you already know Emacs, you'll get along with Edwin fine. If you don't know emacs, the basics are:
C-f, C-b, C-n, and C-p for forwards, backwards, up and down, respectively.
C-Space to start selecting a region (highlight a piece of text to operate on). The region will stay there until you get rid of it with C-Space, or do something with it.
C-w to cut the region
C-k to cut the line, from your cursor onwards
C-y to paste
C-x C-e to execute the line of code directly behind your cursor in the scratch/repl buffer (the default window)
C-c C-c to halt execution of something you ran with C-x C-e
C-x C-c to quit
That should be enough to start using Edwin. It's incredibly nice to be able to just edit your definition of a function, re-evaluate the definition you edited, and have the new version start working in your repl environment on the fly.

Use Emacs, and use Geiser within that to access both the REPL and to help with scheme file editing. It also makes dealing with various Scheme REPLs such as Chez Scheme, Racket, MIT Scheme, Guile, Chicken, Gambit and Chibi Scheme effortless.

You can write the scripts in GNU Guile Scheme to implement the exercises in SICP. Most of the inbuilt procedures work with no problem and there is no difference in syntax.
In Linux,
Write the scripts using shebang notation at the beginning of the script file
#!/usr/..<address to guile interpreter> \
-e <name of procedure which should run first> -s
!#
An Example
#!/usr/local/bin/guile \
-e main -s
!#
(define main (args)
(display (+ 3 4))
(newline))
Make the file executable using chmod + <filename> and run it ./<filename>
P.S. Scheme files are saved using .scm file extension.

Related

How to use a different diff command for Emacs and Shell (in terminal)?

I'm using the graphical Emacs-w32 launched from a Cygwin terminal (on Windows) for all editing, and Zsh (in a MinTTY) for running commands.
I've setup up diff-cmd = colordiff in ~/.subversion/config, and, while that makes nice colors in the terminal (for svn log), that causes the problem of adding color escape codes in the output, when diff'ing in Emacs, leading to uncolorized, unreadable diffs in Emacs.
What would be the most sensible fix to this?
Try this solution found in the EmacsWiki:
if you use colordiff in svn, maybe you need this:
(add-hook 'diff-mode-hook
'(lambda ()
(require 'ansi-color)
(ansi-color-apply-on-region (point-min) (point-max))))
Source: https://www.emacswiki.org/emacs/VersionControl
By the way: searching the web for 'emacs svn colordiff' produced this as a top 5 hit...

gtags very slow in Emacs

I am using gnu global in emacs, everything is ok, but command "gtags-find-tag" search is too slow that cannot bear. only this command is very slow, the others like "gtags-find-rtag" is normal I think.
I delete other config such as cedet, so that exclude these effect, but this issue is still exist.
Do I miss some config steps of global or emacs? or anyone in other platform(linux) has this issue?
===
my Emacs is emacs for Mac OSX version is 24.3, GNU global is 6.2.8, and my config file is
(add-to-list 'load-path (expand-file-name "~/.emacs.d"))
(setq load-path (cons "/Users/xxx/.emacs.d/gtags.el" load-path))
(autoload 'gtags-mode "gtags" "" t)
(add-hook 'c-mode-hook
(lambda ()
(gtags-mode t)
))
I don't use Global (I do keep meaning to try it), but looking at the code, gtags-find-tag and gtags-find-rtag are very similar functions.
I presume the difference must either be in the completing-read phase, or the find phase, both of which are calling Global.
In the completing-read, 'gtags' passes the option -c, while 'grtags' passes -cr
In the find phase, 'gtags' passes -x, while 'grtags' passes -xr.
I would firstly try to establish whether it is faster outside of Emacs than it is inside it. The above might be sufficient for you to test this, but you should be able to figure out the exact commands being used by using M-x debug-on-entry RET call-process RET (which you can subsequently cancel with M-x cancel-debug-on-entry), and then running the gtags functions and seeing which arguments appear in the stack trace for that function call.
If you can't make heads or tails of the stack trace, try copying and pasting it into your question.
Edit:
Based on the information from the comments, it's appears that it's the case-insensitivity argument which is, for some reason, slowing things down to an unreasonable degree.
You should be able to disable that behaviour here:
M-x customize-variable RET gtags-ignore-case RET

How to use arrow keys in mzscheme/guile REPL?

When I'm typing in the REPL of guile or mzscheme
(define x 15)
and then press arrow key I get
(define x 15)^[[D
Is it a terminal problem? or something needs to be configured? It's painful to rewrite a line for a simple edit
I found the answer for Guile, adding this in ~/.guile will cut it:
(use-modules (ice-9 readline))
(activate-readline)
I do not know what guile is, but for mzscheme, you can input
(require readline)
To interactively enable the arrow-key behavior you're looking for. To enable this behavior when the REPL starts, run it using
mzscheme -il readline

mit-scheme REPL with command line history and tab completion

I'm reading SICP and I'm using mit-scheme installed on my os x 10.8 laptop via homebrew.
Everything works as advertised, however I'm spoiled by the ease with which I get tab completion and command line history in REPL's for runtimes like Python and Node.js.
I'm not looking for anything heavy duty, but these features are pretty easy to come by in modern REPL's (it's just a simple startup file in Python and can be implemented in a few lines in Node.js).
Is there an easy way to get tab completion and command history in the mit-scheme REPL without a heavy-duty application or having to switch to emacs (i.e. in an xterm terminal)?
Install the readline wrapper:
brew install rlwrap
Once installed, rlwrap scheme will give you persistent history, paren matching, and tab completion. I typically use rlwrap with the following arguments:
-r Put all words seen on in- and output on the completion list.
-c Complete filenames
-f Specify a list of words to use for tab completion. I'm using an abridged list of bindings from the MIT Scheme Reference Manual.
Rather than republish the list here, you can find it in this gist.
I have this file stored in "$HOME"/scheme_completion.txt
rlwrap -r -c -f "$HOME"/scheme_completion.txt scheme
1 ]=> (flo:a <tab tab>
flo:abs flo:acos flo:asin flo:atan flo:atan2
1 ]=> (flo:abs -42.0)
;Value: 42.
I would actually recommend that you use Emacs, and use Geiser within that to access both the REPL and to help with scheme file editing. It also makes dealing with various Scheme REPLs such as Chez Scheme, Racket, MIT Scheme, Guile, Chicken, Gambit and Chibi Scheme effortless. Emacs remains very well tuned to use with Scheme and LISP. Highly recommended.

Creating a Cygwin emacs macro

I have been researching how to get Cygwin to work under emacs. I have it working, but now I want to write a macro that will do the following:
Launch by typing M-x cygwin
Have the script stored in some obvious place (probably my .emacs file)
M-x shell
Rename the buffer to cygwin (or cygwin1, cygwin2, cygwin3, ... if cygwin exists) probably using M-x rename-buffer
M-x ansi-color-for-comint-mode-on
M-x set-buffer-process-coding-system 'undecided-unix 'undecided-unix
Open and run cygwin
It will basically do all of the above steps. I think most of the organization for this little project is done. How do I tie it all together so I can just type M-x cygwin and see a happy new cygwin buffer? What exactly needs to be added to .emacs? Also, where exactly is .emacs in Windows?
I still want the ability to run M-x shell for the windows shell for now. I may also make similar macros for MSYS and ssh'ing to my Linux boxes. How do I get started?
Many questions, here are some answers:
Your .emacs can exist many places, it depends, read here. In short try C-x C-f .emacs, or check the value of the variable 'user-init-file (C-h v user-init-file).
I think the command you want is something along the lines of this:
(require 'comint) ; this does require comint
(defun cygwin ()
"do what i want for cygwin"
(interactive)
(let ((buffer (get-buffer-create (generate-new-buffer-name "cygwin"))))
(pop-to-buffer buffer)
(unless (comint-check-proc buffer)
(apply 'make-comint-in-buffer (buffer-name buffer) buffer "c:/cygwin/Cygwin.bat"
nil
nil)
(ansi-color-for-comint-mode-on)
(set-buffer-process-coding-system 'undecided-unix 'undecided-unix))))
Note: I directly ran the process Cygwin.bat instead of running shell and then starting that batch program. I believe the effect is the same, and more straight forward. I did choose the easy way out for naming the buffer (using 'generate-new-buffer-name) - you'll want to customize to what you want.
You can dump the above command in your .emacs easiest by doing the C-x C-f .emacs and pasting it in the buffer that gets opened up. Save it and restart (or do M-x eval-defun when your cursor is in the body of that command. Then M-x cygwin will run the command.

Resources