;; 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-beginner-reader.ss" "deinprogramm")((modname property_rat) (read-case-sensitive #f) (teachpacks ()) (deinprogramm-settings #(#f write repeating-decimal #f #t none explicit #f ()))) ; Schnittstelle rationaler Zahlen: (provide rat make-rat rat? rat-numerator rat-denominator +rat *rat) ; Eine rationale Zahl besteht aus ; - einem Zähler (integer) ; - einem Nenner (integer) (define-record-procedures rat make-rat-record ; Konstruktor rat? (rat-numerator rat-denominator)) (: make-rat-record (integer integer -> rat)) (: rat? (any -> boolean)) (: rat-numerator (rat -> integer)) (: rat-denominator (rat -> integer)) ; Erzeuge rationale Zahl in gekürzter Darstellung: (: make-rat (integer integer -> rat)) (define make-rat (lambda (x y) (make-rat-record (/ x (gcd x y)) (/ y (gcd x y))))) (define one-third (make-rat 1 3)) (define two-third (make-rat 2 3)) (define one-ninth (make-rat 1 9)) ; Berechne das Produkt zweier rationaler Zahlen (: *rat (rat rat -> rat)) (check-expect (*rat one-third one-third) one-ninth) #;(define *rat (lambda (r1 r2) ...(rat-numerator r1) ...(rat-denominator r1) ...(rat-numerator r2) ...(rat-denominator r2) ...)) #;(define *rat (lambda (r1 r2) (make-rat ...(rat-numerator r1) ...(rat-denominator r1) ...(rat-numerator r2) ...(rat-denominator r2) ...))) (define *rat (lambda (r1 r2) (make-rat (* (rat-numerator r1) (rat-numerator r2)) (* (rat-denominator r1) (rat-denominator r2))))) ; Berechne die Summer zweier rationaler Zahlen (: +rat (rat rat -> rat)) (check-expect (+rat one-third one-third) two-third) #;(define +rat (lambda (r1 r2) (make-rat ...(rat-numerator r1) ...(rat-denominator r1) ...(rat-numerator r2) ...(rat-denominator r2) ...))) (define +rat (lambda (r1 r2) (make-rat (+ (* (rat-numerator r1) (rat-denominator r2)) (* (rat-numerator r2) (rat-denominator r1))) (* (rat-denominator r1) (rat-denominator r2))))) ; ADT-Gesetz testen: (check-property (for-all ((n integer) (d integer)) (==> (not (= d 0)) (= (/ (rat-numerator (make-rat n d)) (rat-denominator (make-rat n d))) (/ n d)))))