Is my function gaining anything by calling the recursion last at the expense of function length?
Also is it inefficient to nest function definitions like that? Does the second function get redefined each time the outside function is called or does it get stored once when compiled?
Code: Select all
;; Function by user "dsm" on stackoverflow
(define get-n-items
(lambda (lst num)
(if (> num 0)
(cons (car lst) (get-n-items (cdr lst) (- num 1)))
'()))) ;'
;; Return first n number of items
(define (get-n-items num lst)
(define (loop lst num results)
(if (< num 1)
(reverse results)
(loop (cdr lst) (- num 1) (cons (car lst) results))))
(loop lst num '()))