SchemeでCodeEvalのPascals Triangle

↓こういうのを見つけたので Scheme でやってみよう、と思ったら CodeEval って Scheme ないんでやんの。

 cf. Python2でCodeEvalのPascals Triangle – brainstorm
 cf. Pascals Triangle – CodeEval

しょうがないから勝手にやる。

(define pascals-triangle
  (lambda (depth)
    (let loop ((n depth) (pas '((1))))
      (if (= n 1)
          (reverse pas)
          (let ((next (map + (cons 0 (car pas)) (append (car pas) '(0)))))
            (loop (- n 1) (cons next pas)))))))

(define flatten
  (lambda (list-of-list)
    (fold-left append '() list-of-list)))

(define print-pascals-triangle
  (lambda (depth)
    (print (string-join (map number->string (flatten (pascals-triangle (string->number depth)))) " "))))

(define main
  (lambda (args)
    (with-input-from-file (cadr args)
      (lambda ()
        (port-for-each print-pascals-triangle read-line)))))
^o^ > type input-pascal.txt
6

^o^ > gosh pascal.scm input-pascal.txt
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1

たぶんあってる。