## Posted By

darkbaron1912 on 08/05/10

# thi lap trinh ham

/ Published in: Scheme

`(define (pitagore a b)  (let ((a2 (* a a)) (b2 (* b b)))    (sqrt (+ a2 b2)))) (define (divisibleBy? number divisor)  (= (remainder number divisor) 0)); kiem tra 1 nam co nhua hay ko(define (isBissextile? year)  (or (divisibleBy? year 400)      (and (divisibleBy? year 4)           (not (divisibleBy? year 100))))) ; tinh giai thua(define (fac n)  (if (zero? n) ; hoáº·c (<= n 0) Ä‘á»ƒ Ä‘áº£m báº£o xá»­ lÃ½ má»i sá»‘ n      1      (* n (fac (- n 1))))); ham fibonaci(define (fib n)  (if (<= n 1)      n      (+ (fib (- n 1)) (fib (- n 2))))) #lang scheme;Cau 1(define (i-fac n M) (                     if (zero? n) M                        (i-fac (- n 1) (* M n))                     ))(define (facz n)(i-fac n 1));Cau 2(define (fib n) (                 if (<= n 1) n                    (+ (fib (- n 1)) (fib (- n 2)))                 ))(define (i-fib n a b) (                     if (<= n 1) a                        (i-fib (- n 1) (+ a b) a)                     ))(define (fibz n)(                 i-fib n 1 0                 ));Cau 3(define (sum_inv n)(                    if (<= n 0) 0                       (+ (/ 1.0 n) (sum_inv (- n 1)))                    ))(define (i-sum_inv n p) (                         if (<= n 0) p                            (i-sum_inv (- n 1) (+ p (/ 1.0 n)))                         ))(define (sum_invz n)(i-sum_inv n 0));Cau 3(define (x^n x n)(                  if (<= n 0) 1                     (* x (x^n x (- n 1)))                  ))(define (i-x^n x n P)(                      if (<= n 0) P                         (i-x^n x (- n 1) (* P x))                      ))(define (x^nz x n) (i-x^n x n 1));Cau 4(define (tohop n k)(                    cond ((= k 0) 1)                         ((= k n) 1)                         (else (+ (tohop (- n 1) k) (tohop (- n 1) (- k 1))))                    ));Cau 5(define (PIover4 n)(                    if (<= n 0) 1                       ;2*n+1                       (+ (/ ( if (= (remainder n 2) 1) -1.0 1.0) (+ (* 2 n) 1)) (PIover4 (- n 1)))                    ))(define (PI) (* 4(PIover4 100000)));Cau 6(define (tongbp n) (                    if (<= n 0) 0                       (+ (* n n) (tongbp (- n 1)))                    ))(define (i-tongbp n S) (                        if (<= n 0) S                           ( i-tongbp (- n 1) (+ (* n n) S))                        ))(define (tongbpz n) (i-tongbp n 0));Cau 7(define (bai7 x y)(                   / (+ (sqrt (+ (* x x) (* y y))) (sqrt (- (* x x) (* y y)))) (+ 1 (sqrt (+ (* x x) (* y y))) (sqrt (- (* x x) (* y y))))                   ))(define (bai7z x y )(                     let (                          (t1 (sqrt (+ (* x x) (* y y))))                          (t2 (sqrt (- (* x x) (* y y))))                          )(/ (+ t1 t2) (+ 1 t1 t2))                     ));cau 8(define (nbsec h m s)(                      + (* h 3600) (* m 60) s                      ));cau 9(define (i-bai9 x S n t e)(                         if (< (abs t) e) S                            (i-bai9 x (+ S (* 1.0 t)) (+ n 1) (* t (/ (* (- (* 2 n) 2) x x) (* (- (* 2 n) 1) 2 n))) e)                         ))(define (bai9 x)(i-bai9 x 1 2 (/ (* x x) 2) 0.000001));cau 10(define (legendre x n) (                        cond ((= n 0) 1)                             ((= n 1) x)                             (else (                                    - ( * 1.0 x (legendre x (- n 1))) (/ (* (+ n 1) (+ n 1) (legendre x (- n 2))) (* (+ (* 2 n) 1) (+ (* 2 n) 3)))                                    ))                        ))   (define (dinhthuc a11 a12 a13 a21 a22 a23 a31 a32 a33)  (- (+ (* a11 a22 a33) (* a12 a23 a31) (* a13 a21 a32))     (+ (* a13 a22 a31) (* a11 a32 a23) (* a21 a33 a12))))(define (hpt3an a1 b1 c1 d1 a2 b2 c2 d2 a3 b3 c3 d3)  (let      ((d  (dinhthuc a1 b1 c1 a2 b2 c2 a3 b3 c3))       (dx (dinhthuc b1 c1 d1 b2 c2 d2 b3 c3 d3))       (dy (dinhthuc a1 d1 c1 a2 d2 c2 a3 d3 c3))       (dz (dinhthuc a1 b1 d1 a2 b2 d2 a3 b3 d3)))    (if (zero? d)        (if (and (zero? dx) (zero? dy) (zero? dz))            (display "He phuong trinh co vo so nghiem")            (display "He phuong trinh vo nghiem"))        (begin           (display "He phuong trinh co nghiem") (newline)          (display "x=") (display (/ dx d)) (newline)          (display "y=") (display (/ dy d)) (newline)          (display "z=") (display (/ dz d))        )    )  ))    (define (bai7a)  (begin    (display "nhap 1 so <0 >1000")    (let ((mynum (read)))     (if ( or (< mynum 0 ) (> mynum 1000))     (bai7a)     (begin        (display " ket qua binh phuong la ")       (display (* mynum mynum))       )     )))); kiem tra co phai nam nhuan ko(define (divisibleBy? number divisor)  (= (remainder number divisor) 0))(define (isBissextile? year)  (or (divisibleBy? year 400)      (and (divisibleBy? year 4)           (not (divisibleBy? year 100))))); bai toan thap ha noi(define (hanoi n A B C)  (if (= n 0)      (display "")      (begin         (hanoi (- n 1) A C B)        (display "move ") (display A) (display " to ") (display C) (newline)        (hanoi (- n 1) B A C))))(define (bai14 x y)  (let ( (tbp (sqrt (+ (* x x) (* y y)))) (hbp (sqrt(- (* x x) (* y y)))))    (/ (- tbp hbp) (+ 1 tbp hbp)))); ham power n x lap(define (iterpower n x kq)  (cond     ((= n 0) kq)    ((> n 0)  (iterpower (- n 1) x (* kq x)))    (else  (iterpower (- 0 n) (/ 1 x) kq)))) (define (power n x)   (iterpower n x 1));power de quy(define (power2 n x)       (if (not (integer? n))             (display "Error")             (if (< n 0)                    (power2 (- 0 n) (/ 1 x))                   (if (= n 0) 1                               (* x (power2 (- n 1) x))                    )                        )        )    );tinh ra so  giay tu (define (nbsec h m s)  (+ (* h 3600) (* m 60 ) s)); tinh giai thua(define (fac n)  (if (zero? n) ; hoáº·c (<= n 0) Ä‘á»ƒ Ä‘áº£m báº£o xá»­ lÃ½ má»i sá»‘ n      1      (* n (fac (- n 1))))); ham fibonaci(define (fib n)  (if (<= n 1)      n      (+ (fib (- n 1)) (fib (- n 2)))))(define (menu) (begin (display "Enter 0 to quit, 1 to job1, 2 to job2.")  (let ((rd (read)))    (cond ((equal? rd 0) (display "Good bye!"))          ((equal? rd 1) ((display "I work the job 1.")                         (newline ) (menu)))          ((equal? rd 2) ((begin (display "I work the job 2.")                          (newline) (menu))))          (else ( (display "restart :un known command.")                (newline) (menu)))))));test do (define a (list 1 2 3))(write (cons a (list 4 5)));danh sach(define (mylength l)  ( if (null? l)       0       (+ 1 (mylength (cdr l))))) (define (myreverse L)  (if (null? L)      '() ; hoáº·c (list)      (append (myreverse (cdr L)) (list (car L))))) ;ViÌ duÌ£ minh hoaÌ£ sau Ä‘aÌ‚y Ä‘iÌ£nh nghiÌƒa moÌ£Ì‚t haÌ€m diÌ£ch caÌc Ä‘aÌ£i tuÌ›Ì€ nhaÌ‚n xuÌ›ng t;tiáº¿ng Anh :(define (pronoun-traduit pronounF)  (case pronounF    ((je) 'I)    ((tu vous) 'you)    ((il) 'he)    ((elle) 'she)    ((nous) 'we)    ((ils) 'they)    (else 'ronoun-inconnu)))(pronoun-traduit 'vous) (define vec '#())(define n 0)(define i 0)(define x 1)(define p 1)(define (Hoocner)(begin(display "Nhap bac n cua da thuc :")(set! n (read))(set! vec (make-vector (+ n 1)))(display "Nhap cac he so cua da thuc :")(newline)(while (<= i n)(begin(display "a") (display i) (display ":")(vector-set! vec i (read))(set! i (+ i 1))))(display "Nhap gia tri x can tinh :")(set! x (read));Bat dau tinh da thuc;i=n(set! i (- n 1));p=a[n](set! p (vector-ref vec n));i=n-->1;p=p*c + a[i](while (>= i 0)(begin(set! p (+ (* p x) (vector-ref vec i)))(set! i (- i 1))))(display "Gia tri cua bieu thuc :") (display p)  ))`