Skip to content

Instantly share code, notes, and snippets.

View yao2030's full-sized avatar

yao2030 yao2030

  • Shanghai, China
View GitHub Profile
;;
(define (accumulate op init seq)
(if (null? seq)
init
(op (car seq)
(accumulate op init (cdr seq)))))
;;
(define (accumulate-n op init seqs)
(if (null? (car seqs))
'()
(define (last-pair list1)
(if (= (length list1) 1)
(car list1)
(last-pair (cdr list1))))
(define (but-last list1)
(if (= (length list1) 1)
'()
(cons (car list1) (but-last (cdr list1)))))
(define (reverse x)
(if (null? x)
(define (fold-left op initial sequence)
(define (iter result rest)
(if (null? rest)
result
(iter (op result (car rest))
(cdr rest))))
(iter initial sequence))
(define fold-right accumulate)
(define (reverse seq)
(define (up-split painter n)
(if (= n 0)
painter
(let ((smaller (up-split painter (- n 1))))
(below painter (beside smaller smaller)))))
(define (split op1 op2)
(lambda (painter n)
(if (= n 0)
painter
(let ((smaller ((split op1 op2) painter (- n 1))))
(op1 painter (op2 smaller smaller))))))
(define (make-vect x y)
(cons x y))
(define (xcor-vect v)
(car v))
(define (ycor-vect v)
(cdr v))
(define (add-vect v1 v2)
(make-vect (+ (xcor-vect v1) (xcor-vect v2))
(+ (ycor-vect v1) (ycor-vect v2))))
(define (sub-vect v1 v2)
(define (equal? list1 list2)
(cond ((null? list1) (null? list2))
((null? list2) #f)
((eq? list1 list2) #t)
((eq? (car list1) (car list2)) (equal? (cdr list1) (cdr list2)))
(else #f)))
@yao2030
yao2030 / book.txt
Last active December 9, 2015 19:38
1. Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp
2. Code: The Hidden Language of Computer Hardware and Software
3. An introduction to algorithms
4. Artificial Intelligence: a modern approach
5. ON LISP
6. ANSI COMMON LISP
7. LISP IN SMALL PIECES
8. THE LITTLE LISPER
9. THE SEASONED SCHEMER
(define (=number? exp num)
(and (number? exp) (= exp num)))
(define (variable? x) (symbol? x))
(define (same-variable? a b)
(and (variable? a) (variable? b) (eq? a b)))
(define (make-sum a1 a2)
(cond ((=number? a1 0) a2)
;;; SICP 2.58 part.b
;;;
;; sum
(define (make-sum a1 a2)
(cond ((and (number? a1) (number? a2)) (+ a1 a2))
((=number? a1 0) a2)
((=number? a2 0) a1)
(else (list a1 '+ a2))))
(define (sum? x)
(and (pair? x) (eq? (cadr x) '+)))