[plt-scheme] Why does this code pause at the end

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Fri Mar 5 14:20:49 EST 2010

See Robby's answer for coverage 

Consider the following code, too: 

#lang scheme

(define vref vector-ref)

(define (pascal-row row)
  (local ((define r (vector->list row)))
    (list->vector (map + (cons 0 r) (append r (list 0))))))

(define (pascal n k)
  (cond
    [(> k n) (pascal k n)]
    [else
     (local ((define r ((apply compose (make-list (- n 1) pascal-row)) #(1))))
       (+ (vref r (sub1 k)) (vref r k)))]))

(= 286191035203773536729215177959428379150929723879069881376273032504577219690326014732293768640
   (pascal 345 235))

If it weren't for vector->list and list->vector, this would run in ISL+



On Mar 5, 2010, at 12:40 PM, wooks wrote:

> Running this in Advanced Student. It's a fast version of Pascals
> Triangle implemented by computing each row before it returns a result.
> 
> The program returns it's result but a (relatively) long pause ensues
> before the scheme prompt appears. Wondering why.
> 
> (define (pascal-row row)
>  (cons 1
>        (local [(define (sum-neighbours row)
>                  (if (or (empty? row)
>                          (empty? (rest row)))
>                      row
>                      (cons (+ (first row) (second row))
>                            (sum-neighbours (rest row)))))]
>          (sum-neighbours row))))
> 
> (define (pascal n k)
>  (cond
>    [(> k n) (pascal k n)]
>    [else (local [(define (pascal m previous-row)
>                    (cond
>                      [(= m n) (+ (list-ref previous-row (sub1 k))
>                                  (list-ref previous-row k))]
>                      [else (pascal (add1 m) (pascal-row previous-
> row))]))]
>            (pascal 0 empty))]))
> (pascal 345 235)
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.