0

Hi,

I have a method that returns a list of images. I will say,

(get_images str)

and

(list-ref item num) simply displays num'th element in the list.

From the list, I need to print all of the images recursively.
And I noticed DrScheme is not smart enough to perform more than 1 tasks in one method.

What I tried is like:

(define (print-all(list refnum))
(cond [(< ref (length list)) print (list-ref list refnum) and (print-all(kust refnum+1))]
[else (do nothing)]
))

I can't complete this function! please help!!

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

2
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Taywin
0

Hmm.. How about change the way you do inside the function... Let say you get the max length of the list before hand. The condition is still there...

# the starting refnum is equal to (length of the list -1)
(define (print-all(list refnum))
  (cond [(>= 0 (length list)) (print-all(list refnum-1))]
  [else (do nothing)])
  (print (list-ref list refnum))
)

PS: You have typo for variable "ref" for "refnum" and "kust" for "list" in your example?

Edited by Taywin: n/a

0

Hmm.. How about change the way you do inside the function... Let say you get the max length of the list before hand. The condition is still there...

# the starting refnum is equal to (length of the list -1)
(define (print-all(list refnum))
  (cond [(>= 0 (length list)) (print-all(list refnum-1))]
  [else (do nothing)])
  (print (list-ref list refnum))
)

PS: You have typo for variable "ref" for "refnum" and "kust" for "list" in your example?

Thx for the advice! I know how to make recursion in other programming languages, but DrScheme is so trick.

here is the code after your advice:

(define (print-all list refnum)
  (cond [(<=0 (refnum))(print-all(list (+ refnum -1)))]
        [else empty])
  (get-image (list-ref item refnum))
  )

Problem: I can't make another expression out of (cond ...) So I keep getting an error msg like "define: expected only one expression for the function body, but found 1 extra part" on "(get-image (list-ref item refnum)))" What should I do? Please help!!

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

0

Hmm.. Just looked up on DrScheme tutorial a bit. Let's try again...

(define (print-all list)
  (cond
    [(null? list) return]
    [else (get-image(car(list)) (print-all(cdr(list))))]
  )
)

You may need to revise get-image() to take only an object from the list. It should take an object of the list and displays whatever you want.

In the code, the function check if the incoming list is null. If it is null, return and do nothing. Otherwise, it takes the first item in the list and pass it to the get-image(), and then passes the rest of the list to the function again recursively.

Edited by Taywin: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.