Results 1 to 7 of 7

Thread: Haskel help

  1. #1
    Join Date
    Oct 2008
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Haskel help

    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 , I can't formulate it in Haskell.

    If anyone can solve my problem, please help me

    Thank you in advance.

  2. #2
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Types (and data constructors) have to be capitalized. Also, there were some other proplems which I fixed.
    Code:
    type Domino = (Int, Int)
    
    domino :: Int -> Int -> Domino
    domino x y = (x, y)
    -- or: domino = (,)
    
    type Hand = [Domino]
    type Board = [Domino]
    Trinithis

  3. The Following User Says Thank You to Trinithis For This Useful Post:

    no_mad (10-25-2008)

  4. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    It would probably be better to define:
    Code:
    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:
    Code:
    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):
    Code:
    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
    Last edited by Twey; 10-26-2008 at 12:08 AM. Reason: Added better implementation.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. The Following User Says Thank You to Twey For This Useful Post:

    no_mad (10-25-2008)

  6. #4
    Join Date
    Oct 2008
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey View Post
    :
    Code:
    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?

  7. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #6
    Join Date
    Oct 2008
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey View Post
    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

  9. #7
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •