Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

How do you do this?

Name: Anonymous 2013-01-28 0:59

/prog/ How do you do this?

Write the function mult-table which consumes two natural numbers nr and nc, and produces a list of nr lists, each of which contains nc natural numbers. The cth entry of the rth list (where we start numbering at 0) in the produced list should be r*c.
For example,
     (mult-table 2 3) => (list (list 0 0 0) (list 0 1 2))
and
     (mult-table 3 4) =>
        (list (list 0 0 0 0)
              (list 0 1 2 3)
(list 0 2 4 6)).

Hint: use build-list twice. The first time, try to make a list containing nr elements. Then, for each of these elements, make a list of nc elements. You may use other abstract list functions as well.

Name: Anonymous 2013-01-28 2:56

Here's my solution.

(define (mult-table nr nc)
  (define (iter-table r)
    (define (iter-row c)
      (if (< c nc)
        (cons (* r c)
              (iter-row (+ c 1)))
        '()))
    (if (< r nr)
      (cons (iter-row 0)
            (iter-table (+ r 1)))
      '()))
  (iter-table 0))


Of course, this doesn't use the build-list procedure you're supposed to use, because you provide it, so you probably won't get full credit.

What school are you attending that teaches Scheme?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List