Search code examples
schemeracketwescheme

Scheme code cond error in Wescheme


Although the following code works perfectly well in DrRacket environment, it generates the following error in WeScheme:

Inside a cond branch, I expect to see a question and an answer, but I see more than two things here.
at: line 15, column 4, in <definitions>

How do I fix this? The actual code is available at http://www.wescheme.org/view?publicId=gutsy-buddy-woken-smoke-wrest

(define (insert l n e)
  (if (= 0 n)
      (cons e l)
      (cons (car l) 
          (insert (cdr l) (- n 1) e))))

(define (seq start end)
  (if (= start end)
      (list end)
      (cons start (seq (+ start 1) end))))

(define (permute l) 
  (cond 
    [(null? l) '(())]
    [else (define (silly1 p)
            (define (silly2 n) (insert p n (car l)))
            (map silly2 (seq 0 (length p))))
            (apply append (map silly1 (permute (cdr l))))]))

Solution

  • Use local for internal definitions in the teaching languages.

    If you post your question both here and at the mailing list, remember to write you do so. If someone answers here, there is no reason why persons on the mailing list should take time to answer there.

    (define (insert l n e)
      (if (= 0 n)
          (cons e l)
          (cons (car l) 
                (insert (cdr l) (- n 1) e))))
    
    (define (seq start end)
      (if (= start end)
          (list end)
          (cons start (seq (+ start 1) end))))
    
    (define (permute2 l) 
      (cond 
        [(null? l) '(())]
        [else 
         (local [(define (silly1 p)
                   (local [(define (silly2 n) (insert p n (car l)))]
                     (map silly2 (seq 0 (length p)))))]
           (apply append (map silly1 (permute2 (cdr l)))))]))
    
    (permute2 '(3 2 1))