Reputation: 283
So I have a list of tuples defined like so:
type Domino = (Int,Int)
data End = L|R
type Board = [Domino]
d :: Domino
d= (4,5)
b :: Board
b= [(1,3),(3,3),(3,4)]
In my function I need to be able to gain the first part of the board. So for example I can head the board to get the domino (1,3) as a tuple but I've been trying to get the integer one from this and simply failing. I need to be able to compare that integer value. My question is simply how do you acquire the first part of a tuple in an a list as everything I have done and searched keeps failing. Apologies if this is really simple, I am new to haskell. This is my function code, obviously with a bunch of errors
goesP :: Domino->Board->End-> Bool
goesP _ []_ = True
goesP dom bor L = (if head bor fst == fst dom then True else if last bor == snd then True else False)
Upvotes: 0
Views: 845
Reputation: 601
From your question, it doesn't look like you're interested in a generalised function, so these will work:
fst $ head b
will get the very first Int
in that list, and snd $ last b
will get the very last.
How you compare them then is up to you.
Upvotes: 0
Reputation: 21
Something as simple as
goesP :: Domino -> Board -> End -> Bool
goesP _ [] _ = True
goesP _ ((a,b):doms) _ = a
will work, as you can pattern match for the list being empty, and then being a pair cons the rest of a list, and extract the first element out of that pair.
I'm not sure what you're trying to do with the End type in there as well, but I left it in there in my example (although I do nothing with it).
Upvotes: 2
Reputation: 1843
As you may or may not know fst and snd only work for 2-element tuples so for you it should be enough:
fst (a,b) = a
You can also write you own:
get3th (_,_,a,_,_,_,_,_,_,_) = a
As you can see you may want to define your own type.
Upvotes: 0