;; 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 ()))) (: prim4 (list-of natural)) #;(define prim4 (cons 2 (cons 3 (cons 5 (cons 7 empty))))) (define prim4 (list 2 3 5 7)) (: hwlist (list-of string)) (define hwlist (list "Hello" "World!")) (: alist (list-of any)) (define alist (list 1 "Hello")) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Länge einer Liste berechnen (: list-length ((list-of %a) -> natural)) (check-expect (list-length empty) 0) (check-expect (list-length prim4) 4) (check-expect (list-length hwlist) 2) (define list-length (lambda (xs) (cond ((empty? xs) 0) ((cons? xs) (+ 1 (list-length (rest xs))))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Elemente zweier Listen sequenziell zusammenfügen (: 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 (list 1 2) (list 3 4)) (list 1 2 3 4)) ; Probiere Fallunterscheidung über die erste Liste: #;(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 bestimmen, wobei n=0 das Kopfelement (erste Element) ist (: n-th ((list-of %a) natural -> %a)) (check-expect (n-th hwlist 0) "Hello") (check-expect (n-th prim4 1) 3) (check-expect (n-th prim4 3) 7) #;(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)))))))