On 6/3/2024, B. Pym wrote:
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.
SP-Forth
Using a list.
REQUIRE { ~ac/lib/locals.f
REQUIRE StringReplace2 ~nn\lib\string.f
REQUIRE printf<[ ~ilya\Lib\win\System\printf.f
REQUIRE list-all ~ygrek/lib/list/all.f \ all words for cons pair lists
list ALSO!
REQUIRE CASE-INS lib/ext/caseins.f \ Case-insensitive.
: get-bin-digits ( u n -- ...) 0 do dup 1 and swap 2 / loop drop ;
: str-0-term ( adr) count + 0 swap c! ;
: s-keep ( cadr len -- adr) here place here 256 allot ;
create form-str 257 allot
0 value cnt
0 value mylist
: dotty { adr len -- }
\ Doesn't terminate output string with 0-byte.
form-str adr len s" ." s" %d" StringReplace2
form-str str-0-term
len - to cnt drop
%[ \ Start a list.
1 cnt 0 do 2 * loop
0 do
printf<[ i cnt get-bin-digits form-str count ]>
s-keep % \ Add to list.
loop
]%
;
s" apple.bun.c.d" dotty to mylist
cr s" Length of list is " type mylist length . cr
mylist :noname count type cr ; iter
===>
Length of list is 8
apple0bun0c0d
apple0bun0c1d
apple0bun1c0d
apple0bun1c1d
apple1bun0c0d
apple1bun0c1d
apple1bun1c0d
apple1bun1c1d
Just for fun, let's add up the lengths of the strings
in the list.
0 mylist :noname count nip + ; iter .
===>
104
Let's convert the strings to uppercase.
REQUIRE UPPERCASE ~ac/lib/string/uppercase.f
mylist :noname count uppercase ; iter
mylist :noname count type cr ; iter
===>
APPLE0BUN0C0D
APPLE0BUN0C1D
APPLE0BUN1C0D
APPLE0BUN1C1D
APPLE1BUN0C0D
APPLE1BUN0C1D
APPLE1BUN1C0D
APPLE1BUN1C1D
Let's sort by the penultimate character.
: get-ch ( cadr n -- chr ) drop 11 + c@ ;
mylist :noname count get-ch swap count get-ch swap < ; sort
mylist :noname count type cr ; iter
===>
APPLE0BUN0C0D
APPLE0BUN1C0D
APPLE1BUN0C0D
APPLE1BUN1C0D
APPLE0BUN0C1D
APPLE0BUN1C1D
APPLE1BUN0C1D
APPLE1BUN1C1D
Filtering. Make a new list that
only has strings containing "N0".
%[ mylist
:noname
dup count s" N0" StringGetPos
if % else drop then ;
iter
]%
:noname count type cr ; iter
===>
APPLE0BUN0C0D
APPLE1BUN0C0D
APPLE0BUN0C1D
APPLE1BUN0C1D
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)