-4

I have a problem in this code:

module Blueprint where

data Colour = Blue | Green | Red 
  deriving ( Eq, Show )

data Car = Car { wheels :: Integer
               , colour :: Colour
               }
  deriving ( Eq, Show )

data Property = Colour_Is Colour
              | Wheels Ordering Integer
              | And Property Property
              | Not Property
              | Or Property Property 
  deriving Show 

check :: Property -> Car -> Bool
check prop car = case prop of
    Colour_Is col -> col == colour car
    Wheels ord num -> case ord of 
                            LT -> num > wheels car
                            EQ -> num == wheels car
    And l r -> check l car && check r car
    Not p -> not check p car
    Or x y -> check x car || check y car 


cars = [ Car { wheels = 4, colour = Red }
        , Car { wheels = 2, colour = Blue }
        , Car { wheels = 14, colour = Green }
        , Car { wheels = 4, colour = Green }
        , Car { wheels = 2, colour = Red }
        ]

prop1 :: Property
prop1 =  And (Wheels EQ 14) (Colour_Is Green)

test :: Bool
test = and
    [ check ( Wheels EQ 4 ) ( cars !! 0 )
    , check ( Wheels LT 3 ) ( cars !! 1 )
    , check ( And ( Wheels EQ 14 ) ( Colour_Is Green )) ( cars !! 2 )
    , check ( Not ( Colour_Is Red ) ) ( cars !! 3 )
    , filter ( check prop1 ) cars == take 3 cars
    ]

I built the check function and implemented Colour_Is,And,Not and Wheels correctly, but when I add the Or function:

( Or x y -> check x car || check y car )

I'm getting this error from ghci :

D:\My_data\hs\strategy.hs:26:16: parse error on input `->' Failed, modules loaded: none.

I'm new to Haskell. Where is my mistake?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Smokeking
  • 17
  • 1
  • 2
  • 2
    You should format the code using the website's code block, and probably try to narrow the code down to a minimal version that has the problem. – Michael Easter May 22 '11 at 00:32
  • Your formatting in the question is all messed up, which makes it very difficult to help. Please fix it, and make sure the indentation is the same as your actual code. – C. A. McCann May 22 '11 at 00:33
  • 1
    Yeah i know the error must be there, but didnt find the mistake. i implemented it like the And-function so i thought it ant be wrong ?! o.O – Smokeking May 22 '11 at 00:43
  • When I feed GHCi this code, it chokes on line 25 with: `Couldn't match expected type 'Bool' against inferred type 'Property -> Car -> Bool'`. – Skurmedel May 22 '11 at 00:44
  • 1
    its very difficult o.O , i always get strange errors sometimes. My task is to write this package and upload it to a webform where it will be automatically corrected. but the webform (the online ghc compiler ) often write some errors i doent get local. Is there a special , better way to write haskell code as in notepad++ ? – Smokeking May 22 '11 at 01:11

1 Answers1

3

Your parse error is probably due to incorrect indentation, but that error doesn't exist with Dhaivat's reformatting of your question.

Further, you needed parens around the argument for not:

  Not p -> not check p car
-->
  Not p -> not (check p car)

In addition to properly formatting your question, next time please pick a sensible title. The title of "Haskell doesn't work" is a) wrong b) uninformative c) not conductive to getting an answer from people who like Haskell. It's still a small enough community that may people with a personal investment in the Language (people who've worked on the language spec and core libraries) are actually part of the language's social scene.

Thomas M. DuBuisson
  • 64,245
  • 7
  • 109
  • 166
  • 1
    OMG how does it work?! i just copy and pasted the formatted version of my code, ( but it was the same o.O ) and it works?! how? o.O Im very sorry of posting such shit ;-) i will be doing better next time. and yeah haskell is very difficult for someone who didn't get a tutorial , but im doing my best on it. And beacause of my not existing knowledge i hadn't no idea how to name this problem :D , im sorry – Smokeking May 22 '11 at 00:52
  • Is there a special way to edit haskell code? which type of indent is better or correct? with tab's or real spaces? – Smokeking May 22 '11 at 01:01
  • 1
    @Smokeking: Use spaces, always. Tabs are allowed, but the way they're interpreted is a bit tricky and prone to causing invisible bugs depending on your choice of editor. – C. A. McCann May 22 '11 at 01:05
  • thank you very much,.. so last question is : when i have to parens statements and when not?! – Smokeking May 22 '11 at 01:18
  • 1
    Smokeking: Function application is greedy. So `f a b c` will parse as `f (a) (b) (c)` and not `f (a b c)` or `f a (b c)`. In your code you wanted to pass the result of `check p car` to `not`, hence the need for parens. – Thomas M. DuBuisson May 22 '11 at 01:32
  • @smokeking Also, there isn't anything tricky about tabs. Haskell defines tabs the *correct way*, 1 tab = 8 spaces. If your editor displays tabs differently it's going to confuse you. – Thomas M. DuBuisson May 22 '11 at 06:28