Jump to content

read-list-in-steps -> another idea to code this?


Recommended Posts

is there another way to code such a function/idea?

this is (at the moment) a "theoretically function"... no concret use - l'art pour l'art :-)

thanx for smarter LISP-code-IDEAS!

andré

 

;;; evaluate PROGN (as a reset)

(progn
  (defstruct counter n)
  (defvar cnt)
  (setf cnt (make-counter :n -1))
  (defun read-list-in-steps (alist)
    (nth (setf (counter-n cnt) (1+ (counter-n cnt))) alist)))

;;; evaluate a view times, so one value after the other will be in the output
;;; you have to evaluate the progn-seq before every new start!!!

(read-list-in-steps '(1 2 3 4 5 6))
(read-list-in-steps '(c4 f4 e4 f4 g5))

 

Link to comment
Share on other sites

great... that's a smart solution :-) simpler then mine

...i didn't know how to set "-1" outside the FUNCTION without DEFSTRUCT

thanx!!

 

i like it when the program tells me that it has done the job... so i coded a little extension:

(let ((i -1))
  (defun next (liste &key (stop 'nil) (one-cycle 'nil)) 
    (if (equal stop 't)
      (if (< i (1- (length liste)))
        (nth (mod (incf i) (length liste)) liste)
        (if (equal one-cycle 'nil)
          (progn 
            (setf i -1)
            'nil)
          'nil))
      (nth (mod (incf i) (length liste)) liste))))
        

(next '(a b c d e f))
(next '(a b c d e f) :stop t :one-cycle nil) ;; shows a NIL after last value, then starts again
(next '(a b c d e f) :stop t :one-cycle t) ;; shwows only NILs after the last value

 

Edited by AM
small code correction
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

Terms of Use Privacy Policy