Hi Alex, Thanks for the reply, and for suggesting a better solution than the one I made up. I have no immediate use for such a 'sindex' function, and maybe this is not a function that one would need very often when doing things the PicoLisp way.
Have a nice weekend! /Jon On 12. sep. 2014, at 16:06, Alexander Burger <[email protected]> wrote: > 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 > > pre? > sub? > uppc > lowc > > 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 PԔ � &j)m����X�����zV�u�.n7�
