Hi Jon,

> The other day I noticed that I hadn't seen a PicoLisp function for
> locating a string fragment within a longer string. Is there one that I
> just have failed to notice?

Yes, that's right. In fact, the only functions which I would regard as
"string manipulations" (i.e. operate directly on a symbol name) are


This is on purpose. In general, arbitrary manipulations of string data
are supposed to be done with the (quite rich) set of List functions
(after 'chop'ing the symbol name, for example). This gives much more
flexibility and avoids duplicate functionalities.

> It is quite common for programming languages
> to have an "index" function that does this. PicoLisp has an 'index'
> function, but that one cannot be used like this: (index "Li" "PicoLisp")

The above 'sub?' is close to it, but doesn't return a position.

> One way to make up the function I'm missing, could be like this:
> (de sindex (Frag Src)
>    (if (match (make (link '@A) (mapcar link (chop Frag)) (link '@B)) (chop 
> Src))
>       (length @A) ) )

It is not a good idea to build a list only to count its length. Better
count in a loop directly, e.g.:

   (de sindex (Frag Src)
      (for ((I . L) (chop Src) L (cdr L))
         (T (pre? Frag L) I) ) )

> Then (sindex "Li" "PicoLisp") would give 4. Maybe it would be
> preferable for it to give 5.

Yes, the above returns 5. This is more in sync with the PicoLisp
convention of counting elements from 1.

> As it is quite common for PicoLisp functions to have different
> behaviours depending on the type of input, the behaviour of my 'sindex'
> could also be made a part of the standard PicoLisp 'index' function.

I must say that rings an alarm in my head when index numbers are used to
operate on lists (or symbol names). Usually there is a better way. What
do you need this function for?

♪♫ Alex
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe

Reply via email to