#lang racket (require (lib "trace.ss")) (define (fact-stack n) (if (zero? n) 1 (* n (fact-stack (- n 1))))) (trace fact-stack) ;(fact-stack 10) (define (fact-tail n) (fact-tail-helper n 1)) (define (fact-tail-helper n acc) (if (zero? n) acc (fact-tail-helper (- n 1) (* n acc)))) (trace fact-tail-helper) ;(fact-tail 10) (define (fibo-stack n) (if (< n 2) n (+ (fibo-stack (- n 1)) (fibo-stack (- n 2))))) ;(trace fibo-stack) ;(fibo-stack 5) ;(fibo-stack 40) ;; deja incepe sa dureze mult (define (fibo-tail n) (fibo-tail-helper n 0 1)) ;0 1 (0+1) ; 1 1 (1+1) ; 1 2 (define (fibo-tail-helper n a b) (if (zero? n) a (fibo-tail-helper (- n 1) b (+ a b)))) ;(trace fibo-tail-helper) ;(fibo-tail 5) ;(map fibo-tail '(0 1 2 3 4 5 6 7)) ;(fibo-tail 100000) ; recunoaștere tip de recursivitate (define (g L result) (cond ((null? L) result) ((list? (car L)) (g (cdr L) (append (g (car L) '()) result))) ; evaluare aplicativă (else (g (cdr L) (cons (car L) result))))) (trace g) (g '(1 2 (3 4) (5 (6 7))) '())