Common LISP

Unterlagen PSPP HS15

Converting number to base-2 (binary) string representation [duplicate]

(defun int-to-binary-string (i)
  "convert an integer into it's binary representation in string format"
  (let ((res ""))
    (while (not (= i 0))
      (setq res (concat (if (= 1 (logand i 1)) "1" "0") res))
      (setq i (lsh i -1)))
    (if (string= res "")
        (setq res "0"))
    res))

Documentation

Common Lisp Quick Reference
Practical Common Lisp
The Common Lisp Cookbook
COMMON LISP: A Gentle Introduction to Symbolic Computation (PDF)
Land of LISP, No Starch Press github Source Code
Learning Lisp for CMPT 310
Implementation Notes for GNU CLISP
CLiki Common Lisp wiki
Using and Installing CLISP

Editors

Eclipse Common Lisp
CLiki: Development – Editor/IDE support
CLiki: Dandelion
CLiki: Cusp Eclipse IDE plugin
CLiki: Steel Bank Common Lisp (SBCL)
Dandelion – Eclipse Lisp Plugin Eclipse Update
Develop Lisp applications using the Cusp Eclipse plug-in
Does anyone have a CUSP plugin for Eclipse (Common Lisp)
Getting Cusp running from source
What are the good “rich” IDEs for Lisp?
LispWorks Personal Edition (free)

Common LISP

CLISP
Clozure Common Lisp

5.3 The Data and Control Flow Dictionary
14.2 The Conses Dictionary

LIST (System Class)
VALUES (Accessor)
PROGN (Special Operator)
KEYWORDP (Function)
LET (Special Operator)
NULL (Function)
FLOOR, CEILING, TRUNCATE, ROUND (Function)
COND (Macro)
REDUCE (Function)
MAPCAR, MAPLIST (Function)

Debugger

Top Level Loop
lisp, how to eliminate restarts

The following restarts are available:
USE-VALUE      :R1      Input a value to be used instead.
ABORT          :R2      Abort debug loop
ABORT          :R3      Abort debug loop
ABORT          :R4      Abort debug loop
ABORT          :R5      Abort main loop
Break 4 [6]> :q
[1]> (bye)
[1]> (exit)
[1]> (quit)

value

    • a. one of possibly several objects that are the result of an evaluation.
    • b. (in a situation where exactly one value is expected from the evaluation of a form) the primary value returned by the form.
    • c. (of forms in an implicit progn) one of possibly several objects that result from the evaluation of the last form, or nil if there are no forms.
  1. an object associated with a name in a binding.
  2. (of a symbol) the value of the dynamic variable named by that symbol.
  3. an object associated with a key in an association list, a property list, or a hash table.

multiple values

  1. more than one value.
    • “The function truncate returns multiple values.”
  2. a variable number of values, possibly including zero or one.
    • “The function values returns multiple values.”
  3. a fixed number of values other than one.
    • “The macro multiple-value-bind is among the few operators in Common Lisp which can detect and manipulate multiple values.”

form/forms vs. datum

Forms and the Top-Level Loop
Definition of “lisp form”?

The things which you type to the LISP interpreter are called forms; the LISP interpreter repeatedly reads a form, evaluates it, and prints the result. This procedure is called the read-eval-print loop.

Some forms will cause errors. After an error, LISP will put you into the debugger so you can try to figure out what caused the error. LISP debuggers are all different; but most will respond to the command “help” or “:help” by giving some form of help.

In general, a form is either an atom (for example, a symbol, an integer, or a string) or a list. If the form is an atom, LISP evaluates it immediately. Symbols evaluate to their value; integers and strings evaluate to themselves. If the form is a list, LISP treats its first element as the name of a function; it evaluates the remaining elements recursively, and then calls the function with the values of the remaining elements as arguments.

For example, if LISP sees the form (+ 3 4), it treats + as the name of a function. It then evaluates 3 to get 3 and 4 to get 4; finally it calls + with 3 and 4 as the arguments. The + function returns 7, which LISP prints.

form = datum + program, that can be evaluated without an error.
This is a lisp datum, it’s a list of 3, 4 and 1. This is not a form however as trying to evaluate it does not result into another datum. But rather an error:

(3 4 1)

This is a datum, and a form, also called a ‘normal form’ or a ‘self-evaluating datum’, it evaluates to itself:

3

This is a compound form, evaluating it results into the normal form 8:

(+ 3 4 1)

Apart from normal forms and compound forms, compound forms can be subdivided into procedure calls and special forms (also called syntax) but more properly, the head of a special form is the syntax, as in:

(if (oddp 2) (print "me") (print "or me"))

This is a special form because it’s head is syntax, and not a procedure, the only difference between procedure calls and special forms is that procedure calls see all of the arguments of the form as forms in itself and try to evaluate it first and special forms not necessarily do that. As we understand, only the second and fourth member of this compound form get evaluated, the first member is syntax, and the third is discarded in this case.
As we know for instance this code. But it’s not a form in Common Lisp:

((a 1) (b 2))

So:

(let ((a 1) (b 2)) (+ a b))

Is a special form, it does not evaluate its second member, and evaluates its third member in a different fashion than what would be expected if it was not a special form. That is, a and b as subforms of its third form have a different binding. let in this case is a syntactic keyword that signals the special form.

arguments &optional, &rest and &key

&optional = optional argument(s)
            Optionale Parameter

&rest     = rest arguments(s)
            Restparameter
            Werden in eine eigene Liste zusammengefasst.
            &rest steht an letzter Stelle, nach notwendigen
            und optionalen Parametern

&key      = key arguments
            Statt Parameter-Reihenfolge über Key/Value

(defun foo (&key a b c) (list a b c))

(foo)                ==> (NIL NIL NIL)
(foo :a 1)           ==> (1 NIL NIL)
(foo :b 1)           ==> (NIL 1 NIL)
(foo :c 1)           ==> (NIL NIL 1)
(foo :a 1 :c 3)      ==> (1 NIL 3)
(foo :a 1 :b 2 :c 3) ==> (1 2 3)
(foo :a 1 :c 3 :b 2) ==> (1 2 3)

keyword or keys in arguments (colons 🙂

What is the colon (:) in Emacs lisp?
Why colons precede variables in Common Lisp

[1] :yellow
:YELLOW

(list) vs. (values)

Most forms create only one value
A form typically returns only one value. Lisp has only a small number of forms which create or receive multiple values.

Normally multiple values are not used. Special forms are required both to produce multiple values and to receive them. If the caller of a function does not request multiple values, but the called function produces multiple values, then the first value is given to the caller and all others are discarded; if the called function produces zero values, then the caller gets nil as a value.

Constructs for Handling Multiple Values
Essential Multiple Values
alues function in Common Lisp
`values` vs `list` for returning multiple values from Lisp form

(cons) vs. Consing

Why is consing in Lisp slow?

(progn)

Object-Oriented equivalent of LISP's progn function?

(setq) vs. (setf)

Lisp style: setq vs. setf

(set symbol  foo)   = set symbol to foo (Deprecated!)
(setf place  form)  = set place to primary values of forms (Macro to setq)
(setq symbol form)  = set symbol to primary values of form

EMACS Lisp

Tips on Writing Comments

Wikibooks

Basic topics

Lists
Functions

Examples

[1]> (+ 2 3)
5

[2]> (print "hello world")
"hello world"
"hello world"
[3]> (defun hello-name (name) (format nil "Hello ~A" name))
HELLO-NAME

[4]> (hello-name 'matt)
"Hello MATT"
[5]> (load "power.lisp")
;; Loading file power.lisp
;; Loaded file power.lisp
T

[6]> (power 3 4)
81

FAQ

Question

Why 'x' doesn't be recognized?

[13]> (defun beispiel (a b c) (list a b c))
BEISPIEL

[14]> (beispiel 1 2 3)
(1 2 3)

[15]> (beispiel x y z)

*** - SYSTEM::READ-EVAL-PRINT: variable X has no value

Question

How to return a value?

[1]> (defun test123 ()
(print "hallo")
(print "welt")
(print "!"))
TEST123

[2]> (test123)
"hallo" 
"welt" 
"!" 
"!"

Answer

[3]> (defun test123 ()
(print "hallo")
(print "welt")
(print "!")
NIL)
TEST123

[4]> (test123)
"hallo" 
"welt" 
"!" 
NIL

Leave a Reply

Your email address will not be published. Required fields are marked *