#lang racket (define (factorial n) (if (zero? n) 1 (* n (factorial (- n 1))))) ;(factorial 5) ;(map factorial (range 1 11)) (define (factorial-adv n) (apply * (range 2 (+ n 1)))) ;(map factorial-adv (range 1 11)) ; expresie = (f e1 e2 .. en) ;(+ 6 (- (* 5 2) 3) 1) ; (define identificator expresie) (define PI 3.14159265) (define r 5) (define area (* PI r r)) ; functii anonime: (lambda lista-parametri corp) ; λx.x - in Calcul Lambda ;(lambda (x) x) ;λx.λy.(x y) - in Calcul Lambda ;(λx.x λx.y) - in Calcul Lambda ;( (λ (x y) ; (/ (+ x y) 2)) 6 12) ; (define (f x y ... ) corp) (define (arithmetic-mean x y) (/ (+ x y) 2)) (define (sum-of-squares x y) (+ (sqr x) (sqr y))) ;(sum-of-squares (+ 1 2) (* 3 5)) ; perechi ; constructor: cons ; selectori: car, cdr (cons (cons 1 2) 'a) (cons + 3) (car (cons (cons 1 2) 5)) (cdr '(4 . b)) ; liste ; constructori: null, cons, list ; selectori: car, cdr ; alte operații: null?, length, append ;(car (list 1 'a +)) ;(cdr '(2 3 4 5)) ;(null? '()) ;(length (list)) (append (cons 1 '(2)) '(a b)) ; (if conditie rez-then rez-else) ;(if (null? '(1)) ; "lala" ; (- 7 1)) ; (cond (cond1 rez1) (cond2 rez2) ... ) (define L '(1 2 3)) (define val (cond ((null? L) 0) ((null? (cdr L)) (/ 1 0)) (else 'other))) val ; Traducere din axiome ; sum([]) = 0 ; sum(x:l) = x + sum(l) (define (sum L) (if (null? L) 0 (+ (car L) (sum (cdr L))))) (define (my-take L n) (display L) (display n) (newline) (if (or (null? L) (zero? n)) '() (cons (car L) (my-take (cdr L) (sub1 n))))) ; L = '(1 2 3 4 5 6 7) '(2 3 4 5 6 7) ; n = 4 n = 3 ; r = '(1 2 3 4) ; take([], n) = [] ; take(L, 0) = [] ; take((x:l), n) = x : take(l, n-1) (my-take (range 2 10) 20) (my-take (range 2 10) 3)