[Haskell-cafe] Subsequence near solved hopefully
Remi Turk
rturk at science.uva.nl
Sun Oct 17 15:11:51 EDT 2004
On Sun, Oct 17, 2004 at 11:41:59AM -0700, Peter Stranney wrote:
> Thanks guys for all your help, finally through code, sweat and tears i have found the solution;
>
> isSubStrand:: String -> String -> Bool
> isSubStrand [] [] = True
> isSubStrand [] (y:ys) = False
> isSubStrand (x:xs) [] = False
> isSubStrand (x:xs) (y:ys)
> | length(x:xs)>length(y:ys) = False
> | take (length (x:xs)) (y:ys)==(x:xs) = True
> | otherwise = isSubStrand (x:xs) ys
>
> thanks again
> Peter Stranney
Now that you found it, we might as well tell you the other
solution:
import List
-- Point-free (beware of the monomorphism-restriction)
isSubStrand' :: Eq a => [a] -> [a] -> Bool
isSubStrand' = flip (.) tails . any . isPrefixOf
-- and point-full
isSubStrand'' x y = any (x`isPrefixOf`) (tails y)
Groetjes,
Remi "feeling mean" Turk
--
Nobody can be exactly like me. Even I have trouble doing it.
More information about the Haskell-Cafe
mailing list