Funkcionálne programovanie cvičenie 1
Created: 2009-09-23 - 11:25
;klasicke aritmeticke operacie
(+ 5 3)
;definovanie procedur
(define (square x)
(* x x))
(define (sum-of-squares x y)
(* (square x) (square y)))
(< 3 4)
;priklad - zapisat vyraz 5<=x<10 v 3<=4
;vrati pravdivostnu hodnotu #t ako true alebo #f ako false
(define (p? x)
(or (< 5 x 10)
(= 5 x)
(< 3 x 4)
(= x 4)))
;alebo
(define (p? x)
(or (and (<= 5 x)
(< x 10))
(and (< 3 x)
(<= x 4))
)
)
;definujte
; f(x) = x*x // ak x < -3
; = x*x + x // ak x patri [-3,3]
; = x*x + 2*x // inak
(define (f x)
(if (< x -3)
(square x)
(if (<= x 3) ;tu staci aj toto, lebo sa vetva je mensie ako -3 nesplnila
(+ (square x) x)
(+ (square x) (* 2 x)))))
;alebo
(define (f x)
(cond ( (< x -3) (square x))
( (<= x 3) (+ (square x) x)
(else (+ (square x) (* 2 x)))))); monika vravi, ze to je zle a tu je jej riesenie: :)
(define (f x)
(cond ( (< x -3) (square x))
( (<= x 3) (+ (square x) x))
(else (+ (square x) (* 2 x)))))
; na d.u. definovat nejaku funkciu... teda tuto: :D
; f(x y) = x*x*y + x*y*y // ak x+y < 2
; = 2*x*y // ak 2 <= x + y <=3
; = x*y // ak 3
(define (f x)
(cond ( (< x -3) (square x))
( (<= x 3) (+ (square x) x))
(else (+ (square x) (* 2 x)))))
(cond ( (< x -3) (square x))
( (<= x 3) (+ (square x) x))
(else (+ (square x) (* 2 x)))))