September 2012
7 posts
A little bit of file-position
When used with one argument, file-position returns an integer representing the stream’s file position: * (defvar *s* (open "data.bin" :element-type '(unsigned-byte 8)) => *S* * (read-byte *s*) => 42 * (read-byte *s*) => 107 * (file-position *s*) => 2 When used with two arguments, file-position sets the stream position: * (file-position *s* 1) => T * (read-byte *s*) =>...
Sep 27th
A brief history of Lisp
For a 1500-word history of Lisp up to the point of Common Lisp standardization, see section 1.1.2 of the spec. It covers the institutions, projects, people, and influential ideas involved in the creation and evolution of Lisp over the course of several decades, from McCarthy at Dartmouth in the 1950s to the many active branches of Lisp in the 1980s.
Sep 16th
Putting the R in REPL
Try this in your REPL: * (let (#'42) (+ . #'5)) => 47 If you find it perplexing, and you use SBCL, evaluating + next in the REPL might help clear things up.
Sep 15th
1 note
Using an adjustable displaced array as a cursor on...
Scanning a string can be done using input functions withwith-input-from-string: (with-input-from-string (stream "Hello World! How do you do? ") (let ((c (make-string 4))) (loop :for pos = (read-sequence c stream) :while (= pos (length c)) :do (print c) :finally (terpri)))) "Hell" "o Wo" "rld!" " Ho" "w do" " you" " do?" nil This can also be done using a...
Sep 14th
2 notes
Binding keyword arguments
By default, keyword argument variable bindings match the name of the keyword used to pass the value. For example: (defun keytest (&key foo) (list foo)) * (keytest :foo 42) => (42) However, the variable doesn’t have to match the keyword provided. The following syntax will accept a keyword of :foo in the function call but bind a variable named bar: (defun keytest (&key ((:foo...
Sep 10th
3 notes
Multiple export clauses in defpackage
The syntax for defpackage allows multiple export clauses. I like to use this feature to visually group related symbols. (defpackage #:myproject (:use #:cl) ;; Web stuff (:export #:fetch #:parse-url #:status) ;; File utilities (:export #:lines #:first-line #:touch) ...) Although it has no effect on the semantics, I find it helpful for...
Sep 7th
1 note
A simple REPL
Here’s a very simple REPL that includes the *, **, and *** variables: (defun repl () (princ "> ") (loop (shiftf *** ** * (eval (read))) (format t "~a~&> " *))) Provided by Stas Boukarev.
Sep 6th
4 notes