;; 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 property_listoperations) (read-case-sensitive #f) (teachpacks ()) (deinprogramm-settings #(#f write repeating-decimal #f #t none explicit #f ()))) ; Schnittstelle dieses Moduls: (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)) ; Länge einer Liste berechnen (: list-length ((list-of %a) -> natural)) (check-expect (list-length empty) 0) (check-expect (list-length (cons 1 empty)) 1) (check-expect (list-length prim4) 4) #;(define list-length (lambda (xs) ...)) #;(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))))))) ; Zwei Listen aneinanderhängen (: concatenate ((list-of %a) (list-of %a) -> (list-of %a))) (check-expect (concatenate empty prim4) prim4) (check-expect (concatenate prim4 empty) prim4) (check-expect (concatenate (list 1 2) (list 3 4)) (list 1 2 3 4)) #;(define concatenate (lambda (xs ys) ...)) #;(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) ... (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 bestimmen, wobei n=0 das erste Element ist (: n-th ((list-of %a) natural -> %a)) (check-expect (n-th prim4 0) 2) (check-expect (n-th (list 1 2 3 4) 3) 4) #;(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 is too short")) ((cons? xs) ... (first xs) ... ... (n-th (rest xs) n) ...)))) #;(define n-th (lambda (xs n) (cond ((empty? xs) (violation "n-th: list is too short")) ((cons? xs) (if (= n 0) ... (first xs) ... ... (n-th (rest xs) n) ...))))) #;(define n-th (lambda (xs n) (cond ((empty? xs) (violation "n-th: list is too short")) ((cons? xs) (if (= n 0) (first xs) ... (n-th (rest xs) n) ...))))) (define n-th (lambda (xs n) (cond ((empty? xs) (violation "n-th: list is too short")) ((cons? xs) (if (= n 0) (first xs) (n-th (rest xs) (- n 1))))))) ; ADT-Gesetz: Listenlängen addieren sich bei Konkatenation (check-property (for-all ((xs (list-of number)) (ys (list-of number))) (expect (list-length (concatenate xs ys)) (+ (list-length xs) (list-length ys)))))