;; Die ersten drei Zeilen dieser Datei wurden von DrRacket eingefügt. Sie enthalten Metadaten ;; über die Sprachebene dieser Datei in einer Form, die DrRacket verarbeiten kann. #reader(lib "DMdA-vanilla-reader.ss" "deinprogramm")((modname listoperations) (read-case-sensitive #f) (teachpacks ()) (deinprogramm-settings #(#f write repeating-decimal #f #t none explicit #f ()))) ; Modul mit einigen einfachen Operationen auf Listen (provide list-length concatenate n-th) (: prim4 (list-of natural)) ;(define prim4 (cons 2 (cons 3 (cons 5 (cons 7 empty))))) (define prim4 (list 2 3 5 7)) ;; Universelle Prozedure auf Listen: ; Länge einer Liste berechnen (: list-length ((list-of %a) -> natural)) (check-expect (list-length empty) 0) (check-expect (list-length (cons 99 empty)) 1) (check-expect (list-length prim4) 4) #;(define list-length (lambda (xs) ...)) ; Konstruktionsschablone für Listen #;(define list-length (lambda (xs) (cond ((empty? xs) ...) ((cons? xs) ...(first xs)... ...(list-length (rest xs))...)))) (define list-length (lambda (xs) (cond ((empty? xs) 0) ((cons? xs) (+ 1 (list-length (rest xs))))))) ; Konkatenation von Listen: berechne aus zwei gegebenen Listen ; eine Liste, die alle Elemente in gleicher Reihenfolge enthält (: concatenate ((list-of %a) (list-of %a) -> (list-of %a))) (check-expect (concatenate empty empty) empty) (check-expect (concatenate empty prim4) prim4) (check-expect (concatenate prim4 empty) prim4) (check-expect (concatenate (list 1 2) (list 3 4 5)) (list 1 2 3 4 5)) (check-expect (concatenate (list "Hello") (list "World")) (list "Hello" "World")) #;(define concatenate (lambda (xs ys) ...)) ; Konstruktionsschablone für Listen angewendet auf xs: #;(define concatenate (lambda (xs ys) (cond ((empty? xs) ...) ((cons? xs) ...(first xs)... ...(concatenate (rest xs) ys)...)))) (define concatenate (lambda (xs ys) (cond ((empty? xs) ys) ((cons? xs) (cons (first xs) (concatenate (rest xs) ys)))))) ; n-tes Element einer Liste berechnen (n=0: erstes Element) (: n-th ((list-of %a) natural -> %a)) (check-expect (n-th (list 11) 0) 11) (check-expect (n-th (list 1 2 3 4) 3) 4) (check-expect (n-th (list "Hello" "World") 1) "World") #;(define n-th (lambda (xs n) ...)) #;(define n-th (lambda (xs n) (cond ((empty? xs) ...) ((cons? xs) ...(first xs)... ...(n-th (rest xs) n)...)))) (define n-th (lambda (xs n) (cond ((empty? xs) (violation "n-th: list too short")) ((cons? xs) (if (= n 0) (first xs) (n-th (rest xs) (- n 1))))))) (check-property (for-all ((xs (list-of (one-of 1 2 3))) (ys (list-of (one-of 1 2 3)))) (expect (list-length (concatenate xs ys)) (+ (list-length xs) (list-length ys)))))