Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Thinking over dave4420's comments dave4420's comments, I decided to redo the entire algorithm. I think that it fixes most of the bugs.

Thinking over dave4420's comments, I decided to redo the entire algorithm. I think that it fixes most of the bugs.

Thinking over dave4420's comments, I decided to redo the entire algorithm. I think that it fixes most of the bugs.

added version 3
Source Link
user4253
user4253

#Latest Update

This update used pattern matching and less guards, and also outputs 1 if ssearch "blah" "*".

ssearch _ [] = 0
ssearch [] _ = 0
ssearch ('*':_) _ = error "no '*' allowed in hay"
ssearch _ ['*'] = 1
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) 
 | otherwise = 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 
 | otherwise = 0

#Update:

#Update:

#Latest Update

This update used pattern matching and less guards, and also outputs 1 if ssearch "blah" "*".

ssearch _ [] = 0
ssearch [] _ = 0
ssearch ('*':_) _ = error "no '*' allowed in hay"
ssearch _ ['*'] = 1
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) 
 | otherwise = 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 
 | otherwise = 0

#Update:

Tweeted twitter.com/#!/StackCodeReview/status/202945401689612288
deleted 132 characters in body
Source Link
user4253
user4253

#Update:

Thinking over dave4420's comments , I decided to redo the entire algorithm. I think that it fixes most of the bugs.

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

I'm wondering if there are still any bugs, and also, if there is any way I can improve this code.

##Old Code

I'm wondering if there is any way I can improve the performance of it, or to reduce the size of the code. Any other feedback would be welcome.

#Update:

Thinking over dave4420's comments , I decided to redo the entire algorithm. I think that it fixes most of the bugs.

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

I'm wondering if there are still any bugs, and also, if there is any way I can improve this code.

I'm wondering if there is any way I can improve the performance of it, or to reduce the size of the code. Any other feedback would be welcome.

#Update:

Thinking over dave4420's comments , I decided to redo the entire algorithm. I think that it fixes most of the bugs.

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

I'm wondering if there are still any bugs, and also, if there is any way I can improve this code.

#Update:

Thinking over dave4420's comments , I decided to redo the entire algorithm. I think that it fixes most of the bugs.

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

I'm wondering if there are still any bugs, and also, if there is any way I can improve this code.

##Old Code

added fixed code
Source Link
user4253
user4253
Loading
Source Link
user4253
user4253
Loading
lang-hs

AltStyle によって変換されたページ (->オリジナル) /