简体   繁体   中英

How would I get this nested list to be formatted cleanly/correctly in scheme?

I want to have these lists returned in the expected format (() () () ()) but it ends up looking like ((((())))) what combinations of list and append would make this work? Or is there some extra built in function that will help?

Here's the code:

(define p-loop
  (lambda (row col triangle)
    (if (= row 0)
        triangle
        (if (= col 0)
            (p-loop (- row 1) (- row 1) (append (list triangle)))
            (p-loop row (- col 1) (append triangle (list (p-nums row col))))))))

( p-nums returns a single number)

(p-loop 4 4 '()) ideally would return

((1 3 3 1) (1 2 1) (1 1) (1))

but it ends up looking like

(((((1 3 3 1) 1 2 1) 1 1) 1))

For anyone who happened to have this very specific issue I figured it out with the help of someone in person after formatting it in the way Barmar's comment suggested:

(define p-loop
  (lambda (row col triangle)
    (if (= row 0)
        triangle
        (p-loop (- row 1) (- row 1) 
              (append triangle (list (p-row row row '())))))))

(define p-row
  (lambda (row col rowlist)
    (if (= col 0)
       rowlist
       (p-row row (- col 1) 
           (append rowlist (list (p-nums row col)))))))   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM