Log in

View Full Version : Haskel help



no_mad
10-25-2008, 04:44 PM
Hello, I have a problem with haskell. I need to represent and process dominoes in Haskell. Firstly, I need to create datatypes for representing a domino, a hand and a board.. I've done it like so:

type domino = (Int,Int)
domino :: Int->Int->(Int, Int)
domino x y :: (Int, Int)

type hand = [domino]
type board = [domino]

But when I compile it it gives error such as '::' unexpected. :(

Then I have to implement two functions gLeft and gRight, where predicates return True if a given domino can be played at the left end or at the right end respectively. This is where I am stacked :confused: , I can't formulate it in Haskell.

If anyone can solve my problem, please help me

Thank you in advance. :)

Trinithis
10-25-2008, 05:54 PM
Types (and data constructors) have to be capitalized. Also, there were some other proplems which I fixed.

type Domino = (Int, Int)

domino :: Int -> Int -> Domino
domino x y = (x, y)
-- or: domino = (,)

type Hand = [Domino]
type Board = [Domino]

Twey
10-25-2008, 10:08 PM
It would probably be better to define:
data Domino = Domino { left :: Int, right :: Int }
type Hand = [Domino]
type Board = [Domino]... rather than using a type synonym. Additionally, since dominoes can be added to either end of the Board and selected from any point in the Hand, a list probably isn't the ideal datatype — I would go for a sequence for the Board, and a bag for the Hand.

However, proceeding with the lists for the moment:
gLeft, gRight :: Domino -> Board -> Bool
gLeft _ [] = True
gLeft (Domino a b) ((Domino lm _) : _) = lm == a || lm == b
gRight d b = gLeft d (reverse b)Here's a better implementation, using Data.MultiSet and Data.Sequence (the latter distributed with GHC, the former available on Hackage):
import qualified Data.MultiSet as Bag
import Data.Sequence (Seq (EmptyL), viewl, ViewL((:<)), viewr, ViewR((:>)))

data Domino = Domino { left :: Int, right :: Int }
type Hand = Bag.MultiSet Domino
type Board = Seq Domino

gLeft, gRight :: Domino -> Board -> Bool
gLeft _ EmptyL = True
gLeft (Domino l r) b = let ((Domino bl _) :< _) = viewl b in bl == l || bl == r
gRight _ EmptyL = True
gRight (Domino l r) b = let (_ :> (Domino _ br)) = viewr b in br == l || br == r

no_mad
10-25-2008, 10:50 PM
:
rotate (Domino a b) = Domino b a

gLeft, gRight :: Domino -> Board -> Bool
gLeft _ [] = True
gLeft (Domino a b) ((Domino lm _) : _) = lm == a || lm == b
gRight d b = gLeft d (reverse b)

Hi! Thank you!
But I am not quite sure that I understood what the last two lines execute. Why you are using rotate(Domino a b) = Domino b a?
Are rotate and reverse functions in Haskell?

Twey
10-26-2008, 12:09 AM
You're right, sorry, rotate is unnecessary. I was going to use it elsewhere, but changed my mind. :)

reverse :: [a] -> [a] is a built-in.

no_mad
10-26-2008, 03:44 PM
gLeft (Domino a b) ((Domino lm _) : _) = lm == a || lm == b
gRight d b = gLeft d (reverse b)

Sorry for disturbing you again, but I don't have anyone to ask
Could you explain me how above code is executing, cause I can't get it working

joebazooka
07-21-2011, 12:10 PM
Hi guys, Im really sorry to be bumping a 2 year old thread but I have good reason in doing so.

I actually am doing the exact same project that the thread starter is doing so I have a few questions about it and was wondering if help would still be available in this thread ?

Thanks in advance