Haskell substring testing -
When I use the sub_string ("abberr", "habberyry")
It is, obviously, it should be wrong. The point of the function is to search for the first argument within the second one. What is a wrong idea?
sub-string :: (string, string) - & gt; Boole sub-string (_, []) = incorrect sub-string ([], _) = true sub string (a: x, b: y). A / = b = sub_string (a: x, y). Otherwise, you must first switch to the = sub-string (x, y)
[]
and when you are matching, say, substring "abc" "abc"
second, this Is idiomatic Haskell, which is to write a function with two arguments instead of one with one argument. So your code should start: substring :: string -> String - & gt; Bool substring [] _ = true substring _ [] = wrong substring needle (h: aystack) | ...
Now we get to the toughest case, where both of these lists are not empty, there is a problem with the recurring substring as bs
The result of the substrings of code> "abc" such as "axbxcx"
will be returned (because "ABC"
will be the first match 'a'
first, then "bc"
will be seen in the string; substrings algorithm will then be left to see "bxcx"
in "bc"
Code> 'x' passed by 'B'
and search for "xcx"
in "c"
, which will return true
.
Instead your position is more complete. If you are ready to use the function from Data.List
then it is:
| IsPrefixOf needle (h: aystack) = true | otherwise = substring needle Aystack
Otherwise you will have to write your own isPrefixOf
, for example:
isPrefixOf needle greenstack = needle == take (length needle) pile of grass
Comments
Post a Comment