![]() ![]() "Returns the index of the first occurence of the substring of Here's a version of substringp that accepts these start and end parameters, and uses a local recursive function to do the actual processing. Working with these kinds of functions requires a bit of care to make sure you don't provide any indices that are out of range, but it's not too bad, and you don't end up copying any strings. "toc" is in both "octocat" and "toccata"ĬL-USER> (string= "octocat" "toccata" :start1 2 :end1 5 :end2 3) For instance, string= lets you compare two strings. By using these, you can recurse and change the indices into the strings, rather than creating entirely new strings. It's inefficient to make lots of new stings by using subseq, so lots of sequence functions in Common Lisp take :start and :end arguments, or in the case of functions that take two sequences, :start1, :end1, :start2, and :end2 arguments. That in and of itself is not a problem, but recursive string processing in Common Lisp is prone to a few pitfalls. ![]() Your code, and the code that uselpa showed in another answer, are more recursive in nature. is case insensitive, in which case "CAT" is inĬL-USER> (substringp 'cat "concatenate" :test 'char-equal) You can test the characters with CHAR-EQUAL, which result of 'cat is a symbol named "CAT", which is not Default upcasing of symbol names means that the Here are some examples: CL-USER> (substringp "cat" "concatenate")ĬL-USER> (substringp "dog" "concatenate") Remember that everything except nil is a true value (even 0), so you can use the result as a boolean or as an index (as long as you check that it's not nil). Finally, since this returns the result from search, it does double duty for you: it returns the index of the first occurrence if there is an occurrence, and nil otherwise. Remember that with the standard settings, the reader upcases the names of symbols, so the symbol cat designates the string "CAT". Note the use of the string function to convert from string designators (characters, strings, and symbols) to the strings that they designate. Characters within the string are compared by TEST, whichĭefaults to CHAR= (for case-sensitive comparison)." "Returns the index of the first occurrence of the string designatedīy NEEDLE within the string designated by HAYSTACK, or NIL if it does You can implement this in terms of search: (defun substringp (needle haystack &key (test 'char=)) That's the way that the built in function search behaves, for instance. Even better, if you return the index of the string, then you find out where in the other string the first string appears. Instead, you could return the actual string, since the string "NIL" is a true value. E.g., with this approach, you'll have: (string-include 'nil 'vanilla) => nilĭid that return nil because "NIL" is in "VANILLA", because it isn't? It's ambiguous and you can't tell. In your question, you used this example: (string-include 'abd 'abbbe) => nilĪssuming that you're returning the symbols nil and ghf, you'll run into an ambiguity if you ever want to check whether a string contains the substring NIL. Return an index or the substring, not a symbol ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |