Scheme事始め14:高階手続

  • 手続きを引数として取る手続き
    • apply
> (define iota
    (lambda (min max)
      (if (> min max)
          '()
          (cons min(iota (+ min 1) max)))))
> (iota 0 9)
(0 1 2 3 4 5 6 7 8 9)
> (define num0-9 (iota 0 9))
> (apply + num0-9)
45
<||
--map
>|lisp|
> (map - num0-9)
(0 -1 -2 -3 -4 -5 -6 -7 -8 -9)
  • 組み合わせ列挙
> (define (double n)
    (apply append
           (map (lambda (i)
                  (map (lambda (j) (list i j))
                       (iota 1 (- i 1))))
                (iota 1 n))))
> (double 4)
((2 1) (3 1) (3 2) (4 1) (4 2) (4 3))