Created
December 31, 2015 20:45
-
-
Save lispm/f9c5ca800979f3a4adcd to your computer and use it in GitHub Desktop.
TAKE in Scheme, CL and Miranda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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