Scheme: Is it possible to convert a list of S-expressions into a list of atoms?

*爱你&永不变心* 提交于 2021-01-28 11:26:14

问题


I am trying to convert a list of S-expressions to a plain list of atoms similar to a problem in the book The Little Schemer.

My code is (as typed in Dr.Racket):

> (define lat '((coffee) cup ((tea) cup) (and (hick)) cup))
> (define f
    (lambda (lat)
      (cond
        ((null? lat) (quote ()))
        ((atom? (car lat)) (cons (car lat) (f (cdr lat))))
        (else (cons (f (car lat)) (f (cdr lat)))))))
> (f lat)
'((coffee) cup ((tea) cup) (and (hick)) cup)

The above code is returning back the list the same as input list. I tried my best, but getting different answers like:

(coffee)
(cup . cup)
( () (()) (()) )

for various modifications in the program.

I would like to know, can we achieve the answer:

'(coffee cup tea cup and hick cup)

given

'((coffee) cup ((tea) cup) (and (hick)) cup)

by using cond cons car and cdr only.


回答1:


Tweak it:

(define f
    (lambda (lat)
      (cond
        ((null? lat) (quote ()))
        ;; add this clause
        ((null? (car lat)) (f (cdr lat)))
        ((atom? (car lat)) (cons (car lat) (f (cdr lat))))
        (else ;; (cons (f (car lat)) (f (cdr lat)))
             (f (cons (car (car lat))       ; rotate the tree to the right
                      (cons (cdr (car lat)) (cdr lat))))))))  ; and repeat

Uses John McCarthy's "gopher" trick, rotating the tree to the right until the leftmost atom is exposed in the top left position, then splitting it off and continuing.




回答2:


You just need to replace the last cons with append, to flatten the sublists:

(define f
  (lambda (lat)
    (cond
      ((null? lat) (quote ()))
      ((atom? (car lat)) (cons (car lat) (f (cdr lat))))
      (else (append (f (car lat)) (f (cdr lat)))))))

append already is a built-in primitive, but it's simple to implement in terms of the primitive procedures you mentioned, if you want to (not recommended, of course: just use the built-in!).

(define (append l1 l2)
  (cond ((null? l1) l2)
        ((null? l2) l1)
        (else (cons (car l1) (append (cdr l1) l2)))))

Now it works as expected:

(f '((coffee) cup ((tea) cup) (and (hick)) cup))
=> '(coffee cup tea cup and hick cup)

FYI, the procedure you were trying to implement is called flatten and is pretty common, and some Scheme flavors (Racket, for example) already include it. In real life, what you'd do is:

(flatten '((coffee) cup ((tea) cup) (and (hick)) cup))
=> '(coffee cup tea cup and hick cup)



回答3:


This seems to be close to the standard flatten function that everyone wants to write at some point. I always like to see how these can be written without copping out by using append using the nice trick (I think) of having an agenda. The following does this: note this is probably specific to Racket.

(define (tree->atoms tree)
  (define atom?
    ;; Something is an atom if it is not a cons
    (compose not cons?))
  (define (rev thing)
    ;; this is just reverse
    (let rev-loop ([rt thing] [rrt '()])
      (if (null? rt)
          rrt
          (rev-loop (rest rt) (cons (first rt) rrt)))))
  (let tree->atoms-loop ([it tree]
                         [agenda '()]
                         [results '()])
    (cond [(null? it)
           ;; no more left
           (if (null? agenda)
               ;; no more agenda: we're done, so reverse
               ;; the results and return that
               (rev results)
               ;; more agenda, so carry on
               (tree->atoms-loop (first agenda)
                                 (rest agenda)
                                 results))]
          [(atom? it)
           ;; we've found an atom which is not ()
           (if (null? agenda)
               ;; we're done
               (rev (cons it results))
               ;; there is more
               (tree->atoms-loop (first agenda)
                                 (rest agenda)
                                 (cons it results)))]
          [else
           ;; cons: look at the car, and stuff the cdr onto the agenda
           (tree->atoms-loop (car it)
                             (cons (cdr it) agenda)
                             results)])))


来源:https://stackoverflow.com/questions/62946821/scheme-is-it-possible-to-convert-a-list-of-s-expressions-into-a-list-of-atoms

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