Haskell + hgl, 13 bytes
cP$h_<*h'>~ʃ
If you can simply submit a parser object, then that can be 10 bytes:
h_<*h'>~ʃ
Explanation
h_
parses at least one thing off the front of a list (think.+
in regex).h'
parses any amount off the front of a list (think.*
in regex).ʃ
is a function which takes an string and produces a parser which matches exactly that string.
We combine the two with <*
which runs both sequentially giving the result of the first. Then we bind this to ʃ
, so that the result of h_
is passed as the input to ʃ
.
In do notation this would be
do x<-h_;h';ʃ x
Described in plain English this matches any string that contains the same non-empty section at both the start and end.
Non-parser, 17 15 bytes
The parsers are obviously the way to go for this challenge, but I thought I'd give it a go without them to see how good hgl does.
lt 2<(cn**sw)sx
Explanation
sx
gets all prefixes of a listsw
checks if a particular list starts with another.cn
counts the number of elements that satisfy a predicate.(**)
is an infix forliftA2
.
So altogether (cn**sw)sx
counts the number of suffixes of the list are also prefixes.
Now, the empty string and the input string should always pass this test. So we want to test if there is another string which also is both a prefix and a suffix. So we use lt 2
to check that's greater than 2
.
Notes
Some things that could have been better for hgl in this challenge.
- There should be a function
lt2=lt 2
(andgt1
,lt3
etc.). That would have saved a byte here. (**)
has the default precedence which although not causing any issues here, could probably be improved.- Variants of
px
andsx
that give for example non-empty or strict prefixes / suffixes would probably be useful. If I had a function that specifically gave non-empty and strict suffixes then I could just doay**sw$sxsay**sw$sxS
(wheresxssxS
is the imagined function) for 10 bytes.