83 lines
1.8 KiB
Scheme
83 lines
1.8 KiB
Scheme
;; naloga 1.11
|
|
|
|
(define (rec-f n)
|
|
(if (< n 3) n
|
|
(+ (rec-f (- n 1)) (* 2 (rec-f (- n 2))) (* 3 (rec-f (- n 3))))
|
|
)
|
|
)
|
|
|
|
;; iterativno - nosimo rezultat naprej
|
|
;; narobe
|
|
(define (iter-f a b c d cnt)
|
|
(if (= cnt 0)
|
|
a
|
|
(iter-f (+ b (* 2 c) (* 3 d)) a b c (- cnt 1))
|
|
)
|
|
)
|
|
|
|
(define (ief n) (iter-f 4 2 1 0 n))
|
|
;; poskus 2
|
|
(define (itr-f a b c cnt)
|
|
(if (= cnt 0) a
|
|
(itr-f (+ a (* 2 b) (* 3 c)) a b (- cnt 1))
|
|
)
|
|
)
|
|
(define (ief2 n) (itr-f 2 1 0 n))
|
|
|
|
;; naloga 1.12
|
|
;; pascalov trikotnik - rekurzivna funkcija
|
|
|
|
;; https://spritely.institute/static/papers/scheme-primer.html
|
|
|
|
(define (build-tree depth)
|
|
(if (= depth 0)
|
|
'(0)
|
|
(list depth
|
|
(build-tree (- depth 1))
|
|
(build-tree (- depth 1))
|
|
)
|
|
)
|
|
)
|
|
;; mam seznam in ubistvu samo sestejem dve sosednji stevilki v seznamu
|
|
;; (car sez)
|
|
|
|
(define (nov-seznam sez)
|
|
(define (prvi sez) (car sez))
|
|
(define (drugi sez) (if (null? (cdr sez)) 0 (car (cdr sez))) )
|
|
(if (= 0 (drugi sez))
|
|
sez
|
|
(cons (+ (prvi sez) (drugi sez)) (nov-seznam (cdr sez)))
|
|
)
|
|
)
|
|
|
|
(define (pascal globina seznam)
|
|
(display (make-string globina #\ ))
|
|
(display seznam)
|
|
(display "\n")
|
|
(if (= globina 0)
|
|
seznam
|
|
(pascal (- globina 1) (cons 1 (nov-seznam seznam)))
|
|
)
|
|
)
|
|
|
|
(define (p n) (pascal n '(1 1)))
|
|
|
|
;; stack overflow
|
|
(define (pascal-so x y)
|
|
(define (sub1 z) (- z 1))
|
|
(if (or (zero? y) (= x y))
|
|
1
|
|
(+ (pascal-so (sub1 x) y)
|
|
(pascal-so (sub1 x) (sub1 y)))))
|
|
|
|
;; 1.15 aproksimacije sinusa
|
|
(define (cube x) (* x x x))
|
|
(define (prib x) (- (* 3 x) (* 4 (cube x))))
|
|
;; cnt bi lahko defaultno nastavil na nic, ce bi znal
|
|
(define (sine angle cnt)
|
|
(if (not (> (abs angle) 0.1))
|
|
angle
|
|
(prib (sine (/ angle 3.0) (+ cnt 1)))
|
|
)
|
|
)
|