Taco cola
Taco cola

Reputation: 17

Racket print length of "n" elements in a list

I need some help to understand tail recursive

    #lang racket

   (define (lista x)
   (printf(length(cons x (lista (read)))))
   )

   (lista (read))

I can't figure it out how to end the loop of the linked list and then get the length and print this number.

So for example if I have an input of

  2
  1
  3
  5
  7
  5
  10

It must print 7,but the compiler always message timeout, so I don't know if I'm reading correctly the input or there is another way?

Upvotes: 1

Views: 1560

Answers (2)

user2609980
user2609980

Reputation: 10484

You have no stop condition in the recursion, that's why it goes on without printing the length. You can for example break by looking for a certain input, e.g., "length" to print the length.

#lang racket

(define (get-length-of-input)
  (display "Enter a number or get the length (length for length)")
  (let input-loop ((result '()))
    (define input (read-line))
    (cond [(number? (string->number input)) 
           (input-loop (cons input result))]
          [(string=? input "length") 
           (displayln (length result))]
          [else (displayln "unknown input") 
                (input-loop result)])))

; Start the procedure
(get-length-of-input)

Here to make it tail recursive an iterative loop is used with a named-let . A named-let initializes the loop with an initial value (the empty list '()) and starts it of automatically.

Then a case analysis takes place (is the input a number, the string "length" or something else?). If it is a number (checked by trying to convert the input string to a number), this number is added to the local variable result and the loop is continued. If the input is "length" the length is printed and the program stops. Otherwise "unknown input" is displayed and the loop continues.

Upvotes: 0

KeV
KeV

Reputation: 2891

What you are doing does not make sense (i will explain below why). If you are using length, you could immediately return the length of the list. The reason why you are having an infinite loop is because you have no stop condition (i.e. when the list is null? make an end to the recursion).

I suppose this is an assignment which requires you to do it manually, hence not using length.

(define (lngth lst)
  (if (null? lst) ; Did we checked the whole list already?
      0           ; This will put an end to the recursion !
      (+ 1        ; Tail recursive call, we add 1 because we call ourselve recusrively with the cdr of the list (hence with a list that is one shorter)
         (lngth (cdr lst)))))

Now you can call this procedure, (lngth '(1 2 3 4 5)) will return 5.


(define (lista x)
  (printf
   (length
    (cons x 
          (lista (read))))))

Your attempt has a few problems. The biggest problem is that it is lacking a stop condition, which is the reason for the endless loop (which is prompting the user for input again and again). Now when you call lista you will always encounter a recursive call to itself again and again, because of (define (lista x) ... (cons x (lista ...))).

As I said above, the rest of the code doesn't really make sense. You are using cons instead of cdr to traverse through the list, and you are using length instead of adding one, (+ 1 (recursive call)), for the current element of the list.

Upvotes: 1

Related Questions