Question

So I'm new to Haskell and I have to program Rabin Karps algorithm. I feel like my answer should work, but I keep getting a "parse error on 'let'" error when I compile. Could anybody help me out.

Here is my code:

import Data.Char

hash :: String -> Int
hash [] = -1
hash (x:xs) = ((ord x))

rabinKarp :: String -> String -> Bool
rabinKarp [] _ = False
rabinKarp mainString patternString
    let
     hashPattern = hash patternString
     hashMain = hash (take (length pattern) mainString)
    if hashPattern == hashMain
    then True
    else rabinKarp (drop 1 mainString) patternString
Was it helpful?

Solution

you're missing a =, and also an in

rabinKarp mainString patternString =
    let
     hashPattern = hash patternString
     hashMain = hash (take (length pattern) mainString)
    in if hashPattern == hashMain
     then True
     else rabinKarp (drop 1 mainString) patternString

EDIT: in

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top