So I've got an assignment to Write a function
search_string:: String -> String -> Integer
search_string mainstring substring = ...
that takes two string arguments mainstring and substring and examines if substring occurs as a substring of mainstring. If so, the function should compute the position in mainstring where substring starts. If substring occurs multiple times it is the position of the first occurrence that should be computed. If substring does not occur in mainstring the function should compute -1
.
So this is my code so far:
search_string main sub = if sub == take (length sub) main then 0 else search_string_aux main sub 1
search_string_aux main sub n = if sub == search_string (drop n main) sub then n else search_string_aux main sub (n+1)
when I try to load it haskell tells me:
Labb4hs:11:79:
Couldn't match expected type '[a0]' with actual type 'Int'
In the expression : n
In the expression:
If sub == search_string (drop n main) sub then n
else search_string_aux main sub (n+1)
In an equation for `search_string_aux':
search_string_aux main sub n = if sub == search_string (drop n main) sub then n
else search_string_aux main sub (n+1)
So it seems Haskell thinks that n should be a [a0] =(string ???)
with actual type Int
, and I want to make n
an Int
always so I don't get why haskell thinks it's a [a0]
. Would gladly appreciate some help since I'm new to Haskell and programming in general and I have no clue whatsoever on how to fix it.
Try adding a type signature to
search_string_aux
. This will make the error a bit easier to find.if sub == search_string
...but wait, doesn'tsearch_string
returnInteger
? Yetsub
is meant to beString
... Something is not right here.