fork download
  1. #lang racket
  2.  
  3. ;(define (f lst)
  4. ; (let loop ((a (car lst)) (b (cadr lst)) (lst (cddr lst)) (acc '()))
  5. ; (if (null? lst)
  6. ; (cons (+ a b) acc)
  7. ; (loop b (car lst) (cdr lst) (cons (+ a b) acc)))))
  8. ;
  9. ;(define (pascal n)
  10. ; (let loop ((n n) (acc '((1) (1 1))))
  11. ; (cond ((zero? n) `(,(car acc)))
  12. ; ((= n 1) acc)
  13. ; (else (loop (- n 1) `(,@acc (1 ,@(f (last acc)) 1)))))))
  14.  
  15. ;;; 別解
  16.  
  17. (define (pascal n)
  18. (foldl (lambda (y x)
  19. (cond ((zero? y) x)
  20. ((= y 1) `(,@x (,y ,y)))
  21. (else `(,@x (1 ,@(let ((lst (last x)))
  22. (map + (take lst (- (length lst) 1))
  23. (cdr lst))) 1)))))
  24. '((1)) (range 0 (+ n 1))))
Success #stdin #stdout 0.53s 87364KB
stdin
Standard input is empty
stdout
Standard output is empty