Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1782287
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: LINUX

2017-07-19 14:36:15

scheme 和Guile其实都是Lisp的dialect. 因此看起来还是很像的, 拿出来little scheme 看了些实现,确实不错。
讲了很多+ * 的实现,当然书中的实现还仅限于两个argument,
而guile现在的+ / * 都支持多个argument.

点击(此处)折叠或打开

  1. #! /usr/bin/guile -s
  2. !#

  3. (define atom?
  4.     (lambda (x)
  5.         (and (not (pair? x)) (not (null? x)))))


  6. ;; a list of atom
  7. (define lat?
  8.     (lambda (l)
  9.         (cond
  10.             ((null? l) #t)
  11.             ((atom? (car l)) (lat? (cdr l)))
  12.             (else #f))))

  13. ;; check an atom whether a member of a list
  14. (define member?
  15.     (lambda (a lat)
  16.         (cond
  17.             ((null? lat) #f)
  18.             (else (or (eq? (car lat) a)
  19.                     (member? a (cdr lat)))))))

  20. ;; remove an atom of 1st occurrence from the list
  21. (define rember
  22.     (lambda (a lat)
  23.         (cond
  24.             ((null? lat) '())
  25.             ((eq? (car lat) a) (cdr lat))
  26.             (else (cons (car lat) (rember a (cdr lat)))))))

  27. ;; get all the car of the list
  28. (define firsts
  29.     (lambda (lat)
  30.         (cond
  31.             ((null? lat) '())
  32.             (else (cons (car (car lat)) (firsts(cdr lat)))))))


  33. ;; add new to the right of old
  34. (define insertR
  35.     (lambda (old new lat)
  36.         (cond
  37.             ((null? lat) '())
  38.             ((eq? (car lat) old) (cons old (cons new (cdr lat))))
  39.             (else (cons (car lat) (insertR new old (cdr lat)))))))

  40. ;; add new to the left of old
  41. (define insertL
  42.     (lambda (old new lat)
  43.         (cond
  44.             ((null? lat) '())
  45.             ((eq? (car lat) old) (cons new lat))
  46.             (else (cons (car lat) (insertL new old (cdr lat)))))))


  47. ;; substitue old with new
  48. (define subst
  49.     (lambda (old new lat)
  50.         (cond
  51.             ((null? lat) '())
  52.             ((eq? (car lat) old) (cons new (cdr lat)))
  53.             (else (cons (car lat) (subst new old (cdr lat)))))))


  54. ;; subsitute o1 o2 with new
  55. (define subst2
  56.     (lambda (new o1 o2 lat)
  57.         (cond
  58.             ((null? lat) '())
  59.             ( (or (eq? (car lat) o1) (eq? (car lat) o2))
  60.                 (cons new (cdr lat)))
  61.             (else (cons (car lat) (subst2 new o1 o2 (cdr lat)))))))

  62. ;; multiple remove a atom from a list
  63. (define multirember
  64.     (lambda (a lat)
  65.         (cond
  66.             ((null? lat) '())
  67.             ( (eq? (car lat) a)
  68.                 (multirember a (cdr lat)))
  69.             (else (cons (car lat) (multirember a (cdr lat)))))))

  70. ;; multiple insert new to the right of old in a list
  71. (define multiinsertR
  72.     (lambda (new old lat)
  73.         (cond
  74.             ((null? lat) '())
  75.             (else
  76.                 (cond
  77.                     ((eq? (car lat) old)
  78.                         (cons old(cons new (multiinsertR new old (cdr lat)))))
  79.                     (else (cons (car lat) (multiinsertR new old (cdr lat)))))))))

  80. ;compute n+1
  81. (define add1
  82.     (lambda (n)
  83.         (+ n 1)))

  84. ;compute n-1
  85. (define sub1
  86.     (lambda (n)
  87.         (- n 1)))

  88. ; define +
  89. (define my+
  90.     (lambda (n m)
  91.         (cond
  92.             ((zero? m) n)
  93.             (else (add1 (my+ n (sub1 m)))))))

  94. ; define -
  95. (define my-
  96.     (lambda (n m)
  97.         (cond
  98.             ((zero? m) n)
  99.             (else (sub1 (my- n (sub1 m)))))))

  100. ; build a number by total all the numbers in a tuple
  101. (define addtup
  102.     (lambda (lat)
  103.         (cond
  104.             ((null? lat) 0)
  105.             (else (my+ (car lat) (addtup (cdr lat)))))))

  106. ; add n for m times, n*m
  107. (define my_x
  108.     (lambda (n m)
  109.         (cond
  110.             ((zero? m) 0)
  111.             (else (my+ n (my_x n (sub1 m)))))))

  112.                 

  113. ; add two tuple
  114. (define tup+
  115.     (lambda (tup1 tup2)
  116.         (cond
  117.             ((and (null? tup1) (null? tup2)) '())
  118.             (else (cons (my+ (car tup1 ) (car tup2))
  119.                 (tup+ (cdr tup1) (cdr tup2)))))))


  120. ;; define "my>"
  121. (define my>
  122.     (lambda (n m)
  123.         (cond
  124.             ((zero? n) #f)
  125.             ((zero? m) #t)
  126.             (else (my> (sub1 n) (sub1 m))))))

  127. ;; define "my<"
  128. (define my<
  129.     (lambda (n m)
  130.         (cond
  131.             ((zero? m) #f)
  132.             ((zero? n) #t)
  133.             (else (my< (sub1 n) (sub1 m))))))


  134. ; define "="
  135. (define my=
  136.     (lambda (n m)
  137.         (cond
  138.             ((zero? n) (zero? m))
  139.             ((zero? n) #f)
  140.             (else (my= (sub1 n) (sub1 m))))))



  141. ;; define "my2=", 2nd way with "my>" and "my<"
  142. (define my2=
  143.     (lambda (n m)
  144.         (cond
  145.             ((my< n m) #f)
  146.             ((my> n m) #f)
  147.             (else #t))))

  148. ;; define my_expt
  149. (define my_expt
  150.     (lambda (n m)
  151.         (cond
  152.             ((zero? m) 1)
  153.             (else (my_x n (my_expt n (sub1 m)))))))

点击(此处)折叠或打开

  1. ;; define my_division "/"
  2. (define my_division
  3.     (lambda (n m)
  4.         (cond
  5.             ((my< n m) 0)
  6.             (else ( add1 (my_division (my- n m) m))))))

  7. ;; define lat_length; length
  8. (define lat_length
  9.     (lambda (lat)
  10.         (cond
  11.             ((null? lat) 0)
  12.             (else (add1 (lat_length (cdr lat)))))))

  13. ;; define function pick, pick the nst atom in the list
  14. (define pick
  15.     (lambda (n lat)
  16.         (cond
  17.             ((or (zero? n) (null? lat)) '())
  18.             ((zero? (sub1 n)) (car lat))
  19.             (else (pick (sub1 n) (cdr lat))))))


  20. ;; define function rempick
  21. (define rempick
  22.     (lambda (n lat)
  23.         (cond
  24.             ((null? lat) '())
  25.             ((zero? (sub1 n)) (cdr lat))
  26.             (else (cons (car lat) (rempick (sub1 n) (cdr lat)))))))


  27. ;;remove all the numbers from a lat
  28. (define no-nums
  29.     (lambda (lat)
  30.         (cond
  31.             ((null? lat) '())
  32.             (else
  33.                 (cond
  34.                     ((number? (car lat)) (no-nums (cdr lat)))
  35.                     (else (cons (car lat) (no-nums (cdr lat)))))))))


  36. ;; only keep numbers
  37. (define all-nums
  38.     (lambda (lat)
  39.         (cond
  40.             ((null? lat) '())
  41.             ((number? (car lat)) (cons (car lat) (all-nums (cdr lat))))
  42.             (else (all-nums (cdr lat))))))

  43. ;; eqan? compare two object
  44. (define eqans?
  45.     (lambda (o1 o2)
  46.         (cond
  47.             ((and (number? o1) (number? o2)) (my= o1 o2))
  48.             ((or (number? o1) (number? o2)) #f)
  49.             (else (eq? o1 o2)))))

  50. ;; count an atom appeared n times in a list
  51. (define occur
  52.     (lambda (a lat)
  53.         (cond
  54.             ((null? lat) 0)
  55.             ((eq? (car lat) a) (add1 (occur a (cdr lat))))
  56.             (else (occur a (cdr lat))))))


  57. ;; one? (one? n) is #t if n==1 and #f for other number
  58. (define one?
  59.     (lambda (n)
  60.         (cond
  61.             ((zero? n) #f)
  62.             (else (zero? (sub1 n))))))


  63. (define one2?
  64.     (lambda (n)
  65.         (my= n 1)))


  66. ;; rewirte rempick remove the nth atom from a lat
  67. (define rempick2
  68.     (lambda (n lat)
  69.         (cond
  70.             ((one? n) (cdr lat))
  71.             (else (cons (car lat) (rempick2 (sub1 n) (cdr lat)))))))


但是其实看看现在的guile.

点击(此处)折叠或打开

  1. scheme@(guile-user) [3]> (map + '(1 2 3) '(10 20 30) '(100 200 300))
  2. $7 = (111 222 333)





阅读(1295) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~