Sujet : Re: Given string 'a.bc.' -- replace each dot(.) with 0 or 1
De : enometh (at) *nospam* meer.net (Madhu)
Groupes : comp.lang.lispDate : 19. May 2024, 05:28:05
Autres entêtes
Organisation : Motzarella
Message-ID : <m3jzjqlbui.fsf@leonis4.robolove.meer.net>
References : 1
* HenHanna <
v29p14$2mr5l$2@dont-email.me> :
Wrote on Sat, 18 May 2024 01:31:32 -0700:
How can i write this function simply? (in Common Lisp)
>
-- Given a string 'a.bc.' -- replace each dot(.) with 0 or 1.
>
-- So the value is a list of 4 strings:
('a0bc0' 'a0bc1' 'a1bc0' 'a1bc1')
(defun adjusted-logbitp (pos num total-width)
(let* ((int-len (integer-length num))
(i (- pos (- total-width int-len))))
(if (< i 0)
nil
(logbitp i num))))
(defun bindots (str)
(let* ((indices (loop for c across str for i from 0
if (eql c #\.) collect i))
(width (length indices)))
(loop for i below (expt 2 width)
collect (let ((ret (copy-seq str)))
(loop for j in indices
do (setf (aref ret j)
(if (adjusted-logbitp j i width)
#\1
#\0)))
ret))))