Scheme code cond error in Wescheme

青春壹個敷衍的年華 提交于 2019-12-06 00:01:07

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))

Another option would be to restructure the code, extracting the inner definitions (which seem to be a problem for WeScheme) and passing around the missing parameters, like this:

(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 (apply append (map (lambda (p) (silly1 p l))
                             (permute (cdr l))))]))

(define (silly1 p l)
  (map (lambda (n) (silly2 n p l))
       (seq 0 (length p))))

(define (silly2 n p l)
  (insert p n (car l)))

The above will work in pretty much any Scheme implementation I can think of, it's very basic, standard Scheme code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!