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')
-- The order is not important.
If the string has 3 dots, the value is a list of length 8.
If the program is going to be simpler,
pls use, e.g. (a $ b c $) rather than 'a.bc.'
(bindots "abc")("abc")
(bindots "a.bc")("a0bc" "a1bc")
(bindots "a.b.c")("a0b0c" "a0b1c" "a1b0c" "a1b1c")
(bindots "a.b.cd.e")("a0b0cd0e" "a0b0cd1e" "a0b1cd0e" "a0b1cd1e" "a1b0cd0e" "a1b0cd1e"
If the program is going to be simpler,
pls use, e.g. (a $ b c $) rather than 'a.bc.'
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 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))))
what about substitute? common lisp is not scheme of the week.
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')
-- The order is not important.
If the string has 3 dots, the value is a list of length 8.
If the program is going to be simpler,
pls use, e.g. (a $ b c $) rather than 'a.bc.'
On 2024-05-18, HenHanna <HenHanna@devnull.tb> wrote:
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')
-- The order is not important.
If the string has 3 dots, the value is a list of length 8.
If the program is going to be simpler,
pls use, e.g. (a $ b c $) rather than 'a.bc.'
TXR Lisp:
(defun bindots (str)
(let* ((s (copy str))
(ixs (where (op eql #\.) s))
(n (len ixs)))
(collect-each ((digs (rperm '(#\0 #\1) n)))
(set [s ixs] digs)
(copy s))))
(mapcar (lop digits 2) 0..16)((0) (1) (1 0) (1 1) (1 0 0) (1 0 1) (1 1 0) (1 1 1) (1 0 0 0)
(iter-begin '(a b c))(a b c)
(iter-more *1)t
(iter-item *1)a
(iter-step *1)(b c)
(iter-begin "abc")#<seq-iter: 85d75f0>
(iter-more *5)t
(iter-item *5)#\a
(iter-step *5)#<seq-iter: 85d75f0>
(iter-item *5)#\b
(iter-begin 3) ;; identity3
(iter-more *1) ;; unconditional truet
(iter-item *1) ;; identity3
(iter-step *1) ;; successor function4
(mapcar (do pic `>>:0#` @1 @2) "AA".."DD" 0)("AA:00" "AB:01" "AC:02" "AD:03" "BA:04" "BB:05" "BC:06" "BD:07"
HenHanna <HenHanna@devnull.tb> writes:
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')
-- The order is not important.
If the string has 3 dots, the value is a list of length 8.
If the program is going to be simpler,
pls use, e.g. (a $ b c $) rather than 'a.bc.'
Another one:
(defun subst-dots (s &optional (pos 0))
(let ((p (search "." s :start2 pos)))
(if p
(append
(subst-dots (replace (copy-seq s) "0" :start1 p) (1+ p))
(subst-dots (replace (copy-seq s) "1" :start1 p) (1+ p)))
(list s))))
Regards
On 5/19/2024 11:36 AM, Joerg Mertens wrote:
HenHanna <HenHanna@devnull.tb> writes:
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')
-- The order is not important.
If the string has 3 dots, the value is a list of length 8.
If the program is going to be simpler,
pls use, e.g. (a $ b c $) rather than 'a.bc.'
Another one:Nice... Thanks
(defun subst-dots (s &optional (pos 0))
(let ((p (search "." s :start2 pos)))
(if p
(append
(subst-dots (replace (copy-seq s) "0" :start1 p) (1+ p))
(subst-dots (replace (copy-seq s) "1" :start1 p) (1+ p)))
(list s))))
Regards
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')
-- The order is not important.
If the string has 3 dots, the value is a list of length 8.
If the program is going to be simpler,
pls use, e.g. (a $ b c $) rather than 'a.bc.'
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')
-- The order is not important.
If the string has 3 dots, the value is a list of length 8.
On 5/18/2024, HenHanna wrote:
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')
-- The order is not important.
If the string has 3 dots, the value is a list of length 8.
If the program is going to be simpler,
pls use, e.g. (a $ b c $) rather than 'a.bc.'
Gauche Scheme:
(define (dotty s)
(define (f r) (dotty (regexp-replace "[.]" s r)))
(if (string-scan s #\.)
(apply append (map f '("0" "1")))
(list s)))
gosh> (dotty "a.b.c")
("a0b0c" "a0b1c" "a1b0c" "a1b1c")
-- 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')
-- The order is not important.
If the string has 3 dots, the value is a list of length 8.
(defun mapnconcat (fn sequences seperator)
"Like MAPCONCAT but the function is desctuctive."
(let ((seperator-length 0)
(total 0) (times -1) result)
(declare (fixnum seperator-length total times))
(unless (eq fn #'identity)
(map-into sequences fn sequences))
(setq seperator-length (the fixnum (length seperator)))
(dolist (seq sequences)
(incf times) (incf total (+ (length seq) seperator-length)))
(setq result (make-string (- total seperator-length)))
(let ((pos -1)) (declare (fixnum pos))
(loop repeat times do
(let ((seq (pop sequences)))
(loop for i across seq do
(setf (schar result (incf pos)) i))
(loop for i across seperator do
(setf (aref result (incf pos)) i))))
(loop for i across (car sequences) do
(setf (schar result (incf pos)) i)))
result))
On Tue, 28 May 2024 21:06:31 -0400, steve wrote:
(defun mapnconcat (fn sequences seperator)
"Like MAPCONCAT but the function is desctuctive."
(let ((seperator-length 0)
(total 0) (times -1) result)
(declare (fixnum seperator-length total times))
(unless (eq fn #'identity)
(map-into sequences fn sequences))
(setq seperator-length (the fixnum (length seperator)))
(dolist (seq sequences)
(incf times) (incf total (+ (length seq) seperator-length)))
(setq result (make-string (- total seperator-length)))
(let ((pos -1)) (declare (fixnum pos))
(loop repeat times do
(let ((seq (pop sequences)))
(loop for i across seq do
(setf (schar result (incf pos)) i))
(loop for i across seperator do
(setf (aref result (incf pos)) i))))
(loop for i across (car sequences) do
(setf (schar result (incf pos)) i)))
result))
How about:
(defun mapnconcat (fn sequences separator)
"Like MAPCONCAT but the function is destructive."
(let
(
(separator-length 0)
(total 0)
(times -1)
result
)
(declare (fixnum separator-length total times))
(unless (eq fn #'identity)
(map-into sequences fn sequences)
) ; unless
(setq separator-length (the fixnum (length separator)))
(dolist (seq sequences)
(incf times)
(incf total (+ (length seq) separator-length))
) ; dolist
(setq result (make-string (- total separator-length)))
(let ((pos -1))
(declare (fixnum pos))
(loop repeat times do
(let ((seq (pop sequences)))
(loop for i across seq do
(setf (schar result (incf pos)) i)
) ; loop
(loop for i across separator do
(setf (aref result (incf pos)) i)
) ; loop
) ; let
) ; loop
(loop for i across (car sequences) do
(setf (schar result (incf pos)) i)
) ; loop
) ; let
result
) ; let
) ; mapconcat
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 328 |
Nodes: | 16 (2 / 14) |
Uptime: | 94:54:12 |
Calls: | 7,264 |
Calls today: | 2 |
Files: | 12,601 |
Messages: | 5,563,755 |