[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: confused newbie (in asbestos Y-fronts...

William James

2/1/2016 8:30:00 PM

Martti Halminen wrote:

> > by James L. Noyes using XEmacs v.21.1. I'm trying to write a bit of
> > code to return the root string of a word with a given ending. (e.g
> > (suffix-match 'ing 'match) should return "match" (if no match then
> > "nil"))
> >
> > ;;;(I've labelled the ending 'e' and the word 'w')
> >
> > (defun suffix-match (e w)
> > (if (string= (subseq (reverse w) 0 (length e)) (reverse e))
> > (subseq w 0 (- (length w) (length e))) nil))
> >
> > when i try to run it it returns an error - "listp, w". Can anyone tell
> > me of the errors in my code and why w has to be a list when I'm using
> > string-processing "tools".
>
> - The other reply handled most of your questions. For this particular
> piece, it seems to run OK in a Common Lisp when called with strings
> instead of symbols.
>
> - All this reverse/subseq stuff seems superfluous, this seems to work,
> too:
>
> (defun suffix-match2 (e w)
> (if (string= w e :start1 (- (length w) (length e)))
> (subseq w 0 (- (length w) (length e)))
> nil))

MatzLisp (Ruby):

def suffix_match ending, word
word.end_with?(ending) and word[0 ... word.size - ending.size]
end

suffix_match "ing", "acting"
==>"act"
suffix_match "ing", "acts"
==>false
suffix_match "ed", "acted"
==>"act"

--
Use this [sword] for me, if I rule well; if not, against me. --- Trajan
"If a government uses the instruments of power in its hands for the purpose of
leading a people to ruin, then rebellion is not only the right but also the
duty of every individual citizen."