Skip to content

Instantly share code, notes, and snippets.

@lispm
Created December 31, 2015 20:45
Show Gist options
  • Save lispm/f9c5ca800979f3a4adcd to your computer and use it in GitHub Desktop.
Save lispm/f9c5ca800979f3a4adcd to your computer and use it in GitHub Desktop.
TAKE in Scheme, CL and Miranda
; page 13, Ralf Hinze, Einführung in die funktionale Programmierung mit Miranda
#|
take' 0 x = []
take' (n+1) [] = []
take' (n+1) (a:x) = a : take' n x
; The ugly Scheme version in the book
(define take
(lambda (n x)
(cond
((zero? n) '())
(else (cond
((null? x) '())
(else (cons (car x)
(take (-1+ n) (cdr x))))
))
)))
; easily better written as
(define (take n x)
(cond
((zero? n) '())
((null? x) '())
(else (cons (car x)
(take (-1+ n) (cdr x))))))
|#
(defun take (n x)
(if (or (zerop n) (null x))
'()
(cons (first x)
(take (1- n) (rest x)))))
(defun take (n x)
(loop repeat n
while x
collect (pop x)))
(defun take (n x)
(loop for e in x
repeat n
collect e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment