Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1782081
  • 博文数量: 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-08-07 15:55:48

下面这些给我留下了很深的印象
always ask null? as the first question in expressing any function.
when building a list, describe the first typical element, then cons it onto the natural recursion.
always change at least one argument when use recurring.it must be changed to be closer to termination.
The changing argument must be tested in the termination condition. when using cdr, test termination
with null?.
when recurring on a list of atomes, lat , use (cdr lat).
when recurring on a number, n, use (sub1 n),

when recurring on a list of Scheme-expressions, l, ask three questions.
(null? l)
(atom? (car l))
and else.
use (car l) and (cdr l) if neither (null? l) nor (atom? (car l)) are true.


点击(此处)折叠或打开

  1. #! /usr/bin/guile -s
  2. !#
  3. (define atom?
  4.     (lambda (x)
  5.         (and (not (pair? x)) (not (null? x)))))


  6. ;; remove all the atom a from a list
  7. (define rember*
  8.     (lambda (a l)
  9.         (cond
  10.             ((null? l) '())
  11.             ((atom? (car l))
  12.             (cond
  13.                 ((eq? (car l) a) (rember* a (cdr l)))
  14.                 (else (cons (car l) (rember* a (cdr l))))))
  15.             (else (cons (rember* a (car l)) (rember* a (cdr l)))))))



  16. ;; insertR*
  17. (define insertR*
  18.     (lambda (new old l)
  19.         (cond
  20.             ((null? l) '())
  21.             ((atom? (car l))
  22.             (cond
  23.                 ((eq? (car l) old) (cons old (cons new (insertR* new old (cdr l)))))
  24.                 (else (cons (car l) (insertR* new old (cdr l))))))
  25.             (else (cons (insertR* new old (car l)) (insertR* new old (cdr l)))))))

  26. ;; occur*
  27. (define occur*
  28.     (lambda (a l)
  29.         (cond
  30.             ((null? l) 0)
  31.             ((atom? (car l))
  32.             (cond
  33.                 ((eq? (car l) a) (+ 1 (occur* a (cdr l))))
  34.                 (else (occur* a (cdr l)))))
  35.             (else
  36.                 (+ (occur* a (car l)) (occur* a (cdr l)))))))

  37. ;; subst*

  38. (define subst*
  39.     (lambda (new old l)
  40.         (cond
  41.             ((null? l) '())
  42.             ((atom? (car l))
  43.             (cond
  44.                 ((eq? (car l) old) (cons new (subst* new old (cdr l))))
  45.                 (else (cons (car l) (subst* new old (cdr l))))))
  46.             (else
  47.                 (cons (subst* new old (car l)) (subst* new old (cdr l)))))))

  48. ;; insertL*
  49. (define insertL*
  50.     (lambda (new old l)
  51.         (cond
  52.             ((null? l) '())
  53.             ((atom? (car l))
  54.             (cond
  55.                 ((eq? (car l) old) (cons new l))
  56.                 (else (cons (car l) (insertL* new old (cdr l))))))
  57.             (else
  58.                 (cons (insertL* new old (car l)) (insertL* new old (cdr l)))))))


  59. ;; member*
  60. (define member*
  61.     (lambda (a l)
  62.         (cond
  63.             ((null? l) #f)
  64.             ((atom? (car l)) (or (eq? (car l) a) (member* a (cdr l))))
  65.             (else
  66.                 (or (member* a (car l)) (member* a (cdr l)))))))



  67. ;; leftmost
  68. (define leftmost
  69.     (lambda (l)
  70.         (cond
  71.             ((null? l) #f)
  72.             ((atom? (car l)) (car l))
  73.             (else
  74.                 (leftmost (car l))))))



  75. ;; compare two atom
  76. (define eqan?
  77.     (lambda (a b)
  78.         (cond
  79.             ((and (number? a) (number? b)) (= a b))
  80.             ((or (number? a) (number? b)) #f)
  81.             (else (eq? a b)))))


  82. ;; eqlist3?
  83. (define eqlist3?
  84.     (lambda (l1 l2)
  85.         (cond
  86.             ((and (null? l1) (null? l2)) #t)
  87.             ((and (null? l1) (atom? (car l2))) #f)
  88.             ((null? l1) #f)
  89.             ((and (atom? (car l1)) (null? l2)) #f)
  90.             ((and (atom? (car l1)) (atom? (car l2)))
  91.              (and (eqan? (car l1) (car l2)) (eqlist3? (cdr l1) (cdr l2))))
  92.             ((atom? (car l1)) #f)
  93.             ((null? l2) #f)
  94.             ((atom? (car l2)) #f)
  95.             (else
  96.                 (and (eqlist? (car l1) (car l2)) (eqlist3? (cdr l1)(cdr l2)))))))



  97. (define eqlist2?
  98.     (lambda (l1 l2)
  99.         (cond
  100.         ((and (null? l1) (null? l2)) #t)
  101.         ((or (null? l1) (null? l2)) #f)
  102.         ((and (atom? (car l1)) (atom? (car l2)))
  103.          (and (eqan? (car l1) (car l2)) (eqlist2? (cdr l1) (cdr l2))))
  104.         ((or (atom? (car l1)) (atom? (car l2))) #f)
  105.         (else (and (eqlist2? (car l1) (car l2)) (eqlist2? (cdr l1) (cdr l2)))))))

  106. ;; eq_s_exp
  107. (define eq_s_exp?
  108.     (lambda (s1 s2)
  109.         (cond
  110.             ((and (atom? s1) (atom? s2)) (eqan? s1 s2))
  111.             ((or (atom? s1) (atom? s2)) #f)
  112.             (else (eqlist? s1 s2)))))


  113. (define eqlist?
  114.     (lambda (l1 l2)
  115.         (cond
  116.             ((and (null? l1) (null? l2)) #t)
  117.             (( or (null? l1) (null? l2)) #f)
  118.             (else
  119.                 (and (eq_s_exp? (car l1) (car l2)) (eqlist? (cdr l1) (cdr l2)))))))

  120. ; remove s-expression from a list
  121. (define rember
  122.     (lambda (s l)
  123.         (cond
  124.             ((null? l) '())
  125.             ((equal? (car l) s) (rember s (cdr l)))
  126.             (else (cons (car l) (rember s (cdr l)))))))


  127. ; numbered?
  128. (define numbered?
  129.     (lambda (aexp)
  130.         (cond
  131.             ((atom? aexp) (number? aexp))
  132.             (else (and (numbered? (car aexp)) (numbered? (car (cdr (cdr aexp)))))))))

  133. ; compute '(3 + 4)
  134. (define value
  135.     (lambda (nexp)
  136.         (cond
  137.             ((atom? nexp) nexp)
  138.             ((eq? (car (cdr nexp)) '+ ) (+ (value (car nexp)) (value (car (cdr (cdr nexp))))))
  139.             ((eq? (car (cdr nexp)) '- ) (- (value (car nexp)) (value (car (cdr (cdr nexp))))))
  140.             ((eq? (car (cdr nexp)) '* ) (* (value (car nexp)) (value (car (cdr (cdr nexp))))))
  141.             (else
  142.             (/ (value (car nexp)) (value (car (cdr (cdr nexp)))))))))

  143.  
  144. ; get 3 from '(+ 3 4)
  145. (define fst-sub-exp
  146.     (lambda (aexp)
  147.         (car (cdr aexp))))

  148. ; get 4 from '(+ 3 2)
  149. (define sec-sub-exp
  150.     (lambda (aexp)
  151.         (car (cdr (cdr aexp)))))

  152. ;get operator
  153. (define operator
  154.     (lambda (aexp)
  155.         (car aexp)))

  156. ; compute '(+ 3 4)
  157. (define value1
  158.     (lambda (nexp)
  159.         (cond
  160.             ((atom? nexp) nexp)
  161.             ((eq? (operator nexp) '+) (+ (value1 (fst-sub-exp nexp)) (value1 (sec-sub-exp nexp))))
  162.             ((eq? (operator nexp) '-) (- (value1 (fst-sub-exp nexp)) (value1 (sec-sub-exp nexp))))
  163.             ((eq? (operator nexp) '*) (* (value1 (fst-sub-exp nexp)) (value1 (sec-sub-exp nexp))))
  164.             (else
  165.             (/ (value1 (fst-sub-exp nexp)) (value1 (sec-sub-exp nexp)))))))


  166. ; member
  167. (define member?
  168.     (lambda (s lat)
  169.         (cond
  170.             ((null? lat) #f)
  171.             (else (or (eq_s_exp? (car lat) s) (member? s (cdr lat)))))))

  172. ; set? whether a lat is a set
  173. (define set?
  174.     (lambda (lat)
  175.         (cond
  176.             ((null? lat) #t)
  177.             ((member? (car lat) (cdr lat)) #f)
  178.             (else (set? (cdr lat))))))

  179. ;makeset
  180. (define makeset
  181.     (lambda (lat)
  182.         (cond
  183.             ((null? lat) '())
  184.             ((member? (car lat) (cdr lat)) (makeset (cdr lat)))
  185.             (else (cons (car lat) (makeset (cdr lat)))))))

  186. ;; multiple remove a atom from a list
  187. (define multirember
  188.     (lambda (a lat)
  189.         (cond
  190.             ((null? lat) '())
  191.             ( (eq? (car lat) a)
  192.                 (multirember a (cdr lat)))
  193.             (else (cons (car lat) (multirember a (cdr lat)))))))


  194. ;;rewrite makeset with multirember
  195. (define makeset2
  196.     (lambda (lat)
  197.         (cond
  198.             ((null? lat) '())
  199.             (else (cons (car lat) (makeset2 (multirember (car lat) (cdr lat))))))))


  200. ;check whether each atom in set 1 is also in set2;subset
  201. (define subset?
  202.     (lambda (set1 set2)
  203.         (cond
  204.             ((null? set1) #t)
  205.             (else (cond
  206.                     ((member? (car set1 ) set2) (subset? (cdr set1) set2))
  207.                     (else #f))))))

  208. (define subset2?
  209.     (lambda (set1 set2)
  210.         (cond
  211.             ((null? set1) #t)
  212.             ((member? (car set1) set2) (subset? (cdr set1) set2))
  213.             (else #f))))

  214. ; two set are equal
  215. (define eqset?
  216.     (lambda (set1 set2)
  217.         (and (subset? set1 set2) (subset? set2 set1))))

  218. (define eqset2?
  219.     (lambda (set1 set2)
  220.         (cond
  221.             ((subset? set1 set2) (subset? set2 set1))
  222.             (else #f))))

  223. ;intersect?
  224. (define intersect?
  225.     (lambda (set1 set2)
  226.         (cond
  227.             ((null? set1) #f)
  228.             ((member? (car set1) set2) #t)
  229.             (else (intersect? (cdr set1) set2)))))


  230. (define intersect
  231.     (lambda (set1 set2)
  232.         (cond
  233.             ((null? set1) '())
  234.             ((member? (car set1) set2) (cons (car set1) (intersect (cdr set1) set2)))
  235.             (else (intersect (cdr set1) set2)))))

  236. ;union
  237. (define union
  238.     (lambda (set1 set2)
  239.         (cond
  240.             ((null? set1) set2)
  241.             ((member? (car set1) set2) (union (cdr set1) set2))
  242.             (else (cons (car set1) (union (cdr set1) set2))))))


  243. ;set difference
  244. (define difference
  245.     (lambda (set1 set2)
  246.         (cond
  247.             ((null? set1) '())
  248.             ((member? (car set1) set2) (difference (cdr set1) set2))
  249.             (else (cons (car set1) (difference (cdr set1) set2))))))

  250. ;intersectall l-set: list of sets
  251. (define intersectall
  252.     (lambda (l-set)
  253.         (cond
  254.             ((null? (cdr l-set)) (car l-set))
  255.             (else (intersect (car l-set) (intersectall (cdr l-set)))))))


点击(此处)折叠或打开

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

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



  6. (define a-pair?
  7.     (lambda (x)
  8.         (cond
  9.             ((null? x) #f)
  10.             ((atom? x) #f)
  11.             ((null? (cdr x)) #f)
  12.             ((null? (cdr (cdr x))) #t)
  13.             (else #f))))


  14. (define first
  15.     (lambda (p)
  16.         (cond
  17.             (else (car p)))))

  18. (define second
  19.     (lambda (p)
  20.         (cond
  21.             (else (car (cdr p))))))

  22. (define revpair
  23.     (lambda (p)
  24.         (build (second p) (first p))))


  25. (define build
  26.     (lambda (s1 s2)
  27.         (cond
  28.             (else (cons s1 (cons s2 '()))))))

  29. (define third
  30.     (lambda (l)
  31.         (car (cdr (cdr l)))))

  32. ;; check an atom whether a member of a list
  33. (define member?
  34.     (lambda (a lat)
  35.         (cond
  36.             ((null? lat) #f)
  37.             (else (or (eq? (car lat) a)
  38.                     (member? a (cdr lat)))))))

  39. ; set? whether a lat is a set
  40. (define set?
  41.     (lambda (lat)
  42.         (cond
  43.             ((null? lat) #t)
  44.             ((member? (car lat) (cdr lat)) #f)
  45.             (else (set? (cdr lat))))))

  46. ;; get all the car of the list
  47. (define firsts
  48.     (lambda (lat)
  49.         (cond
  50.             ((null? lat) '())
  51.             (else (cons (car (car lat)) (firsts(cdr lat)))))))

  52. ;;get all the second of the list
  53. (define seconds
  54.     (lambda (lat)
  55.         (cond
  56.             ((null? lat) '())
  57.             (else
  58.                 (cons (car (cdr (car lat))) (seconds (cdr lat)))))))


  59. ; rel: relation, list of pairs and set of pairs
  60. (define fun?
  61.     (lambda (rel)
  62.         (set? (firsts rel))))


  63. (define revrel
  64.     (lambda (rel)
  65.         (cond
  66.             ((null? rel) '())
  67.             (else
  68.                 (cons (build (second (car rel)) (first (car rel)))
  69.                     (revrel (cdr rel)))))))
  70.                     
  71. (define revrel1
  72.     (lambda (rel)
  73.         (cond
  74.             ((null? rel) '())
  75.             (else
  76.                 (cons (revpair (car rel)) (revrel1 (cdr rel)))))))

  77. (define fullfun?
  78.     (lambda (fun)
  79.         (set? (seconds fun))))


  80. (define one-to-one?
  81.     (lambda (fun)
  82.         (fun? (revrel fun))))

#
eq? tests just for the same object (essentially a pointer comparison). This is fast, and can be used when searching for a particular object, or when working with symbols or keywords (which are always unique objects).
eqv? extends eq? to look at the value of numbers and characters. It can for instance be used somewhat like = (see Comparison) but without an error if one operand isn’t a number.
equal? goes further, it looks (recursively) into the contents of lists, vectors, etc. This is good for instance on lists that have been read or calculated in various places and are the same, just not made up of the same pairs. Such lists look the same (when printed), and equal? will consider them the same.
Generally eqv? below should be used when comparing numbers or characters.
阅读(1342) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~