0

How would I loop this list in scheme?

(define test-document '(
                    ((h e l l o))
                    ((t h i s)(i s)(t e s t))
                    ))

What I tried it only showed the first column.

2
  • What do you mean by "loop this list"? Commented Apr 20, 2014 at 2:55
  • Being able to access each word in the list in order. Commented Apr 20, 2014 at 3:07

3 Answers 3

3

car and cdr family of functions are your friends to navigate lists. Here are some examples.

(define test-document '(
                    ((h e l l o))
                    ((t h i s)(i s)(t e s t))
                    ))

(car test-document)  ;; `((h e l l o))
(caar test-document)  ;; `(h e l l o)
(cadr test-document) ;; `((t h i s) (i s) (t e s t))
(car (cadr test-document) ;; `(t h i s)
(cadr (cadr test-document) ;; `(i s)
(caddr (cadr test-document) ;; `(test )

Define a function that will walk the list and call a function for each item that is not a list.

(define (walk-list lst fun)
   (if (not (list? lst))
      (fun lst)
      (if (not (null? lst))
         (begin
            (walk-list (car lst) fun)
            (walk-list (cdr lst) fun)))))

Call it to print each item.

(walk-list test-document print)
Sign up to request clarification or add additional context in comments.

3 Comments

But what if you don't know the size of the document.
@PhilipRego I added some more content to my answer to illustrate how you can walk a list and do something with the items in it.
How do I get it to print the words without parethesis. it gets the words as a list but i cant do anything with it cause it has the dumb pareteses around them (define (walk-list lst fun) (if (not (equal? (car lst) '())) (fun lst) (if (not (null? lst)) (begin (walk-list (car lst) fun) (walk-list (cdr lst) fun))))) (walk-list test-document display)
1

What you have is a list of lists of lists:

(define test-document '(((h e l l o)) ((t h i s) (i s) (t e s t))))

To loop over its elements you must create a loop of a loop of a loop. To do so we can use map and curry as follows:

(map (curry map (curry map
        (compose string->symbol string-upcase symbol->string)))
    test-document)

This produces the following output:

(((H E L L O)) ((T H I S) (I S) (T E S T)))

If your Scheme interpreter doesn't have a built-in curry function then you can define one as follows:

(define (curry func . args)
    (lambda x (apply func (append args x))))

Hope this helped.

2 Comments

It says curry is undefined
Which scheme interpreter are you using? The curry function is available in mzscheme. Anyway I updated my answer with an implementation of curry.
1

Were you thinking of something like this?

(define (walk-list lst)
  (define (sub-walk lst)
    (if (null? lst)
        '()
        (let ((x (car lst)))
          (if (list? x)
              (cons (sub-walk x) (sub-walk (cdr lst)))
              (apply string-append (map symbol->string lst))))))
  (flatten (sub-walk lst)))

then

(walk-list test-document)
=> '("hello" "this" "is" "test")

which you can process using the usual suspects (map, filter, ...).

If your Scheme has no flatten procedure, you can use this one:

(define (flatten lst)
  (reverse
   (let loop ((lst lst) (res null))
     (if (null? lst)
         res
         (let ((c (car lst)))
           (loop (cdr lst) (if (pair? c) (loop c res) (cons c res))))))))

2 Comments

I don't have the function flatten
That's why I put one in my answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.