Search Function With Wildcard in Haskell
Update

Yes, that's better.

Here is a version of your code that uses pattern matching more and guards less. I think it is clearer.

ssearch _ [] = 0
ssearch [] _ = 0
ssearch ('*' : _) _ = error "no '*' allowed in hay" --no confusion with wildcard things
ssearch haystack ('*' : needles) = ssearch haystack needles --removes '*', and then searches through hay for the char after the '*'
ssearch (hay:haystack) (needle:needles)
  | needle == hay = search2 haystack needles + ssearch haystack (needle:needles) 
    where
    search2 _ [] = 1
    search2 [] _ = 0
    search2 ('*' : _) _ = error "no '*' allowed in hay" -- I need to declare this twice because search2 often jumps ahead of ssearch.
    search2 _ ['*'] = 1 
    search2 haystack ('*' : needles) = ssearch haystack needles 
    search2 (hay:haystack) (needle:needles)
      | needle == hay = search2 haystack needles 
    search2 _ _ = 0
ssearch (_:haystack) needles = ssearch haystack needles


There may still be bugs, but I can't see any :-)

Original


It appears to be buggy.


I expect search "hello" "he*lo" to result in 1, but it gives me 0.
I expect search "chanchance" "chance" to result in 1, but it gives me 0.


Also


search "bobobob" "bob" results in 2, but perhaps it should result in 3. Whether or not you want your function to count overlapping matches, you should document that it does/doesn't.


You need to rethink your algorithm. (Edit: as you are learning Haskell, I presume you'd rather get your function working yourself, rather than have us spoon-feed you a working version.)
x and y are not very descriptive names. They are suitable when you are writing generic code, but this code is more specific. I would call them haystack and needle instead (from the common English phrase).
This line:

  | (y == '*') &amp;&amp; ((head ys) == '*') = error "You can not include two '**' in a row, that messes up the wildcard operator"



ys may be [], so head ys may be an error. You should be using pattern matching instead of head (and often instead of comparing to [] as well).
Why should a second * be an error anyway? The second * should successfully match a run of 0 characters, surely?