Try:
(define (at n xs)
(cond ((null? xs) xs)
((= n 1) (car xs))
(else (at (- n 1) (cdr xs)))))
Use it as follows:
(at 3 '(1 2 0 8 5 6)) => 0
For zero-based indexing change the (= n 1)
check on the 3rd line to (= n 0)
.
Edit: So you want to partially apply the at
function? All you need is curry
and flip
. They are defined as follows:
(define (curry func . args)
(lambda x (apply func (append args x))))
(define (flip func)
(lambda (a b) (func b a)))
Using curry
and flip
you can now partially apply at
as follows:
(define position (curry (flip at) '(1 2 0 8 5 6)))
You can now use position
as follows:
(position 3) => 0
(position 4) => 8
Hope that helped.