I want to select first elements from a list.
(define (take lst n)
(let loop ((result '()) (i n) (lst lst))
(if (or (null? lst) (<= i 0))
(reverse result)
(loop (cons (car lst) result) (- i 1) (cdr lst)))))
(define (take/alternative lst n)
(let loop ((result '()) (i n) (lst lst))
(cond ((<= i 0) (reverse result))
((null? lst) (error "take: not enough elements"))
(else
(loop (cons (car lst) result) (- i 1) (cdr lst))))))
Credit: Jakub T. Jankiewicz
The same function is part of the SRFI-1.
(take '(1 2 3 4 5) 3)
;; ==> (1 2 3)
(take '(1 2) 10)
;; ==> (1 2)
(take/alternative '(1 2) 10)
;; ==> take: not enough elements