head.daveops.net

Snippets for yer computer needs

Common Lisp

Basics

;; global variable
(defparameter *foo* 123)
;; global constant
(defconstant *limit* 100)

; basic function
(defun divisible-by-3 (n)
  (= (mod n 3) 0))

; emit to STDOUT
(format t "hello world")

Source files

; load a file
(load "file.lisp")

; compile a file
(compile-file "file.lisp")

Property lists

; create a plist
(list :first "frank" :last "sinatra")

; get a value from the plist
(getf '(:first "frank" :last "sinatra") :first)
; returns "frank"

Format directives

(format destination control-string args) => result

destination can be nil, t (ie STDOUT), a stream, or string w/ fill pointer

~ Directive
% newline
{ Consume a list
a ‘aesthetic’, output in human form
R human output of number

Streams

;; check if it's a stream
(output-stream-p *standard-output*)
(input-stream-p *standard-input*)
;; write to standard output
(write-char #\a *standard-output*)
(print 'blerk *standard-output*)
;; read a character
(read-char *standard-input*)
;; write to file
(with-open-file (file-handle "example.txt" :direction :output)
(print "text to write" file-handle))
;; read from file
(with-open-file (file-handle "example.txt" :direction :input)
(read file-handle))
;; file exists?
(probe-file "/path/to/file")
;; mkdir -p
(ensure-directories-exist "/path/to/file")

Resources