Right now I'm working on a problem in Haskell in which I'm trying to check a list for a particular pair of values and return True/False depending on whether they are present in said list. The question goes as follows:
Define a function called
after
which takes a list of integers and two integers as parameters.after numbers num1 num2
should return true ifnum1
occurs in the list andnum2
occurs afternum1
. If not it must return false.
My plan is to check the head of the list for num1
and drop it, then recursively go through until I 'hit' it. Then, I'll take the head of the tail and check that against num2
until I hit or reach the end of the list.
I've gotten stuck pretty early, as this is what I have so far:
after :: [Int] -> Int -> Int -> Bool
after x y z
| y /= head x = after (drop 1 x) y z
However when I try to run something such as after [1,4,2,6,5] 4 5
I get a format error. I'm really not sure how to properly word the line such that haskell will understand what I'm telling it to do.
Any help is greatly appreciated! Thanks :)
Edit 1: This is the error in question:
Program error: pattern match failure: after [3,Num_fromInt instNum_v30 4] 3 (Num_fromInt instNum_v30 2)
after
– Johnathan Scott