码迷,mamicode.com
首页 > 其他好文 > 详细

全民Scheme(1):数字游戏

时间:2015-06-26 00:31:15      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:scheme   lambda   game   

刚刚看到两句话,感觉不错,分享给大家:

1.用户的问题,就是我们的问题

2.做一些用户想要的东西

(define add1
  (lambda (x)
    (+ 1 x)))

(define sub1
  (lambda (x)
    (- x 1)))

(add1 67)
(sub1 5)
(sub1 0)

(zero? 22)

(define addup
  (lambda (tup)
    (cond
      ((null? tup) 0)
      (else (+ (car tup) (addup (cdr tup)))))))

(addup '(3 4 5))

(define *
  (lambda (x y)
    (cond
      ((eq? 1 x) y)
      (else (+ y (* (sub1 x) y))))))

(* 3 9)

(define tup+
  (lambda (tup1 tup2)
    (cond
      ((null? tup1) '())
      (else (cons (+ (car tup1) (car tup2)) (tup+ (cdr tup1) (cdr tup2)))))))

(tup+ '(2 3 4 5) '(5 4 3 2))

(define >
  (lambda (x y)
    (cond
    ((zero? x) #f)
    ((zero? y) #t)
    (else (> (sub1 x) (sub1 y))))))

(> 4 3)
(> 3 3)
(> 3 4)

(define <
  (lambda (x y)
    (cond
      ((zero? y) #f)
       ((zero? x) #t)
       (else (< (sub1 x) (sub1 y))))))
(< 3 3)
(< 4 3)
(< 3 4)

(define =
  (lambda (x y)
    (cond
      ((> x y) #f)
      ((< x y) #f)
      (else #t))))
(= 3 4)
(= 3 3)
(= 4 3)

(define expt
  (lambda (x y)
    (cond
    ((zero? y) 1)
    (else (* x (expt x (sub1 y)))))))

(expt 5 3)

(define /
  (lambda (x y)
    (cond
      ((< x y) 0)
      (else (add1 (/ (- x y) y))))))
(/ 10 2)

(define length
  (lambda (lat)
    (cond
      ((null? lat) 0)
      (else (add1 (length (cdr lat)))))))
(length '(good hello))


(define pick
  (lambda (n lat)
    (cond
      ((eq? n 1) (car lat))
      (else (pick (sub1 n) (cdr lat))))))

(pick 3 '(hotdogs with hot mustard))

(define no-nums
  (lambda (list)
    (cond
      ((null? list) '())
      ((number? (car list)) (no-nums (cdr list)))
      (else (cons (car list) (no-nums (cdr list)))))))

(no-nums '(5 pears 6 prunes 9 dates))

(define all-nums
  (lambda (lat)
    (cond
      ((null? lat) '())
      ((number? (car lat)) (cons (car lat) (all-nums (cdr lat))))
      (else (all-nums (cdr lat))))))
(all-nums '(99 abc 33 mm 9 gg fuck))

(define eqlat?
  (lambda (a1 a2)
    (cond
      ((and (null? a1) (null? a2)) #t)
      ((and (number? (car a1)) (number? (car a2)))
       (cond
         ((= (car a1) (car a2)) (eqlat? (cdr a1) (cdr a2)))
         (else #f)))
      ((eq? (car a1) (car a2)) (eqlat? (cdr a1) (cdr a2)))
      (else #f))))

(eqlat? '(a b c 33) '(a b c 33))

(define occur
  (lambda (a lat)
    (cond
      ((null? lat) 0)
      ((number? a)
       (cond
         ((and (number? (car lat)) (= (car lat) a)) (add1 (occur a (cdr lat))))
         (else (occur a (cdr lat)))))
      ((number? (car lat)) (occur a (cdr lat)))
      ((eq? a (car lat)) (add1 (occur a (cdr lat))))
      (else (occur a (cdr lat))))))
(occur 'a '(a b c d e a 3 4 a))
(occur 3 '(b cd e 3 4 3 ed 3))

(define one?
  (lambda (n)
    (= n 1)))


(one? 3)


全民Scheme(1):数字游戏

标签:scheme   lambda   game   

原文地址:http://blog.csdn.net/booirror/article/details/46642971

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!