if i have somthing like that:
func (x1:x2:x3:xs) = xs
then x1,x2,x3
must exist, yes?
they can't be []
, but must(again, MUST) be with a value, yes?
also, the xs
can be []
or [a]
or [a,a,a]
(etc'), yes?
(in [a]
i mean that it's a list with one number, and [a,a,a]
is list of three numbers).
also i have function that define isPrefixOf:
myIsPrefixOf :: (Eq a) => [a] -> [a] -> Bool
[] `myIsPrefixOf` [] = True
[] `myIsPrefixOf` (x:xs) = True
list `myIsPrefixOf` [] = False
(l:ls) `myIsPrefixOf` (x:xs) = if l == x then ls `myIsPrefixOf` xs
else False
if i remove the first pattern, that the function will look like this:
myIsPrefixOf :: (Eq a) => [a] -> [a] -> Bool
[] `myIsPrefixOf` (x:xs) = True
list `myIsPrefixOf` [] = False
(l:ls) `myIsPrefixOf` (x:xs) = if l == x then ls `myIsPrefixOf` xs
else False
and now i will write:
[] `myIsPrefixOf` []
i will get: False(it should be True).
is it because that the first pattern has in his right side element: (x:xs)
, and because of that, x
MUST be with a value, therefore i pass through the first pattern, and get to the second pattern:
list `myIsPrefixOf` [] = False
which match, and return False.
am i right?
if i'm right, then the difference is that if i write (x:xs)
, x
MUST be a value and NOT []
.
on the other hand, if i write list
, it can be match against []
and [a]
and [a,a,a]
(etc'), and because of that, list
of the second pattern, will match to the the first []
in my input, and therefore i'll get False ?
(as before, in [a]
i mean that it's a list with one number, and [a,a,a]
is list of three numbers).
also, to correct this situation, i need to replace:
[] myIsPrefixOf
(x:xs) = True
with that:
[] `myIsPrefixOf` list = True
and now the expressions:
[] `myIsPrefixOf` []
[] `myIsPrefixOf` [1,2,3]
will both match agains:
[] `myIsPrefixOf` list = True
hope i'm right on those things, and now for another question:
here is the fixed function from the start(after applying the changes)
myIsPrefixOf :: (Eq a) => [a] -> [a] -> Bool
[] `myIsPrefixOf` list = True
list `myIsPrefixOf` [] = False
(l:ls) `myIsPrefixOf` (x:xs) = if l == x then ls `myIsPrefixOf` xs
else False
now, if i remove the second pattern match, that the function will look like this:
myIsPrefixOf :: (Eq a) => [a] -> [a] -> Bool
[] `myIsPrefixOf` list = True
(l:ls) `myIsPrefixOf` (x:xs) = if l == x then ls `myIsPrefixOf` xs
else False
and call the function like that:
[1,2] `myIsPrefixOf` [1]
i get an error that said there is no exhaustive patterns in the function.
i want to see if i understand why it is happening.
the function pass through the first pattern and get to the second one:
(l:ls) `myIsPrefixOf` (x:xs) = if l == x then ls `myIsPrefixOf` xs
else False
so:
[1,2] `myIsPrefixOf` [1]
and:
l == x
.
they both 1
, so i match again the second pattern:
(2:[]) `myIsPrefixOf` ([]:[])
now, l == 2
, but x == []
and because that, the expression: l == x
returns the no-exhaustive pattern...
is it because i'm trying to check for equality between a number and a list?
the equality parameter (==) should check only elements that are in the same type?
(i.e. : 'a' == 'b'
or 1 == 3
)
well, do i understand it all right? :-)
thanks a lot :-).