0
(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?

Machavity
  • 30,841
  • 27
  • 92
  • 100
suckypizza
  • 39
  • 1
  • 6

1 Answers1

1

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)
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 3
    Note that having that `read` inside a function `getFirstFew` is highly misleading and should be considered bad code style IMO. A function `take N lst` or `getFirst N lst` would be a better choice. – Daniel Jour Nov 14 '15 at 08:37