2015년 10월 11일 일요일

Emacs with racket-mode as a replacement for DrRacket

Recently I've been taking the UBC course Systematic Program Design (SPD) Part 2 on edX. The language used in the course is Basic Student Language (BSL) from the well-known How to Design Programs (HtDP 2nd Edition) curriculum. The DrRacket IDE used in the course is a very convenient tool containing an editor, REPL, stepper and even allows you to copy-and-paste images directly into the editor window after which they can be manipulated in programs.

In Part 1 of the course, I stuck with DrRacket despite being an Emacs user. Later when searching Google for info on using Emacs for HtDP languages and Racket, many Emacs users suggested Geiser mode, but recently a Racket-specific major mode called racket-mode has been developed and is available through the melpa package repository. If you are running Emacs 24+ you can enable melpa with the following line in your .emacs file:

(add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/") t)

Then M-x package-list-packages and C-s for racket-mode.

racket-mode works great and can even render images in the REPL:


One thing to watch out for when saving code that includes copy-and-pasted images in the DrRacket editor window, however, is that the saved file will be in a special binary format that is not version control friendly. If you want to use git to track changes in your BSL / Racket code, for example, your code needs to be in text format.

A DrRacket binary file will have a header that looks like the following:

#reader(lib"read.ss""wxme")WXME0108 ## 
#|
   This file uses the GRacket editor format.
   Open this file in DrRacket version 5.3 or later to read it.

   Most likely, it was created by saving a program in DrRacket,
   and it probably contains a program with non-text elements
   (such as images or comment boxes).

            http://racket-lang.org/
|#

In the starter files provided by the instructors, all the comment boxes are graphical; this will also cause your BSL source files to be saved in binary format. To avoid this, you can convert graphical comment boxes to pure text comments by clicking anywhere inside the comment box, opening the Racket menu and selecting Comment Out with Semicolons:



Note you can also create block comments in BSL or Racket with #| some text |# (hash+pipe some text pipe+hash). Regular line comments are denoted by semicolon(s).

In the programs you have to write for HtDP and the SPD edX course, images will be frequently incorporated into your programs. The best way to avoid having your source files become binaries is to not cut-and-paste images directly into the DrRacket editor window. There is a function called (bitmap ...) that is part of the 2htdp/image library that allows you to refer to an image in a file other than your source file.

For example:

(require 2htdp/image)
(define test-image (bitmap "images/cloud.png"))
test-image
(flip-vertical test-image)
(scale 0.5 test-image)

The path to cloud.png is relative to the location of the current open file. Since the image file is not contained in the source, it will not become a big ugly binary when saved.

The results of the code above in the racket-mode REPL window:


Now all you have to do is tell your version control system to ignore all files in the images folder. Since I use git, I added the following file formats to .gitignore in my repo directory:

#Tell git to ignore image files
*.jpeg
*.jpg
*.bmp
*.png
*.gif
#ignore temp files
*~

Now you should be all set to use Emacs as your development environment for HtDP 2e and the UBC Systematic Program Design courses!

References:

http://stackoverflow.com/questions/17895038/best-way-to-define-an-image-variable-in-racket

http://stackoverflow.com/questions/12674288/how-to-run-racket-in-emacs/12675896#12675896


댓글 1개: