AM Posted May 7, 2017 perhaps there's a OM-solution... in this case it's to hard to find... (search-engine?) otherwise... (defun integer-to-binary-lengths* (alist) (loop for i in alist when (> (abs i) 1) append (append (list 1) (loop repeat (- (abs i) 1) collect 0)) else collect 1)) (integer-to-binary-lengths* '(2 2 2 1 1 4 4 4 4)) (integer-to-binary-lengths* '(6 4 8 5 2 1 10 2)) Quote Share this post Link to post Share on other sites
opmo Posted May 7, 2017 (binary-to-decimal '(1 1 0)) => 6 (decimal-to-binary '(9 3 6 1 10 11)) => ((1 0 0 1) (1 1) (1 1 0) (1) (1 0 1 0) (1 0 1 1)) Quote Share this post Link to post Share on other sites
AM Posted May 7, 2017 it's not decimal-to-binary!!! another idea... for example... 6 => 1 0 0 0 0 0 = a "1" and 5 times a "0") Quote Share this post Link to post Share on other sites
Stephane Boussuge Posted May 8, 2017 I've made this long time ago, bit different but may be also of interest, this function use a specs of number of "1" and "0" in input: ;;; GEN-BINARY_INTEGER (defun gen-binary-integer (one-list zero-list &key (flatten t)) (do-verbose ("gen-binary-integer") (if flatten (flatten (loop for i in one-list for o in (gen-trim (length one-list) zero-list) collect (append (gen-repeat i '(1)) (gen-repeat o '(0)) ))) (loop for i in one-list for o in (gen-trim (length one-list) zero-list) collect (append (gen-repeat i '(1)) (gen-repeat o '(0)) ))))) #| USAGE (gen-binary-integer '(3 4 3 2 4 2 5) '(1 1 3)) => (1 1 1 0 1 1 1 1 0 1 1 1 0 0 0 1 1 0 1 1 1 1 0 1 1 0 0 0 1 1 1 1 1 0) (gen-binary-integer '(3 4 3 2 4 2 5) '(1 1 3) :flatten nil) => ((1 1 1 0) (1 1 1 1 0) (1 1 1 0 0 0) (1 1 0) (1 1 1 1 0) (1 1 0 0 0) (1 1 1 1 1 0)) (setf intg '(3 2 4 5 3)) (setf zero-k '(1 2 1 3)) (gen-binary-integer intg zero-k) => '(1 1 1 0 1 1 0 0 1 1 1 1 0 1 1 1 1 1 0 0 0 1 1 1 0) |# S. 1 AM reacted to this Quote Share this post Link to post Share on other sites