Jump to content

Recommended Posts

Posted

if you want to pick a sample from approx.center (depends on odd/even)  of a list... 

 

(defun pick-sample-from-center (list span)
  (let ((center (if (evenp (length list))
                  (/ (length list) 2)
                  (/ (1+ (length list)) 2)))
        (span (if (> span (length list))
                    (length list)
                    (append span))))
    (loop repeat span
      with startpoint = (if (evenp span)
                          (- center (/ span 2))
                          (- center (/ (1+ span) 2)))

      for i = startpoint then (incf startpoint)
      collect (nth i list))))


;;;EXAMPLES:

(pick-sample-from-center '(1 2 3 4 5 4 3 2 1) 7)
=> (2 3 4 5 4 3 2)

(pick-sample-from-center '(1 2 3 4 5 4 3 2 1) 3)
=> (4 5 4)

(pick-sample-from-center '(1 2 3 4 5 4 3 2 1) 6)
=> (3 4 5 4 3 2)

(pick-sample-from-center '(1 2 3 4 5 4 3 2 1) 20) ; (if (> span length) => input-list as output
=> (1 2 3 4 5 4 3 2 1)

 

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