Posted By

numbbbbb on 03/03/15


Tagged


Versions (?)

scheme


 / Published in: Scheme
 

URL: scheme-yanghui

scheme写杨辉三角

  1. (define (triangle n)
  2. (define (calNumber c now)
  3. (cond
  4. ((= now 1) 1)
  5. ((= now c) 1)
  6. (else (+ (calNumber (- c 1) (- now 1)) (calNumber (- c 1) now)))))
  7. (define (printLine c now)
  8. (cond
  9. ((= now 1) (begin (display "1") (display " ")) (printLine c (+ now 1)))
  10. ((= now c) (display "1") (newline))
  11. (else (begin (display (+ (calNumber (- c 1) (- now 1)) (calNumber (- c 1) now))) (display " ")) (printLine c (+ now 1)))))
  12. (newline)
  13. (define (triangle-iter c)
  14. (if (not (> c n))
  15. (cond
  16. ((= c 1) (display "1") (newline) (triangle-iter 2))
  17. ((= c 2) (display "1 1") (newline) (triangle-iter 3))
  18. (else (printLine c 1) (triangle-iter (+ c 1))))))
  19. (triangle-iter 1)
  20. )
  21.  
  22. (triangle 5)

Report this snippet  

You need to login to post a comment.