EMZETT.
Login

Kurz: format ist eine kleine Sprache für Ausgabe:

Teil des Kurses Common Lisp

Kapitel 6 von 8 im Kurs Common Lisp. Mit Fortschritt, Quiz und Zertifikat auf der Lernseite.

format im Detail

format ist eine kleine Sprache für Ausgabe:

(format t "~a und ~s~%" "Text" "Text")
(format t "Dezimal ~d, binär ~b, oktal ~o, hex ~x~%" 255 255 255 255)
(format t "Fließkomma ~f, ~,2f, ~e~%" 3.14159 3.14159 31415.9)
(format t "Breite |~8d|~8@a|~8a|~%" 42 "re" "li")
(format t "~r~%" 21)
(format t "~:r ~@r~%" 3 14)
(format t "~{~a ~}~%" '(1 2 3))
(format t "~{~a~^, ~}~%" '(a b c))
(format t "~{(~a . ~a) ~}~%" '(x 1 y 2))
(format t "~a~[ null~; eins~; zwei~]~%" "Zahl:" 1)
(format t "~:[nein~;ja~]~%" t)
(format t "~(KLEIN~) ~@(titel~)~%")
(format t "~%Mit ~10t Tabulator~%")
(format t "~a~%" (format nil "~3,'0d" 7))
(format t "~,,'.,4:d~%" 1234567)

Ausgabe:

Text und "Text"
Dezimal 255, binär 11111111, oktal 377, hex FF
Fließkomma 3.14159, 3.14, 3.14159e+4
Breite |      42|      re|li      |
twenty-one
third XIV
1 2 3
A, B, C
(X . 1) (Y . 2)
Zahl: eins
ja
klein Titel
 
Mit        Tabulator
007
123.4567

Dateien

(let ((pfad "/tmp/lisp-demo.txt"))
  (with-open-file (aus pfad :direction :output :if-exists :supersede)
    (format aus "eins~%zwei~%drei~%")
    (write-line "vier" aus))
 
  (with-open-file (ein pfad)
    (loop for zeile = (read-line ein nil nil)
          while zeile
          do (format t "> ~a~%" zeile)))
 
  (with-open-file (ein pfad)
    (format t "~a Zeichen~%" (file-length ein)))
 
  (format t "~a~%" (probe-file pfad))
  (delete-file pfad)
  (format t "~a~%" (probe-file pfad)))

Ausgabe:

> eins
> zwei
> drei
> vier
20 Zeichen
/tmp/lisp-demo.txt
NIL

Strings als Streams und read

(format t "~a~%" (with-output-to-string (s)
                   (format s "a=~a " 1)
                   (write-string "b" s)))
 
(with-input-from-string (ein "10 20 30")
  (let ((summe 0))
    (loop for zahl = (read ein nil nil)
          while zahl
          do (incf summe zahl))
    (format t "Summe ~a~%" summe)))
 
(format t "~a~%" (read-from-string "(+ 1 2)"))
(format t "~a~%" (eval (read-from-string "(* 6 7)")))
(format t "~a~%" (prin1-to-string "mit \"Anführungszeichen\""))
(format t "~a ~a~%" (parse-integer "  42  " :junk-allowed t) (parse-integer "x" :junk-allowed t))
(let ((*print-base* 2)) (format t "~a~%" 10))
(let ((*print-pretty* nil)) (print '(a b)))
(terpri)

Ausgabe:

a=1 b
Summe 60
(+ 1 2)
42
"mit \"Anführungszeichen\""
42 NIL
1010
 
(A B)

Pakete (Namensräume)

(defpackage :meine-bibliothek
  (:use :cl)
  (:export :verdopple :*version*))
 
(in-package :meine-bibliothek)
 
(defparameter *version* "1.0")
(defun verdopple (x) (* 2 x))
(defun intern-hilfe (x) (+ x 1))
 
(in-package :cl-user)
(format t "~a ~a~%" (meine-bibliothek:verdopple 21) meine-bibliothek:*version*)
(format t "~a~%" (meine-bibliothek::intern-hilfe 1))
(format t "~a~%" (package-name *package*))

Ausgabe:

42 1.0
2
COMMON-LISP-USER

Zeit, Zufall, Umgebung

(let ((*random-state* (sb-ext:seed-random-state 42)))
  (format t "~a~%" (<= 0 (random 100) 99)))
(format t "~a~%" (integerp (get-universal-time)))
(format t "~a~%" (stringp (lisp-implementation-type)))
(format t "~a~%" (stringp (sb-ext:posix-getenv "PATH")))
(format t "~a~%" (typep (get-internal-real-time) 'integer))
(multiple-value-bind (q r) (floor 17 5)
  (format t "~a Rest ~a~%" q r))
(destructuring-bind (a (b c) &rest d) '(1 (2 3) 4 5)
  (format t "~a ~a ~a ~a~%" a b c d))

Ausgabe:

T
T
T
T
T
3 Rest 2
1 2 3 (4 5)

Merke

  • format ist eine Mini-Sprache: ~a, ~d, ~f, ~{~}, ~[~]
  • with-open-file schließt Dateien automatisch; read-line liest Zeilen
  • Streams gibt es auch für Strings (with-output-to-string)
  • defpackage und in-package schaffen Namensräume

Übungsaufgabe

Lies eine Datei ein und gib die Anzahl der Zeilen und Wörter aus.

Quiz zur Selbstkontrolle

Weiter im Kurs

Zurück: Strukturen, Klassen und CLOS

Weiter: Ökosystem und Werkzeuge

Alle Kapitel: Common Lisp im Überblick