Question

   (define (walk-list lst fun)                       ;;walk-list(list, fun)
    (if (not(null? lst))                             ;;IF the list isn't NULL
      (begin
        (if (list? lst)                              ;;&& the list is actually a list ,       THEN{
          (begin  
            (if (equal? (car lst) '())              ;;IF the first element in the list is empty
              (fun lst)                              ;;THEN call the function on the list (funct is supose to get each word)
            (if (not (null? lst))                    ;;ELSE IF the first item isn't a list 
              (begin                                 ;;{         
                (walk-list (car lst) fun)            ;;walk-list((car lst),fun) 
                (walk-list (cdr lst) fun)))))))))    ;;walk-list((cdr lst),fun)
(walk-list test-document display)                    ;;walk through the list with the given document 

The will look something like this:

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

I'm trying to get each individual word into the document have a function applied to it. Where is says (fun list). But the function is never called.

Was it helpful?

Solution

First off. begin is if you need to do more than one expression. The first expression then needs to have side effects or else it's just a waste of processing power.

Ie.

(begin 
  (display "hello") ; display is a side effect
  (something-else))

When you don't have more than one expression begin isn't needed. if has 3 parts. They are:

(if predicate-expression    ; turnas into something true or #f (the only false value)
    consequent-expression   ; when predicate-expression evalautes to anything but #f
    alternative-expression) ; when predicate-expression evaluates to #f this is done

You should ident your code properly. Here is the code idented with DrRacket IDE, with reduncant begin removed and missing alternative-expressions added so you see where they return:

(define (walk-list lst fun)                       ;;walk-list(list, fun)
  (if (not (null? lst))                           ;;IF the list isn't NULL
      (if (list? lst)                             ;; && the list is actually a list ,       THEN{
          (if (equal? (car lst) '())              ;; IF the first element in the list is empty
              (fun lst)                           ;; THEN call the function on the list (funct is supose to get each word)
              (if (not (null? lst))               ;; ELSE IF the first item isn't a list 
                  (begin                          ;; Here begin is needed        
                    (walk-list (car lst) fun)     ;; walk-list((car lst),fun) 
                    (walk-list (cdr lst) fun))    ;; walk-list((cdr lst),fun)
                  'undfined-return-1))            ;; stop recursion, return undefined value
          'undefined-return-2)                    ;; stop recursion, return undefined value
      'undefined-return-3))                       ;; stop recursion, return undefined value

So when does (fun lst) get called? Never! There is no () in any car in (((h e l l o))((t h i s) (i s) (t e s t))) and (equal? (car lst) '()) which is (null? (car lst)) will always be #f. Since we know (not (null? lst)) is #t so it will walk car and cdr where either 'undefined-return-2 or 'undefined-return-3 will be evaluated and the procedure stops when everything is visited and nothing processed.

You haven't shown what (walk-list test-document display) should have displayed but I make a wild guess that you want it for every element except pairs and null, thus I would have written this like this:

(accumulate-tree test-document display (lambda (a d) 'return) '())

accumulate-tree you'll find in this SICP handout. It demonstrates many uses for it as well. For completeness I'll supply it here:

(define (accumulate-tree tree term combiner null-value)
  (cond ((null? tree) null-value)
        ((not (pair? tree)) (term tree))
        (else (combiner 
               (accumulate-tree (car tree) 
                                term
                                combiner
                                null-value)
               (accumulate-tree (cdr tree)
                                term
                                combiner
                                null-value)))))

Judging from you code you are an Algol programmer learning your first Lisp. I advice you to look at the SICP videoes and book.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top