I wrote this bit of Lisp code. I'd appreciate comments for improvement, including comments about formatting, design (including choice of data stuctures) and performance. This was tested with sbcl 1.0.55.0 and written in emacs 23.2 with slime 20111027-2 on Debian squeeze. The formatting is therefore at least partly emacs formatting, though I did decide what should go on each line.

(defun inc (x endpos)
  ;;; Given a vector of the form V=(V_1, V_2, V_3,...V_k))
  ;;; with V_i in the set {"A", "C", "G", "T"}
  ;;; increment the vector. This behaves as though {"A", "C", "G", "T"} were the
  ;;; numbers 0, 1, 2, 3 in base 4. E.g. ("A", "C", "G", "T") increments to
  ;;; ("A", "C", "T", "A") is equivalent to ("0", "1", "2", "3") incrementing to
  ;;; ("0", "1", "3", "0")
  (let ((y (copy-seq x)))
    (case (aref y endpos)
      (#\A (setf (aref y endpos) #\C))
      (#\C (setf (aref y endpos) #\G))
      (#\G (setf (aref y endpos) #\T))
      (#\T (setf (aref y endpos) #\A)
       (when (> endpos 0)
         (setf y (inc y (- endpos 1))))))
    y))

(defun genstrings (stringlen)
  ;;; Generate all possible strings of length 'stringlen' containing
  ;;; the characters {"A", "C", "G", "T"}
  (let ((stringnum (expt 4 stringlen))
    (s (make-array stringlen :element-type 'character :adjustable t :initial-element #\A :fill-pointer t))
    (strlst (make-array 0 :element-type 'string :adjustable t :fill-pointer 0)))
    (dotimes (i stringnum)
      (vector-push-extend s strlst)
      (setf s (inc s (- stringlen 1))))
    strlst))
share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.