/ Published in: Scheme
Expand |
Embed | Plain Text
;Bo ngoac (define (lamPhangDS L) (cond ((null? L) '()) ((list? (car L)) (append (lamPhangDS (car L)) (lamPhangDS (cdr L)))) (else (cons (car L) (lamPhangDS (cdr L)))))) (lamPhangDS '(a b c (d e) f g (h i (j k) l) m)) ;(list? '(a b)) -------------------------------------------------------------------- ;chuyen thap phan sang nhi phan (define (TP_to_NP x str) (if (zero? x) (string-append str "") (TP_to_NP (quotient x 2) (string-append str (number->string (remainder x 2)))))) (define (inNP x) (list->string(reverse(string->list (TP_to_NP x ""))))) ;goi ham (inNP 14) (define (luyThua x y) ;x^y (dung de quy) (if (= y 0) 1 (* x (luyThua x (- y 1))) ) ) ;;chuyen nhi phan sang thap phan (define (NP_to_TP str2 s) (if (zero? (string-length str2)) s (NP_to_TP (substring str2 1 (string-length str2)) (+ s (* (string->number(substring str2 0 1)) (luyThua 2 (- (string-length str2) 1))))))) (define (inTP str) (NP_to_TP str 0)) ;goi ham (inTP "1110") ------------------------------------------------------------------ (define (even_length L) (if (zero? (remainder (length L) 2)) "Danh sach chan" (odd_length L))) (define (odd_length L) (if (zero? (remainder (length L) 2)) (even_length L) "Danh sach le")) ;goi ham ;(even_length (list 1 2 3 4 5 6)) (odd_length (list 1 2 3 4 5 6)) ------------------------------------------------------------ (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) )) ------------------------------------------------------------------ (define (luyThua x y) ;x^y (dung de quy) (if (= y 0) 1 (* x (luyThua x (- y 1))) ) ) ;;tinh tong lap phuong cua 1 so (define (tongLapPhuong x s) (if (= x 0) s (tongLapPhuong (quotient x 10) (+ s (luyThua (remainder x 10) 3))))) ;;tra ve 1 neu tong so lap phuong = chinh no (define (isTongLapPhuong x) (if (and (= (tongLapPhuong x 0) x) (>= x 100)) 1 0)) ;;tra ve string gom cac so lap phuong (define (lietKeSoLapPhuong n str) (if (zero? n) (if (zero? (string-length str)) (string-append str "Khong co so nao ca.") (substring str 0 (- (string-length str) 2))) (if (= (isTongLapPhuong n) 1) (lietKeSoLapPhuong (- n 1) (string-append str (number->string n) ", ")) (lietKeSoLapPhuong (- n 1) str)))) ;goi ham (lietKeSoLapPhuong 909090 "") ----------------------------------------------------------------------------------------- (define (Bai5 n x) (cond ((= n 1) (sqrt x)) (else (sqrt (+ x (Bai5 (- n 1) x)))))) ---------------------------------------------------------------------- ;Cho một sâu ký tự có độ dài N, N được xem rất lớn. Hãy phân loại mỗi ký tự ;theo 4 kiểu như sau: Kiểu chữ thường, kiểu chữ hoa, kiểu chữ số và kiểu ;khác (ký tự không thuộc 3 kiểu trên)? (define (Dem S) (let((ct '()) (ch '()) (so '()) (kh '()) (i 0) (kt #\space)) (while (< i (string-length S)) (begin (set! kt (string-ref S i)) (cond ((and (char>=? kt #\a)(char<=? kt #\z)) (set! ct (append ct (list kt)))) ((and (char>=? kt #\A)(char<=? kt #\Z)) (set! ch (append ch (list kt)))) ((and (char>=? kt #\0)(char<=? kt #\9)) (set! so (append so (list kt)))) (else (if(not(char=? kt #\space)) (set! kh(append kh (list kt)))))) (set! i (+ i 1)))) (begin (display "Chu thuong: ") (display ct) (newline) (display "Chu hoa: ") (display ch) (newline) (display "Chu so: ") (display so) (newline) (display "Cac ky tu khac: ") (display kh) (newline) ))) ;chay : (Dem "hhghghgh") ------------------------------------------------------------- ;Viết hàm để : ;- Tính số lượng các ký tự trong một tập tin ;- Tính số lượng các ký tự không phải là ký tự phân cách (không phải là các dấu trống, nhảy cột, nhảy dòng) (define (Count-c) (define f (open-input-file "C:/hung.txt")) (define dem1 0) (define dem2 0) (define c 0) (set! c (read-char f)) (while(and (char? c) (not (eof-object? c))) (if(and (not (eqv? c #\space)) (not (eqv? c #\newline)) (not (eqv? c #\tab)) (not (eqv?(char->integer c) 13))) (set! dem1 (+ dem1 1)) ) (set! dem2 (+ dem2 1)) (set! c (read-char f)) ) (display "So luong cac ki tu: ") (display dem2) (newline) (display "So luong cac ki tu khong la ki tu trong : ") (display dem1) ) ------------------------------------------------------------------------ (define (canbac-2 n x) (if (> n 0) (sqrt( + x (canbac-2 (- n 1) x))) 0 ) ) (define (tinhcan) (display "Nhap vao n va x theo thu tu") (newline) (let( (n (read)) (x (read)) ) (canbac-2 n x) ) ) -------------------------------------------------------- (define dinhthuc (lambda (a11 a12 a13 a21 a22 a23 a31 a32 a33) (-(+(* a11 a22 a33)(* a21 a13 a32 )(* a12 a23 a31 )) (+(* a13 a22 a31)(* a11 a32 a23)( * a33 a12 a21)) ) ) ) (define (giai_he_phuong_trinh a1 b1 c1 d1 a2 b2 c2 d2 a3 b3 c3 d3) ( let ( (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)) (d (dinhthuc a1 b1 c1 a2 b2 c2 a3 b3 c3 )) ) ( 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 "nghiem cua he la : ") (display (/ dx d)) (display " ") (display (/ dy d)) (display " ") (display (/ dz d)) (display " ") ) ) ) ) --------------------------------------------------------------------------- (define (dinhthuc-3 a1 b1 c1 a2 b2 c2 a3 b3 c3) (- (+ (* a1 b2 c3)(* b1 c2 a3)(* c1 a2 b3)) (+ (* a3 b2 c1)(* b3 c2 a1)(* c3 a2 b1)) ) ) (define (he3-an) (display "Nhap vao cac he so theo thu tu sau")(newline) (display "a1 b1 c1 d1")(newline) (display "a2 b2 c2 d2")(newline) (display "a3 b3 c3 d3")(newline) (let* ( (a1 (read)) (b1 (read)) (c1 (read)) (d1 (read)) (a2 (read)) (b2 (read)) (c2 (read)) (d2 (read)) (a3 (read)) (b3 (read)) (c3 (read)) (d3 (read)) (d (dinhthuc-3 a1 b1 c1 a2 b2 c2 a3 b3 c3)) (dx (dinhthuc-3 d1 b1 c1 d2 b2 c2 d3 b3 c3)) (dy (dinhthuc-3 a1 d1 c1 a2 d2 c2 a3 d3 c3)) (dz (dinhthuc-3 a1 b1 d1 a2 b2 d2 a3 b3 d3)) ) (display "Nghiem x=")(write (/ dx d))(newline) (display "Nghiem y=")(write (/ dy d))(newline) (display "Nghiem z=")(write (/ dz d))(newline) ) ) ----------------------------------------------------------------- (define (Hanoi n A B C) (cond ((= n 1) (display "Move ")(write A)(display " to ")(write B)(newline)) (else (Hanoi (- n 1) A C B) (Hanoi 1 A B C) (Hanoi (- n 1) C B A)))) ;(Hanoi 3 'A 'B 'C) --------------------------------------------------------------
You need to login to post a comment.
