Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>(define (term-expander n val partial) (if (zero? n) partial (term-expander (- n 1) val (cons val partial)))) (define (append-expand a b) (if (pair? b) (append a (term-expander (car b) (cadr b) '())) (append a (list b)))) (define (expand l) (foldl append-expand l '() )) &gt; (expand '(a (3 b) (3 a) b (2 c) (3 a))) (a b b b a a a b c c a a a) </code></pre> <p>So... <code>term-expander</code> takes <code>(5 a)</code> -> <code>'(a a a a a)'</code>. <code>append-expand</code> will append the newest term (expanded iff it is a pair) to our answer in progress. just need to fold this over the list.</p> <p>I'd hope you have a <code>foldl</code> already, but heres mine just in case.</p> <pre><code>(define (foldl op seq init) (define (iter acc rest) (if (null? rest) acc (iter (op acc (car rest)) (cdr rest)))) (iter init seq)) </code></pre> <hr> <h2>EDIT</h2> <p>Realized afterwards this is can be done a LITTLE bit more naturally with <code>foldr</code> rather than <code>foldl</code> (will need <code>append-expand</code> written differently). Why don't you see if you can find it? Same idea mostly, but it will operate on the list from back to front and lends itself more naturally to <code>cons</code> structures</p> <hr> <p>Here's me doing it without fold, but it works the same way and i dont really care for it</p> <pre><code>(define (term-expander p partial) (if (zero? (car p)) partial (term-expander (cons (- (car p) 1) (cdr p)) (cons (cadr p) partial)))) (define (expand l) (define (expand-rec l partial) (if (null? l) partial (let ((t (if (pair? (car l)) (term-expander (car l) '()) (list (car l))))) (expand-rec (cdr l) (append partial t))))) (expand-rec l '())) &gt; (expand '(a (3 b) (3 a) b (2 c) (3 a))) (a b bb a a a b c c a a a) </code></pre> <p>If you would prefer, the <code>(if (pair?...</code> section could go in to <code>term-expander</code> instead, and then you'd just call it across the board. I slightly prefer this way but its all the same</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload