nr: #2 dodano: 20171218 22:12
if you look into scheme's take and drop functions you can achieve what you want. for example observe this simple procedure:
(define (splitparts lst num)
(letrec ((recurse
(lambda (lst num acc)
(if (null? lst)
acc
(recurse (drop lst num) num (append acc (list (take lst num))))))))
(recurse lst num '())))
> (splitparts '(1 2 3 4) 2)
((1 2) (3 4))
> (splitparts '(1 2 3 4 5 6 7 8) 2)
((1 2) (3 4) (5 6) (7 8))
now the problem with this is that you if take and drop expect the list to have at least the number of elements that you are requesting.
so we can write our own versions that take up to some number of elements and if they get less they don't care. here is such an implementation of take as inspired by this thread with a properly tail recursive implementation
(define (takeup to lst)
(letrec ((recurse
(lambda (to lst acc)
(if (or (zero? to) (null? lst))
acc
(recurse ( to 1) (cdr lst) (append acc (list (car lst))))))))
(recurse to lst '())))
> (takeup 5 '(1 2 3))
(1 2 3)
> (takeup 5 '(1 2 3 4 5 6 7))
(1 2 3 4 5)
now you can easily write your splitparts function when you implement a similar dropupto function. In common lisp you have the subseq function that you can use to achieve functionality similar to take and drop.
EDIT: common lisp implementations of simple take and drop (please excuse my very non idiomatic CL)
;; recursive implemention of take just for demo purposes.
(defun takeinner (lst num acc)
(if (or (= num 0) (null lst))
acc
(takeinner (cdr lst) ( num 1) (append acc (list (car lst))))))
(defun take (lst num)
(takeinner lst num '()))
;; of course take can be implemented using subseq as drop.
(define takealternative (lst num)
(subseq lst 0 num))
(defun drop (lst num)
(subseq lst num))
(defun splitpartsinner (lst num acc)
(if (null lst)
acc
(splitpartsinner (drop lst num) num (append acc (list (take lst num))))))
(defun splitparts (lst num)
(splitpartsinner lst num '()))
> (splitparts '(1 2 3 4) 2)
((1 2) (3 4))
this will suffer from the problem described above so you still have to implement the upto versions.
