scheme 和Guile其实都是Lisp的dialect. 因此看起来还是很像的, 拿出来little scheme 看了些实现,确实不错。
讲了很多+ * 的实现,当然书中的实现还仅限于两个argument,
而guile现在的+ / * 都支持多个argument.
-
;; define my_division "/"
-
(define my_division
-
(lambda (n m)
-
(cond
-
((my< n m) 0)
-
(else ( add1 (my_division (my- n m) m))))))
-
-
;; define lat_length; length
-
(define lat_length
-
(lambda (lat)
-
(cond
-
((null? lat) 0)
-
(else (add1 (lat_length (cdr lat)))))))
-
-
;; define function pick, pick the nst atom in the list
-
(define pick
-
(lambda (n lat)
-
(cond
-
((or (zero? n) (null? lat)) '())
-
((zero? (sub1 n)) (car lat))
-
(else (pick (sub1 n) (cdr lat))))))
-
-
-
;; define function rempick
-
(define rempick
-
(lambda (n lat)
-
(cond
-
((null? lat) '())
-
((zero? (sub1 n)) (cdr lat))
-
(else (cons (car lat) (rempick (sub1 n) (cdr lat)))))))
-
-
-
;;remove all the numbers from a lat
-
(define no-nums
-
(lambda (lat)
-
(cond
-
((null? lat) '())
-
(else
-
(cond
-
((number? (car lat)) (no-nums (cdr lat)))
-
(else (cons (car lat) (no-nums (cdr lat)))))))))
-
-
-
;; only keep numbers
-
(define all-nums
-
(lambda (lat)
-
(cond
-
((null? lat) '())
-
((number? (car lat)) (cons (car lat) (all-nums (cdr lat))))
-
(else (all-nums (cdr lat))))))
-
-
;; eqan? compare two object
-
(define eqans?
-
(lambda (o1 o2)
-
(cond
-
((and (number? o1) (number? o2)) (my= o1 o2))
-
((or (number? o1) (number? o2)) #f)
-
(else (eq? o1 o2)))))
-
-
;; count an atom appeared n times in a list
-
(define occur
-
(lambda (a lat)
-
(cond
-
((null? lat) 0)
-
((eq? (car lat) a) (add1 (occur a (cdr lat))))
-
(else (occur a (cdr lat))))))
-
-
-
;; one? (one? n) is #t if n==1 and #f for other number
-
(define one?
-
(lambda (n)
-
(cond
-
((zero? n) #f)
-
(else (zero? (sub1 n))))))
-
-
-
(define one2?
-
(lambda (n)
-
(my= n 1)))
-
-
-
;; rewirte rempick remove the nth atom from a lat
-
(define rempick2
-
(lambda (n lat)
-
(cond
-
((one? n) (cdr lat))
-
(else (cons (car lat) (rempick2 (sub1 n) (cdr lat)))))))
但是其实看看现在的guile.
-
scheme@(guile-user) [3]> (map + '(1 2 3) '(10 20 30) '(100 200 300))
-
$7 = (111 222 333)
阅读(1295) | 评论(0) | 转发(0) |