Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1782153
  • 博文数量: 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-10 15:55:38

看到第9章了,开始真是看的头大了,所以little scheme就到这里吧。

点击(此处)折叠或打开

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

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


  6. (define rember-f
  7.     (lambda (test? a l)
  8.         (cond
  9.             ((null? l) '())
  10.             ((test? (car l) a) (cdr l))
  11.             (else (cons (car l) (rember-f test? a (cdr l)))))))


  12. (define rember-f2
  13.     (lambda (test?)
  14.         (lambda (a l)
  15.             (cond
  16.                 ((null? l) '())
  17.                 ((test? (car l) a) (cdr l))
  18.                 (else (cons (car l) ((rember-f2 test?) a (cdr l))))))))

  19. ;rewrite insertL to insertL-f
  20. (define insertL-f
  21.     (lambda (test?)
  22.         (lambda (new old l)
  23.             (cond
  24.                 ((null? l) '())
  25.                 ((test? (car l) old) (cons new (cons old (cdr l))))
  26.                 (else (cons (car l) ((insertL-f test?) new old (cdr l))))))))


  27. ;rewrite insertR to insertR-f
  28. (define insertR-f
  29.     (lambda (test?)
  30.         (lambda (new old l)
  31.             (cond
  32.                 ((null? l) '())
  33.                 ((test? (car l) old) (cons old (cons new (cdr l))))
  34.                 (else (cons (car l) ((insertR-f test?) new old (cdr l))))))))

  35. ;write a function insert-g insert either at the left or at the rigth?
  36. (define seqL
  37.     (lambda (new old l)
  38.         (cons new (cons old l))))

  39. (define seqR
  40.     (lambda (new old l)
  41.         (cons old (cons new l))))

  42. (define insert-g
  43.     (lambda (seq)
  44.         (lambda (new old l)
  45.             (cond
  46.                 ((null? l) '())
  47.                 ((eq? (car l) old) (seq new old (cdr l)))
  48.                 (else (cons (car l) ((insert-g seq) new old (cdr l))))))))


  49. ; redefine insertL
  50. (define insertL (insert-g seqL))

  51. (define insertL2
  52.     (insert-g
  53.         (lambda (new old l)
  54.             (cons new (cons old l)))))

  55. ; rdefine insertR
  56. (define insertR (insert-g seqR))

  57. (define insertR2
  58.     (insert-g
  59.         (lambda (new old l)
  60.             (cons old (cons new l)))))

  61. (define seqS
  62.     (lambda (new old l)
  63.         (cons new l)))

  64. ;susbtitue
  65. (define subst-f (insert-g seqS))

  66. ;rember
  67. (define seqrem (lambda (new old l) l))

  68. (define rember-f3
  69.     (lambda (a l)
  70.         ((insert-g seqrem) #f a l)))

  71. ; get 3 from '(+ 3 4)
  72. (define fst-sub-exp
  73.     (lambda (aexp)
  74.         (car (cdr aexp))))

  75. ; get 4 from '(+ 3 2)
  76. (define sec-sub-exp
  77.     (lambda (aexp)
  78.         (car (cdr (cdr aexp)))))

  79. ;get operator
  80. (define operator
  81.     (lambda (aexp)
  82.         (car aexp)))

  83. (define atom-to-function
  84.     (lambda (x)
  85.         (cond
  86.             ((eq? x '+ ) +)
  87.             ((eq? x '* ) *)
  88.             ((eq? x '- ) -)
  89.             (else / ))))

  90. (define value
  91.     (lambda (nexp)
  92.         (cond
  93.             ((atom? nexp) nexp)
  94.             (else ((atom-to-function (operator nexp)) (value (fst-sub-exp nexp))
  95.                 (value (sec-sub-exp nexp)))))))

  96. (define multirember-f
  97.     (lambda (test?)
  98.         (lambda (a lat)
  99.             (cond
  100.                 ((null? lat) '())
  101.                 ((test? a (car lat)) ((multirember-f test?) a (cdr lat)))
  102.                 (else (cons (car lat) ((multirember-f test?) a (cdr lat))))))))

  103. (define eq?-c
  104.     (lambda (a)
  105.         (lambda (x)
  106.             (eq? x a))))

  107. (define eq?-tuna
  108.     (eq?-c 'tuna ))

  109. (define multiremberT
  110.     (lambda (test? lat)
  111.         (cond
  112.             ((null? lat) '())
  113.             ((test? (car lat)) (multiremberT test? (cdr lat)))
  114.             (else (cons (car lat) (multiremberT test? (cdr lat)))))))


  115. ;
  116. (define a-friend
  117.     (lambda (x y)
  118.         (null? y)))


  119. (define multirember-co
  120.     (lambda (a lat col)
  121.         (cond
  122.             ((null? lat) (col '() '()))
  123.             ((eq? (car lat) a) (multirember-co a (cdr lat) (lambda (newlat seen)
  124.                 (col newlat (cons (car lat) seen)))))
  125.             (else
  126.                 (multirember-co a (cdr lat) (lambda (newlat seen)
  127.                     (col (cons (car lat) newlat) seen)))))))


点击(此处)折叠或打开

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

  3. ;; define function pick, pick the nst atom in the list
  4. (define pick
  5.     (lambda (n lat)
  6.         (cond
  7.             ((or (zero? n) (null? lat)) '())
  8.             ((zero? (- n 1)) (car lat))
  9.             (else (pick (- n 1) (cdr lat))))))

  10. (define looking
  11.     (lambda (a lat)
  12.         (keep-looking a (pick 1 lat) lat)))

  13. (define keep-looking
  14.     (lambda (a sorn lat)
  15.         (cond
  16.             ((number? sorn) (keep-looking a (pick sorn lat) lat))
  17.             (else (eq? sorn a )))))

  18. ;======================================
  19. (define first
  20.     (lambda (p)
  21.         (cond
  22.             (else (car p)))))

  23. (define second
  24.     (lambda (p)
  25.         (cond
  26.             (else (car (cdr p))))))

  27. (define build
  28.     (lambda (a1 a2)
  29.         (cond
  30.             (else (cons a1 (cons a2 '()))))))

  31. (define shift
  32.     (lambda (pair)
  33.         (build (first (first pair))
  34.         (build (second (first pair)) (second pair)))))


  35. ;===============
  36. ;length
  37. (define length*
  38.     (lambda (para)
  39.         (cond
  40.             ((atom? para ) 1)
  41.             (else (+ (length* (first para)) (length* (second para)))))))


开始翻了翻SICP, 听说前4章对于初学者是有用的,但是感觉对于数学的要求很高。
对我这个文科生,自学计算机的真够难啊。
计算一个数x的平方根的函数sqrt().
假设先猜测x的平方根y ,初始值是y。 则可以通过 (y+x/y)/2 求得一个更好的y值。

点击(此处)折叠或打开

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

  3. (define (sqrt-iter guess x)
  4.     (if (good-enough? guess x)
  5.         guess
  6.         (sqrt-iter (improve guess x) x)))

  7. (define (improve guess x)
  8.     (average guess (/ x guess)))

  9. (define (average x y)
  10.     (/ (+ x y) 2))

  11. (define (good-enough? guess x)
  12.     (< (abs (- (square guess) x)) 0.001))

  13. (define (square x)
  14.     (* x x))

  15. (define (mysqrt x)
  16.     (sqrt-iter 1.0 x))


  17. (define (new-if predicate then-clause else-clause)
  18.     (cond
  19.         (predicate then-clause)
  20.         (else else-clause)))

  21. ;never stop
  22. (define (sqrt-iter2 guess x)
  23.     (new-if (good-enough? guess x)
  24.         guess
  25.         (sqrt-iter2 (improve guess x) x )))

  26. (define (mysqrt2 x)
  27.     (sqrt-iter2 1.0 x))


  28. ;another approach to define good-enough3? , this function works very well
  29. ; for smalle numbers
  30. (define (good-enough3? guess prev-guess)
  31.     (< (abs (- guess prev-guess)) (* 0.001 guess)))

  32. (define (sqrt-iter3 guess prev-guess x)
  33.     (if (good-enough3? guess prev-guess)
  34.         guess
  35.         (sqrt-iter3 (improve guess x) guess x)))


  36. (define (mysqrt3 n)
  37.     (sqrt-iter3 1 0 n))



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