2

I'm a newbie regarding Haskell (and Java for that matter) and I tried to find a solution to my probably very simple problem. I need to create two arrays where it checks if the first array is a prefix of the second one, for example [5,6,7] [5,6,7,6,2] would give me the boolean true. This is what I tried, however I have no idea how to declare an array. n should be the highest slot number of array a and it counts down. As soon as one slot of a doesn't equal b it should return False. Is there another way to implement it without the n variable?

isaprefix :: [Int] -> [Int] -> Int -> Bool
isaprefix a b n = if n==0
            then True
            else
                if a[n] == b [n]
                then isaprefix ((a[n-1]) (b[n-1]))
                else False
5
  • 1
    btw: you are talking about lists here - array are quite different beasts in Haskell ;) Commented Dec 22, 2015 at 20:07
  • Pretty sure you meant something like isaprefix (a !! (n - 1)) (b !! (n - 1)) as the syntax a[n-1] does not have any meaning in Haskell. (More accurately it is a function being applied to a one-element list of some element of the Num typeclass.) Commented Dec 22, 2015 at 20:08
  • CR gave you the answer - but you can find it yourself if you look for prefix on Hoogle you'll find this and a click on source code reveals the same implementation ;) Commented Dec 22, 2015 at 20:11
  • @CRDrost That's not what he meant either. He meant that in the if check, but the body of the if statement doesn't really make any sense. It doesn't type check for multiple reasons. I can't even come up with a simple way of correcting it. Commented Dec 22, 2015 at 23:40
  • Maybe just isaprefix a b (n-1) and then you call it with isaprefix a b (length a - 1) or something. But then if n == 0, you should return a !! 0 == b !! 0 instead of True. Commented Dec 22, 2015 at 23:42

1 Answer 1

8

Yes, there is. You use pattern matching:

-- type signature can be slightly more general: any list of equatables.
isaprefix :: (Eq a) => [a] -> [a] -> Bool

-- first are the two "boring" cases.
-- we define that empty lists are prefixes of everything.
isaprefix [] _ = True

-- then: we also define that you can't be the prefix of an empty list.
isaprefix _ [] = False

-- those 2 are technically in conflict, so you need to know that the empty list is defined,
-- by the order of those definitions, to be a prefix of the empty list.  This supports the
-- general property that `isprefixof x x == True` for all `x`.

-- ok, now here's the more interesting pattern: both lists are nonempty. We need them to
-- match on their first elements and also for the rest of the first list to prefix the 
-- rest of the second list.
isaprefix (a:as) (b:bs)
    | a == b     = isaprefix as bs
    | otherwise  = False
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.