Ausgabe und Formatierung
(use-modules (ice-9 format) (srfi srfi-13))
(format #t "Name: ~a, Alter: ~a~%" "Mia" 17)
(format #t "~s und ~a~%" "text" "text")
(format #t "Dezimal ~d, binär ~b, oktal ~o, hex ~x~%" 255 255 255 255)
(format #t "Fließkomma ~,2f~%" 3.14159)
(format #t "Breite |~8d|~8a|~%" 42 "li")
(format #t "~a~%" (format #f "~a-~a" 1 2))
(format #t "Liste: ~{~a ~}~%" '(1 2 3))
(display (list 'a "b" #\c 1.5 1/2))
(newline)
(write (list 'a "b" #\c 1.5 1/2))
(newline)
(display (string-pad (number->string 7) 4))
(newline)
(display (list (exact->inexact 2/3) (round 7/2) (inexact->exact 0.25)))
(newline)Ausgabe
Name: Mia, Alter: 17 "text" und text Dezimal 255, bin?r 11111111, oktal 377, hex ff Flie?komma 3.14 Breite | 42|li | 1-2 Liste: 1 2 3 (a b c 1.5 1/2) (a "b" #\c 1.5 1/2) 7 (0.6666666666666666 4 1/4)
Dateien und Ports
(use-modules (ice-9 rdelim))
(define datei "/tmp/scheme-demo.txt")
(call-with-output-file datei
(lambda (port)
(display "eins" port) (newline port)
(display "zwei" port) (newline port)
(write '(a "b" 3) port) (newline port)))
(call-with-input-file datei
(lambda (port)
(let schleife ((zeile (read-line port)) (n 1))
(unless (eof-object? zeile)
(display n) (display ": ") (display zeile) (newline)
(schleife (read-line port) (+ n 1))))))
(with-input-from-file datei
(lambda ()
(display (read-line)) (newline)))
(display (file-exists? datei)) (newline)
(delete-file datei)
(display (file-exists? datei)) (newline)
(define aus (open-output-string))
(write 'symbol aus)
(display " und Text" aus)
(display (get-output-string aus)) (newline)
(define ein (open-input-string "10 20 (a b)"))
(display (list (read ein) (read ein) (read ein) (eof-object? (read ein))))
(newline)
(display (with-output-to-string (lambda () (display "eingefangen"))))
(newline)Ausgabe
1: eins 2: zwei 3: (a "b" 3) eins #t #f symbol und Text (10 20 (a b) #t) eingefangen
Fehlerbehandlung
(define (zeige x) (display x) (newline))
(define (sicher-teile a b)
(catch 'numerical-overflow
(lambda () (/ a b))
(lambda (schluessel . args) 'durch-null)))
(zeige (sicher-teile 10 2))
(zeige (sicher-teile 1 0))
(define ergebnis
(catch #t
(lambda () (error "Etwas ging schief:" 42))
(lambda (schluessel . args) (list 'gefangen schluessel))))
(zeige ergebnis)
(zeige (catch 'mein-fehler
(lambda () (throw 'mein-fehler 1 2))
(lambda (schluessel . args) (list 'gefangen schluessel args))))
(zeige (false-if-exception (/ 1 0)))
(zeige (call/cc (lambda (k)
(with-exception-handler
(lambda (e) (k (list 'behandelt e)))
(lambda () (raise 'boom))))))
(dynamic-wind
(lambda () (zeige "vorher"))
(lambda () (zeige "mittendrin"))
(lambda () (zeige "nachher")))Ausgabe
5
durch-null
(gefangen misc-error)
(gefangen mein-fehler (1 2))
#f
(behandelt #<&compound-exception components: (#<&assertion-failure> #<&origin origin: #f> #<&message message: "Wrong type (expecting ~A): ~S"> #<&irritants irritants: ("exact integer" boom)> #<&exception-with-kind-and-args kind: wrong-type-arg args: (#f "Wrong type (expecting ~A): ~S" ("exact integer" boom) (boom))>)>)
vorher
mittendrin
nachherModule
Guile organisiert Code in Modulen mit define-module:
(define-module (meine bibliothek)
#:export (verdopple dreifach))
(define (verdopple x) (* 2 x))
(define (dreifach x) (* 3 x))
(use-modules (srfi srfi-1) (ice-9 match))
(display (verdopple 21)) (newline)
(display (match '(1 2 3)
((a b c) (+ a b c))
(_ 'unbekannt)))
(newline)
(display (match '(add 1 2)
(('add a b) (+ a b))
(('mul a b) (* a b))))
(newline)Ausgabe
42 6 3
Merke
format(Modulice-9 format),displayundwritegeben aus- Dateien über
call-with-output-fileundcall-with-input-file, Strings als Ports - Fehler mit
catch/throw,guardundraise;dynamic-windfür Aufräumarbeiten - Module mit
define-moduleunduse-modules;ice-9 matchbringt Pattern Matching
Aufgabe
Lies eine Datei ein und gib die Zeilen rückwärts aus.