listschemelispcdr

How would I store a value into read for Scheme? Or is that not even possible?


(define (getFirstFew lst)
  (cond
  ((= (read) 0) '()) ;returns nothing
  (else(cons (car lst)(getFirstFew (cdr lst)(- (read) 1))))))

That is my code above. So i'm trying to write a procedure that will get the first x elements (user gets to choose what x gets to be) from a list. For example, input 4 with (getFirstFew '(1 6 2 4 5)) will result in '(1 6 2 4).

My current problem with this is that with using read two times, it gets called twice and then breaks the program. Is there a way for me to store whatever the user inputs into a variable and then use that variable throughout the program? Or is there another solution to this problem?


Solution

  • Notice that you have to perform the read only once, and store the value for future reference. Normally we'd use a let for this, but given that we also have to iterate over the list and decrement x on each iteration, a named let will be more appropriate. Try this:

    (define (getFirstFew lst)
      (let loop ((lst lst) (x (read)))
        (if (= x 0)
            '()
            (cons (car lst)
                  (loop (cdr lst) (- x 1))))))
    

    It works as expected:

    (getFirstFew '(1 6 2 4 5))
    > 4
    => '(1 6 2 4)