Commit e5eedb62 authored by rokka's avatar rokka

ueb9

parent 69a9085c
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-advanced-reader.ss" "lang")((modname A81) (read-case-sensitive #t) (teachpacks ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp")) #f)))
; [X] (list-of X) -> Boolean
; Check whether a list has an even amount of elements
(check-expect (list/even? (list 1 2 3)) #false)
(check-expect (list/even? empty) #true)
(check-expect (list/even? (list 1 2 3 4)) #true)
(define (list/even? l)
(match l
[empty #true]
[(cons x empty) #false]
[(cons x (cons y z)) (list/even? z)]
)
)
; (list-of Number) -> (list-of Posn)
; Converts a list with an even amount of numbers
; (x1 y1 x2 y2 ... xn yn) into a list of posn instances
; pairs of the form (0 0) are omitted.
(check-expect (flat->posn (list 1 2 3 4)) (list (make-posn 1 2) (make-posn 3 4)))
(check-expect (flat->posn (list 0 0)) empty)
(check-expect (flat->posn (list 1 2 0 0 3 4)) (list (make-posn 1 2) (make-posn 3 4)))
(check-expect (flat->posn (list 1 2 3)) empty)
(check-expect (flat->posn empty) empty)
(define (flat->posn l)
(match l
[empty empty]
[(cons 0 ( cons 0 y)) (flat->posn y)]
[(cons x ( cons y z))
(cond
[(list/even? l) (cons (make-posn x y) (flat->posn z))]
[else empty]
)
]
)
)
; A Tree is either:
; - (make-tree value Tree Tree)
; - false
(define-struct tree (value left right))
; Tree -> Number
; Computes the height of a tree
;(check-expect (tree/height (make-tree (make-tree (make-tree #false #false) #false)
(check-expect (tree/height (make-tree 2
(make-tree 2 #false #false) (make-tree 2 #false #false))) 2)
(check-expect (tree/height (make-tree 1
(make-tree 2
(make-tree 3 #false #false) (make-tree 3 #false #false))
(make-tree 2 #false #false))) 3)
(check-expect (tree/height (make-tree 2 #false #false)) 1)
(check-expect (tree/height false) 0)
(define (tree/height t)
(match t
[false 0]
[tree (add1 (max (tree/height (tree-left t)) (tree/height (tree-right t))))]
)
)
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-advanced-reader.ss" "lang")((modname A81) (read-case-sensitive #t) (teachpacks ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp")) #f)))
; [X] (list-of X) -> Boolean
; Check whether a list has an even amount of elements
(check-expect (list/even? (list 1 2 3)) #false)
(check-expect (list/even? empty) #true)
(check-expect (list/even? (list 1 2 3 4)) #true)
(define (list/even? l)
(match l
[empty #true]
[(cons x empty) #false]
[(cons x (cons y z)) (list/even? z)]
)
)
;(list-of Number) -> (list-of Posn)
; Converts a list with an even amount of numbers
; (x1 y1 x2 y2 ... xn yn) into a list of posn instances
; pairs of the form (0 0) are omitted.
(check-expect (flat->posn (list 1 2 3 4)) (list (make-posn 1 2) (make-posn 3 4)))
(check-expect (flat->posn (list 0 0)) empty)
(check-expect (flat->posn (list 1 2 0 0 3 4)) (list (make-posn 1 2) (make-posn 3 4)))
(check-expect (flat->posn (list 1 2 3)) empty)
(check-expect (flat->posn empty) empty)
(define (flat->posn l)
(match l
[empty empty]
[(cons 0 ( cons 0 y)) (flat->posn y)]
[(cons x ( cons y z))
(cond
[(list/even? l) (cons (make-posn x y) (flat->posn z))]
[else empty]
)
]
)
)
; A Tree is either:
; - (make-tree value Tree Tree)
; - false
(define-struct tree (value left right))
; Tree -> Number
; Computes the height of a tree
;(check-expect (tree/height (make-tree (make-tree (make-tree #false #false) #false)
(check-expect (tree/height (make-tree 2
(make-tree 2 #false #false) (make-tree 2 #false #false))) 2)
(check-expect (tree/height (make-tree 2
(make-tree 2
(make-tree 2 #false #false)
(check-expect (tree/height (make-tree 2 #false #false)) 1)
(check-expect (tree/height false) 0)
(define (tree/height t)
(match t
[false 0]
[tree (add1 (max (tree/height (tree-left t)) (tree/height (tree-right t))))]
)
)
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-advanced-reader.ss" "lang")((modname A82) (read-case-sensitive #t) (teachpacks ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp")) #f)))
; Number Number -> Number
; Computes the sum from m to n
(define (sumFromTo m n)
(cond [(> m n) 0]
[else (+ m (sumFromTo (add1 m) n))]
)
)
; Die Abbruchbedingung (> m n) wird immer erreicht, denn jeder rekursive Aufruf erfolgt
; mit m = m + 1 und n = n. D.h. zu m wird jedesmal 1 addiert, während n unverändert bleibt.
; Folglich wird die Abbruchbedinung (> m n) immer irgendwann erreicht,
; falls Sie nicht schon bei Aufruf z.B. (sumFromTo 10 9) gilt.
; Somit terminiert die Funktion immer.
; Sein m.n Natürliche Zahlen mit m < n
; Sei f: N, N -> N , f(m,n) = n - m
; Es gilt: f(m + 1, n) < f(m,n), folglich fällt die Funktion strikt monoton mit jedem Aufruf.
; Number Number -> Number
; Computes the binomial coefficient of n and k
; if 0 <= k <= n
(define (binomi n k)
(cond [(or (= 0 k) (= n k)) 1]
[else (+ (binomi (sub1 n) (sub1 k))
(binomi (sub1 n) k) )]
)
)
; Die Abbruchbedingung (or (= 0 k) (= n k)) wird stehts erreicht, denn jeder rekursive Aufruf erfolgt
; entweder mit n = n - 1, k = k - 1 oder mit n = n - 1, k = k. Jede Variante bringt
; die Abbruchbedingung mit jedem Schritt näher, bis Sie schließlich erreicht wird.
; Folglich terminiert die Funktion immer.
; Aufrufvariante 1:
; Sein n, k natürliche Zahlen mit 0 <= k <= n
; Sei f: N, N, -> N, f(n,k) = k - n
; Es gilt: f(n - 1, k) < f(n,k) folglich fällt die Funktion strikt monoton mit jedem Aufruf
; Aufrufvariante 2:
; Sei g: N, N -> N g(n,k) = 0 + k
; Es gilt: g(n - 1, k - 1) < g(n,k) folglich fällt die Funktion strikt monton mit jedem Aufruf
; Number -> Number
; Computes the greatest natural number t with (t * t) <= x
(define (sqrt/nat x)
(local [(define (sqrt/above x t)
(if (> (* t t) x) (sub1 t)
(sqrt/above x (add1 t))))]
(sqrt/above x 1)))
; Die Abbruchbedingung (> (* t t) x) wird stehts erreicht, weil mit jedem Aufruf 1 zu t addiert wird,
; während x unverändert bleibt, daher kommt die Abbruchbedingungen mit jedem Schritt näher.
; Sei f: N,N -> N f(t, x) = x - t
; Es gilt f(t + 1, x) < f(t, x) folglich ist die Funktion strikt monton fallend mit jedem Aufruf
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-advanced-reader.ss" "lang")((modname A82) (read-case-sensitive #t) (teachpacks ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp")) #f)))
; Number Number -> Number
; Computes the sum from m to n
(define (sumFromTo m n)
(cond [(> m n) 0]
[else (+ m (sumFromTo (add1 m) n))]
)
)
; Die Abbruchbedingung (> m n) wird immer erreicht, denn jeder rekursive Aufruf erfolgt
; mit m = m + 1 und n = n. D.h. zu m wird jedesmal 1 addiert, während n unverändert bleibt.
; Folglich wird die Abbruchbedinung (> m n) immer irgendwann erreicht,
; falls Sie nicht schon bei Aufruf z.B. (sumFromTo 10 9) gilt.
; Somit terminiert die Funktion immer.
; Number Number -> Number
; Computes the binomial coefficient of n and k
; if 0 <= k <= n
(define (binomi n k)
(cond [(or (= 0 k) (= n k)) 1]
[else (+ (binomi (sub1 n) (sub1 k))
(binomi (sub1 n) k) )]
)
)
; Die Abbruchbedingung (or (= 0 k) (= n k)) wird stehts erreicht, denn jeder rekursive Aufruf erfolgt
; entweder mit n = n - 1, k = k - 1 oder mit n = n - 1, k = k. Jede Variante bringt
; die Abbruchbedingung mit jedem Schritt näher, bis Sie schließlich erreicht wird.
; Folglich terminiert die Funktion immer.
; Number -> Number
; Computes the greatest natural number t with (t *t) <= x
(define (sqrt/nat x)
(local [(define (sqrt/above x t)
(if (> (* t t) x) (sub1 t))
(sqrt/above x (add1 t)))]
(sqrt/above x 1)))
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-advanced-reader.ss" "lang")((modname A83) (read-case-sensitive #t) (teachpacks ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp")) #f)))
(require 2htdp/abstraction)
; Number -> Number
; computes the n -th element of the fibonacci sequence
(check-expect (fib 1) 1)
(check-expect (fib 2) 1)
(check-expect (fib 3) 2)
(check-expect (fib 4) 3)
(check-expect (fib 30) 832040)
(define (fib n)
(match n
[1 1]
[2 1]
[_ (+ (fib (sub1 n)) (fib (- n 2)))]
)
)
(check-expect (fib/fast 1) 1)
(check-expect (fib/fast 2) 1)
(check-expect (fib/fast 3) 2)
(check-expect (fib/fast 4) 3)
(check-expect (fib/fast 30) 832040)
(define (fib/fast n)
(local
[(define (fib/fast-acc n n-1 n-2)
(match n
[1 1]
[2 1]
[_ (fib/fast-acc (sub1 n) n-1
(fib/fast-acc n 0 1)))
\ No newline at end of file
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-advanced-reader.ss" "lang")((modname A91) (read-case-sensitive #t) (teachpacks ((lib "image.rkt" "teachpack" "2htdp") (lib "universe.rkt" "teachpack" "2htdp") (lib "abstraction.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "image.rkt" "teachpack" "2htdp") (lib "universe.rkt" "teachpack" "2htdp") (lib "abstraction.rkt" "teachpack" "2htdp")) #f)))
; A Reference is either:
; -Buch (buch)
; -Zeitungsartikel (artikel)
; -Konferenzbeitrag (konferenz)
; interp. References from above named.
(define-type Reference
(buch (autor string?)
(titel string?)
(verlag string?)
(ort string?)
(jahr number?)
)
(artikel (autor string?)
(titel string?)
(zeitschriftsname string?)
(jahr number?)
)
(konferenz (autor string?)
(titel string?)
(konferenzname string?)
(verlag string?)
(ort string?)
(jahr number?)
)
)
; Reference -> String
; Converts a Reference to a String.
(check-expect (cite (make-buch "Autor" "Titel" "Verlag" "Ort" 2000))
"Autor. Titel. Verlag, Ort 2000")
(check-expect (cite (make-artikel "Autor" "Titel" "Name der Zeitschrift" 2000))
"Autor. Titel. Name der Zeitschrift, 2000")
(check-expect (cite (make-konferenz "Autor" "Titel" "Name der Konferenz" "Verlag" "Ort" 2000))
"Autor. Titel. In Name der Konferenz. Verlag, Ort 2000")
(define (cite ref)
(type-case Reference ref
[buch (autor titel verlag ort jahr)
(string-append autor ". "
titel ". "
verlag ", "
ort " "
(number->string jahr)
)
]
[artikel (autor titel zeitschriftsname jahr)
(string-append autor ". "
titel ". "
zeitschriftsname ", "
(number->string jahr)
)
]
[konferenz (autor titel konferenzname verlag ort jahr)
(string-append autor ". "
titel ". "
"In " konferenzname ". "
verlag ", "
ort " "
(number->string jahr)
)
]
)
)
\ No newline at end of file
;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-advanced-reader.ss" "lang")((modname A91) (read-case-sensitive #t) (teachpacks ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp")) #f)))
(require 2htdp/abstraction)
; cite-help is a struct of the categories
; bücher
; artikel aus zeitschriften
; konferenzbeiträgen
(define-type cite-help
(buch (autor string?) (titel string?) (ort-jahr string?))
(artikel (autor string?) (titel string?) (magazin string?) (jahr string?))
(konferenzbeiträge (autor string?) (titel string?) (konferenz string?) (verlag string?) (ort-jahr string?))
)
(define BUCH (make-buch "AUTOR" "TITEL" "ORT JAHR"))
(define (cite reference)
(type-case cite-help reference
[buch (autor titel ort-jahr) (string-append autor", " titel", " ort-jahr".")]
[artikel (autor titel magazin jahr) (string-append autor", " titel", " magazin", " jahr".")]
[konferenzbeiträge (autor titel konferenz verlag ort-jahr)
(string-append autor", " titel", " konferenz", " verlag", " ort-jahr".")]
)
)
(cite (make-buch "AUTOR" "TITEL" "ORT JAHR"))
(cite (make-artikel "AUTOR" "TITEL" "MAGAZIN" "JAHR"))
(cite (make-konferenzbeiträge "AUTOR" "TITEL" "KONFERENZ" "VERLAG" "ORT-JAHR"))
#lang racket
(require test-engine/racket-tests)
(provide
(contract-out
[lst-append (-> (listof number?) (listof number?) (listof number?))]
)
)
; Fügt die Liste l2 hinter die List l1 an
(check-expect (lst-append '(1 2) '(4 3)) '(1 2 4 3))
(check-expect (lst-append '(1 2) '()) '(1 2))
(define (lst-append l1 l2)
(match l1
['() (first l2)]
[(cons x '()) (cons x l2)]
[(cons x xs) (cons x (lst-append xs l2))]
)
)
(provide
(contract-out
[f (-> (-> number? number? boolean?) pair? boolean?)]
)
)
; Gibt das Resultat zurück, dass sich aus dem Vergleich der Beiden
; Elemente in pair mittels dem Operator comparator ergibt.
(check-expect (f = '(1 2)) false)
(define (f comparator pair)
(comparator (first pair) (second pair)))
(test)
\ No newline at end of file
#lang racket
(require test-engine/racket-tests)
(require typed/test-engine/racket-tests)
(provide
(contract-out
[lst-append (-> (listof number?) (listof number?) (listof number?))]
)
)
; Fügt die Liste l2 hinter die List l1 an
(check-expect (lst-append '(1 2) '(4 3)) '(1 2 4 3))
(check-expect (lst-append '(1 2) '()) '(1 2))
(define (lst-append l1 l2)
(match l1
['() (first l2)]
[(cons x '()) (cons x l2)]
[(cons x xs) (cons x (lst-append xs l2))]
)
)
(provide
(contract-out
[f (-> (-> number? number? boolean?) pair? boolean?)]
)
)
; Gibt das Resultat zurück, dass sich aus dem Vergleich der Beiden
; Elemente in pair mittels dem Operator comparator ergibt.
(check-expect (f = '(1 2)) false)
(define (f comparator pair)
(comparator (first pair) (second pair)))
(test)
\ No newline at end of file
#lang racket
(require test-engine/racket-tests)
(provide
(contract-out
[lst-append (-> (listof number?) (listof number?) (listof number?))]
)
)
; Fügt die Liste l2 hinter die List l1 an
(check-expect (lst-append '(1 2) '(4 3)) '(1 2 4 3))
(check-expect (lst-append '(1 2) '()) '(1 2))
(define (lst-append l1 l2)
(match l1
['() (first l2)]
[(cons x '()) (cons x l2)]
[(cons x xs) (cons x (lst-append xs l2))]
)
)
(provide
(contract-out
[f (-> (-> number? number? boolean?) pair? boolean?)]
)
)
; Gibt das Resultat zurück, dass sich aus dem Vergleich der Beiden
; Elemente in pair mittels dem Operator comparator ergibt.
(check-expect (f = '(1 2)) false)
(define (f comparator pair)
(comparator (first pair) (second pair)))
(test)
(lst-append '("Violiating" "the" "Contract") '("with" "Listof" "string"))
\ No newline at end of file
#lang racket
(require test-engine/racket-tests)
(provide
(contract-out
[lst-append (-> (listof number?) (listof number?) (listof number?))]
)
)
; Fügt die Liste l2 hinter die List l1 an
(check-expect (lst-append '(1 2) '(4 3)) '(1 2 4 3))
(check-expect (lst-append '(1 2) '()) '(1 2))
(define (lst-append l1 l2)
(match l1
['() (first l2)]
[(cons x '()) (cons x l2)]
[(cons x xs) (cons x (lst-append xs l2))]
)
)
(provide
(contract-out
[f (-> (-> number? number? boolean?) pair? boolean?)]
)
)
; Gibt das Resultat zurück, dass sich aus dem Vergleich der Beiden
; Elemente in pair mittels dem Operator comparator ergibt.
(check-expect (f = '(1 2)) false)
(define (f comparator pair)
(comparator (first pair) (second pair)))
(test)
\ No newline at end of file
#lang typed/racket
(require typed/test-engine/racket-tests)
; Fügt die Liste l2 hinter die List l1 an
(check-expect (lst-append '(1 2) '(4 3)) '(1 2 4 3))
(check-expect (lst-append '(1 2) '()) '(1 2))
(: lst-append (-> (Listof Number) (Listof Number) (Listof Number)))
(define (lst-append l1 l2)
(match l1
['() l2] ; first liefert Number, muss aber Listof Number liefern
[(cons x '()) (cons x l2)]
[(cons x xs) (cons x (lst-append xs l2))]
)
)
; Gibt das Resultat zurück, dass sich aus dem Vergleich der Beiden
; Elemente in pair mittels dem Operator comparator ergibt.
(check-expect (f = '(1 2)) false)
(: f (-> (-> Integer Integer Boolean) (Listof Integer) Boolean))
(define (f comparator pair)
(comparator (first pair) (second pair)))
(test)
\ No newline at end of file
#lang typed/racket
(require typed/test-engine/racket-tests)
; Fügt die Liste l2 hinter die List l1 an
(check-expect (lst-append '(1 2) '(4 3)) '(1 2 4 3))
(check-expect (lst-append '(1 2) '()) '(1 2))
(define (lst-append l1 l2)
(match l1
['() (first l2)]
[(cons x '()) (cons x l2)]
[(cons x xs) (cons x (lst-append xs l2))]
)
)
; Gibt das Resultat zurück, dass sich aus dem Vergleich der Beiden
; Elemente in pair mittels dem Operator comparator ergibt.
(check-expect (f = '(1 2)) false)
(define (f comparator pair)
(comparator (first pair) (second pair)))
(test)
\ No newline at end of file
#lang typed/racket
(require typed/test-engine/racket-tests)
; Fügt die Liste l2 hinter die List l1 an
(check-expect (lst-append '(1 2) '(4 3)) '(1 2 4 3))
(check-expect (lst-append '(1 2) '()) '(1 2))
(: lst-append (-> (Listof Number) (Listof Number) (Listof Number)))
(define (lst-append l1 l2)
(match l1
['() l2] ; first liefert Number, muss aber Listof Number liefern
[(cons x '()) (cons x l2)]
[(cons x xs) (cons x (lst-append xs l2))]
)
)
; Gibt das Resultat zurück, dass sich aus dem Vergleich der Beiden
; Elemente in pair mittels dem Operator comparator ergibt.
(check-expect (f = '(1 2)) false)
(: f (-> (-> Integer Integer Boolean) (Listof Integer) Boolean))
(define (f comparator pair)
(comparator (first pair) (second pair)))
(test)
(lst-append '("Violiating" "the" "Contract") '("with" "listof" "strings"))
; Hier tritt für beide Parameter "type mismatch" auf, d.h. der Typ der Liste, bei diesem Aufruf
; Listof String ("given: (List String String String"), stimmt nicht mit der Signatur, Listof Number
; ("expected: (Listof Number)", überein.
(f = '(1.2 29))
; Hier auch "type mismatch" weil die Liste nicht nur Integers enthält sondern auch eine Float.
\ No newline at end of file
#lang typed/racket
(require typed/test-engine/racket-tests)
; Fügt die Liste l2 hinter die List l1 an
(check-expect (lst-append '(1 2) '(4 3)) '(1 2 4 3))
(check-expect (lst-append '(1 2) '()) '(1 2))
(: lst-append (-> (Listof Number) (Listof Number) (Listof Number)))
(define (lst-append l1 l2)
(match l1
['() l2] ; first liefert Number, muss aber Listof Number liefern
[(cons x '()) (cons x l2)]
[(cons x xs) (cons x (lst-append xs l2))]
)
)
; Gibt das Resultat zurück, dass sich aus dem Vergleich der Beiden
; Elemente in pair mittels dem Operator comparator ergibt.
(check-expect (f = '(1 2)) false)
(: f (-> (-> Integer Integer Boolean) (Listof Integer) Boolean))
(define (f comparator pair)
(comparator (first pair) (second pair)))
(test)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment