String matching: Difference between revisions

Content added Content deleted
(→‎{{header|Fortran}}: added modern fortran version)
m (→‎TXR Lisp: Drop @(do ...); rename script to cmatch2.tl)
Line 2,757: Line 2,757:
===TXR Lisp===
===TXR Lisp===


<lang txr>@(do
<lang txrlisp>(tree-case *args*
((big small)
(tree-case *args*
((big small)
(cond
(cond
((< (length big) (length small))
((< (length big) (length small))
(put-line `@big is shorter than @small`))
(put-line `@big is shorter than @small`))
((str= big small)
((str= big small)
(put-line `@big and @small are equal`))
(put-line `@big and @small are equal`))
((match-str big small)
((match-str big small)
(put-line `@small is a prefix of @big`))
(put-line `@small is a prefix of @big`))
((match-str big small -1)
((match-str big small -1)
(put-line `@small is a suffix of @big`))
(put-line `@small is a suffix of @big`))
(t (let ((pos (search-str big small)))
(t (let ((pos (search-str big small)))
(if pos
(if pos
(put-line `@small occurs in @big at position @pos`)
(put-line `@small occurs in @big at position @pos`)
(put-line `@small does not occur in @big`))))))
(otherwise
(put-line `@small does not occur in @big`))))))
(put-line `usage: @(ldiff *full-args* *args*) <bigstring> <smallstring>`)))</lang>
(otherwise
(put-line `usage: @(ldiff *full-args* *args*) <bigstring> <smallstring>`))))</lang>
{{out}}
{{out}}
<pre>$ txr cmatch2.txr x
<pre>$ txr cmatch2.tl x
usage: txr cmatch2.txr <bigstring> <smallstring>
usage: txr cmatch2.tl <bigstring> <smallstring>
$ txr cmatch2.txr x y z
$ txr cmatch2.tl x y z
usage: txr cmatch2.txr <bigstring> <smallstring>
usage: txr cmatch2.tl <bigstring> <smallstring>
$ txr cmatch2.txr catalog cat
$ txr cmatch2.tl catalog cat
cat is a prefix of catalog
cat is a prefix of catalog
$ txr cmatch2.txr catalog log
$ txr cmatch2.tl catalog log
log is a suffix of catalog
log is a suffix of catalog
$ txr cmatch2.txr catalog at
$ txr cmatch2.tl catalog at
at occurs in catalog at position 1
at occurs in catalog at position 1
$ txr cmatch2.txr catalog catalogue
$ txr cmatch2.tl catalog catalogue
catalog is shorter than catalogue
catalog is shorter than catalogue
$ txr cmatch2.txr catalog catalog
$ txr cmatch2.tl catalog catalog
catalog and catalog are equal
catalog and catalog are equal
$ txr cmatch2.txr catalog dog
$ txr cmatch2.tl catalog dog
dog does not occur in catalog</pre>
dog does not occur in catalog</pre>