Commit 69a9085c authored by rokka's avatar rokka

ueb7

parent fb9a1f6d
;; 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-intermediate-lambda-reader.ss" "lang")((modname A71) (read-case-sensitive #t) (teachpacks ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp")) #f)))
; (Number -> Number) Number -> Number
; compute the sum of the given function f to running from 1 to n
(check-expect (sumTo add1 5) 20)
(check-expect (sumTo + 2) 3)
(check-expect (sumTo - 3) -6)
(define (sumTo f n)
(cond
[(= n 1) (f n)]
[else (+ (f n) (sumTo f (sub1 n)))]
)
)
; (Number -> Number) Number Number Number -> Number
; approximate the integral a~b f(x)dx with the sum f(a + k * dx) with dx = b - a / n
; where f is f(x), a upper border, b lower border and n the count of approximation steps
(check-expect (integral add1 1 3 4) 6)
(check-expect (integral add1 1 2 1) 2)
(check-expect (integral - 1 3 4) -4)
(check-expect (integral - 1 3 100) -4)
(define (integral f a b n)
(local (; (Number -> Number) Number Number Number -> Number
; does the recursives steps k
(define (integral-recursiv f a b n k)
; dx is a constant that changes every step, so it is defined locally for every step
(local (; dx is a number
; dx is defined as (b - a / n)
(define DX
(/ (- b a) n)
)
)
(cond
[(zero? k) (* (f (* (+ a k) DX )) DX)]
[else (integral-recursiv f a b (sub1 n) (sub1 k))]
)
)
)
)
; set max steps to n - 1
(integral-recursiv f a b n (sub1 n))
)
)
\ 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-intermediate-lambda-reader.ss" "lang")((modname A71) (read-case-sensitive #t) (teachpacks ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp")) #f)))
; (Number -> Number) Number -> Number
; compute the sum of the given function f to running from 1 to n
(check-expect (sumTo add1 5) 20)
(check-expect (sumTo + 2) 3)
(check-expect (sumTo - 3) -6)
(define (sumTo f n)
(cond
[(= n 1) (f n)]
[else (+ (f n) (sumTo f (sub1 n)))]
)
)
; (Number -> Number) Number Number Number -> Number
; approximate the integral a~b f(x)dx with the sum f(a + k * dx) with dx = b - a / n
; where f is f(x), a upper border, b lower border and n the count of approximation steps
(check-expect (integral add1 1 3 4) 6)
(check-expect (integral add1 1 2 1) 2)
(check-expect (integral - 1 3 4) -4)
(check-expect (integral - 1 3 100) -4)
(define (integral f a b n)
(local (; Number Number Number -> Number
; helper to compute dx = (b - a)/ n
(define (dx a b n)
(/ (- b a) n)
)
)
(local (; (Number -> Number) Number Number Number -> Number
; does the recursives steps k
(define (integral-recursiv f a b n k)
(cond
[(zero? k) (* (f (* (+ a k) (dx a b n))) (dx a b n))]
[else (integral-recursiv f a b (sub1 n) (sub1 k))]
)
)
)
; set max steps to n - 1
(integral-recursiv f a b n (sub1 n))
))
)
\ 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-intermediate-lambda-reader.ss" "lang")((modname A72) (read-case-sensitive #t) (teachpacks ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp")) #f)))
; [X, Y, Z] (X Y -> Z) X -> (Y -> Z)
; fixes the first paramter of an binary function f to x and returns new unary function
(check-expect ((partial + 2) 2) 4)
(check-expect ((partial - 2) 2) 0)
(define (partial f x)
(λ (y)
(f x y)
)
)
; [X] Number (X -> X) -> (X -> X)
; returns a functions which runs f n-times
(check-expect ((repeated 3 add1) 0) 3)
(check-expect ((repeated 3 sub1) 0) -3)
(define (repeated n f)
(λ (x)
(cond
[(= n 1) (f x)]
[else (f ((repeated (sub1 n) f) x))]
)
)
)
; [X, Y, Z] (X -> Y) (Y -> Z) -> (X -> Z)
; does a function composition f and g -> f(g(x))
(check-expect ((combine sub1 add1) 0) 0)
(check-expect ((combine add1 add1) 0) 2)
(check-expect ((combine sub1 sub1) 0) -2)
(define (combine f g)
(λ (x)
(f (g x))
)
)
\ 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-intermediate-lambda-reader.ss" "lang")((modname A72) (read-case-sensitive #t) (teachpacks ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp")) #f)))
; [X, Y, Z] (X Y -> Z) X -> (Y -> Z)
; fixes the first paramter of an binary function f to x and returns new unary function
(check-expect ((partial + 2) 2) 4)
(check-expect ((partial - 2) 2) 0)
(define (partial f x)
(λ (y) (f x y)))
; [X] Number (X -> X) -> (X -> X)
; returns a functions which runs f n-times
(check-expect ((repeated 3 add1) 0) 3)
(check-expect ((repeated 3 sub1) 0) -3)
(define (repeated n f)
(λ (x)
(cond
[(= n 1) (f x)]
[else (f ((repeated (sub1 n) f) x))]
)
)
)
; [X, Y, Z] (X -> Y) (Y -> Z) -> (X -> Z)
; does a function composition f and g -> f(g(x))
(check-expect ((combine sub1 add1) 0) 0)
(check-expect ((combine add1 add1) 0) 2)
(check-expect ((combine sub1 sub1) 0) -2)
(define (combine f g)
(λ (x)
(f (g x))))
\ 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-intermediate-lambda-reader.ss" "lang")((modname A73) (read-case-sensitive #t) (teachpacks ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp")) #f)))
; [X,Y] (X -> Y) (list-of X) -> (list-of Y)
; maps the given list l via the given function f to a new list
(check-expect (my-map add1 `(1 1 1)) `(2 2 2))
(check-expect (my-map (λ (x) (> x 5)) `(1 3 6)) `(#false #false #true))
(define (my-map f l)
(foldr (λ (x r)
(cons (f x) r))
empty
l)
)
; [X] (X -> Boolean) (list-of X) -> (list-of X)
; filters the list l for the predicate p
(check-expect (my-filter string? `(1 "test" 3 "GNU" 'a)) `("test" "GNU"))
(check-expect (my-filter number? `(1 "GNU" 2 'b)) `(1 2))
(define (my-filter p l)
(foldr
(λ (x r)
(cond
[(p x) (cons x r)]
[else r]
)
)
empty
l
)
)
; [X] (X -> Boolean) (list-of X) -> Boolean
; checks if p is true for all elements of l
(check-expect (for-all string? `("test" "tes" "te")) #true)
(check-expect (for-all string? `("test" "tes" "te" 3)) #false)
(define (for-all p l)
(cond
[(= (length l) (length (my-filter p l))) #true]
[else #false]))
; [X] (X -> Boolean) (list-of X) -> Boolean
; checks if there is at leasat on element in list l, for which predicate p is true
(check-expect (exists string? `(2 3 "test" 4)) #true)
(check-expect (for-all string? `(1 2 3)) #false)
(define (exists p l)
(cond
[(zero? (length (my-filter p l))) #false]
[else #true]))
\ 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-intermediate-lambda-reader.ss" "lang")((modname A73) (read-case-sensitive #t) (teachpacks ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "universe.rkt" "teachpack" "2htdp") (lib "image.rkt" "teachpack" "2htdp")) #f)))
; [X,Y] (X -> Y) (list-of X) -> (list-of Y)
; maps the given list l via the given function f to a new list
(check-expect (my-map add1 `(1 1 1)) `(2 2 2))
(check-expect (my-map (λ (x) (> x 5)) `(1 3 6)) `(#false #false #true))
(define (my-map f l)
(foldr (λ (x r)
(cons (f x) r))
empty
l)
)
; [X] (X -> Boolean) (list-of X) -> (list-of X)
; filters the list l for the predicate p
(check-expect (my-filter string? `(1 "test" 3 "GNU" 'a)) `("test" "GNU"))
(check-expect (my-filter number? `(1 "GNU" 2 'b)) `(1 2))
(define (my-filter p l)
(foldr
(λ (x r)
(cond
[(p x) (cons x r)]
[else r]
)
)
empty
l)
)
; [X] (X -> Boolean) (list-of X) -> Boolean
; checks if p is true for all elements of l
(check-expect (for-all string? `("test" "tes" "te")) #true)
(check-expect (for-all string? `("test" "tes" "te" 3)) #false)
(define (for-all p l)
(cond
[(= (length l) (length (my-filter p l))) #true]
[else #false]))
; [X] (X -> Boolean) (list-of X) -> Boolean
; checks if there is at leasat on element in list l, for which predicate p is true
(check-expect (exists string? `(2 3 "test" 4)) #true)
(check-expect (for-all string? `(1 2 3)) #false)
(define (exists p l)
(cond
[(zero? (length (my-filter p l))) #false]
[else #true]))
\ 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