0
votes

I have a problem with a function showlength which I'm programming in Scheme:

(define showlength
  (lambda (m lst)
    (cond ((number? m) (list (cons m lst) 
                             (+ 1(length lst))))    
          ((pair? m) (let* ([x (cdr m)]
                            [y (car m)])
                       (showlength x lst)
                       (showlength y lst))))))

The code is meant to take either a number or a pair of numbers and a list(lst) and returns a list showing all values contained in the list and the length of the list. The program works for when I have just a number of example:

(showlength 2 '())

and returns

((2) 1)

but when I try it for a problem consisting of a pair of numbers of example

(showlength (cons 2 3) ' ())

in which the returned value is meant to be

((0 . 1) 3 2) 3)

it shows

((2) 1)

What is wrong with my code?

EDIT: For those who the code isnt that clear to. When a pair of numbers is used, the code is meant to add the cdr of the pair to the list. Then its meant to add the car of the pair to the list( where the list already contains the cdr of the pair from above) and then returns the list and its length

1
I would propose that you format and indent your code in a readable way. That would help a lot. - Rainer Joswig
You're discarding the result of (showlength x lst) (in your example, (showlength 3 '())) and returning (showlength y lst) (showlength 2 '())). It's very unclear what your function is supposed to do - neither of your examples seem to correspond to your description. In the first, "all values contained in the list" would be nothing, and the length of the list is 0. - molbdnilo
Rainer Joswig Is this readable enough for you? sorry still an amateur in this - kkk
molbdnilo I thought that the values( list and length) gotten from the (showlength x lst) will update the lst in which i would then use the updated values to compile the (showlength y lst) functon and return the value - kkk

1 Answers

0
votes

The issue is that in your let statement, you have two independent calls to show length, and you implementation is just returning the value of the first.

In scheme a function only ever returns one value (generally, some implementation provide special mechanisms to allow a value to expect and accept multiple return values. )

My question is where in the world do you expect a zero to come from? The smallest value in the m is 2, and the smallest value that length can return of it's own volition is 1, when lst length is zero. Whatever it is you're doing you need to step back and check your assumptions, something isn't right.

(define showlength
  (lambda (m lst)
    (cond ((number? m) (list (cons m lst) 
                             (+ 1(length lst))))    
          ((pair? m) (let* ((x (cdr m))
                            (y (car m)))
                       (cons (showlength x lst)
                             (showlength y lst)))))))

Tying the returns together with cons will get you a single value without losing anything, but it's not the return value you want.