如何在scheme / lisp中分解这样的列表?

时间:2012-12-10 01:10:24

标签: functional-programming lisp scheme

输入1:

(decompose '(* 1 2 3 4))

输出1:

'(* 1 (* 2 (* 3 4)))

输入2

(decompose '(+ 1 2 3 (* 5 6 7)))

输出2

'(+ 1 (+ 2 (+ 3 (* 5 (* 6 7)))))    

有没有人有这方面的想法?

3 个答案:

答案 0 :(得分:2)

与评估它的方式相同,但您只需输出将要使用的表达式,而不是输出结果。

这是我在Racket上测试的实现:

(define (decompose expr)
  (define (expand x)
    (if (list? x)
        (decompose x)
        x))
  (define oper (car expr))
  (let next ((args (map expand (cdr expr))))
    (if (<= (length args) 2)
        (cons oper args)
        (list oper (car args) (next (cdr args))))))

答案 1 :(得分:1)

我看到你发布了自己的解决方案,所以我想可以展示我的完整答案。这是另一种可能的实现,作为一对相互递归的过程。我喜欢这个解决方案不需要使用lengthlist?(这可能需要对列表进行不必要的遍历)这一事实,并且只使用基本函数(没有foldr,{{1需要},reverse或任何其他更高阶的程序。)

map

它适用于您的示例,然后是一些:

(define (decompose lst)
  (if (or (null? lst)                     ; if the list is empty
          (null? (cdr lst))               ; or has only one element
          (null? (cddr lst)))             ; or has only two elements
      lst                                 ; then just return the list
      (process (car lst)                  ; else process car of list (operator)
               (cdr lst))))               ; together with cdr of list (operands)

(define (process op lst)
  (cond ((null? (cdr lst))                ; if there's only one element left
         (if (not (pair? (car lst)))      ; if the element is not a list
             (car lst)                    ; then return that element
             (decompose (car lst))))      ; else decompose that element
        ((not (pair? (car lst)))          ; if current element is not a list
         (list op                         ; build a list with operator,
               (car lst)                  ; current element,
               (process op (cdr lst))))   ; process rest of list
        (else                             ; else current element is a list
         (list op                         ; build a list with operator,
               (decompose (car lst))      ; decompose current element,
               (process op (cdr lst)))))) ; process rest of list

答案 2 :(得分:0)

Chris Jester-Young的解决方案修改:

(define (decompose x)
  (if (pair? x)
      (let ((operator (car x))
            (expanded-x (map decompose x)))
        (let decompose-helper ((operands (cdr expanded-x)))
          (if (<= (length operands) 2)
              (cons operator operands)
              (list operator (car operands) (decompose-helper (cdr operands))))))
      x))