Page 1 of 5 123 ... LastLast
Results 1 to 10 of 44

Thread: Haskell Help

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

    Default Haskell Help

    Hi people, i have some haskell problems with expressions. What i am trying to do is add two expressions together and simplify the output. I have this code

    Code:
    module X where
    
    type Variable = String
    
    data X = Const Integer
    	| Var Variable
    	| Plus X X
    	deriving (Eq, Show)
    What i am trying to achieve is when i write for example Plus (Var "x")(Const 0) in Hugs, to get "Var x". The 0 is disregarded as it adds nothing to x (0 + x = x). If the Const is 1 again the output should be "Var x" as 1 + x = x.

    Any help is appreciated

    Thnx
    Last edited by scarface; 11-19-2008 at 04:19 PM. Reason: mistake in code

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

    Default

    What? Since when does 1 + x = x? I think the universe would explode or something if that were true.
    Code:
    -- code removed by request
    Last edited by Twey; 12-01-2008 at 06:21 AM.
    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!

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

    Default

    thnx for the code i will try it later when i come back home..actually i have done a mistake..not 1+x but rather 1*x = x..i apologize for the confusion ..

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

    Default

    But you have no multiplication operator. You'd have to add one:
    Code:
    -- code removed by request
    Last edited by Twey; 12-01-2008 at 06:20 AM.
    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. #5
    Join Date
    Oct 2008
    Posts
    20
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default

    yes i was just giving example with the multiplication to make it clearer..also is there a way to add the 2 Expr together in case they are both Const? example : if we have Plus(Const 2)(Const 5) = 7. What i did was i wrote a separate function

    [/CODE]add :: Expr -> Integer
    add (Plus (Const x) (Const y)) = x + y[/CODE]

    and it works but i was wondering if i can have this in the simplify function? It gives me an error to do with the Type. I have defined simplify to be Expr -> Expr but with the adding i want to get an integer back and thats where the error comes.
    Last edited by scarface; 11-20-2008 at 05:01 PM. Reason: mistake

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

    Default

    What we were doing before was merely normalisation; now, you're talking about evaluation. It will work something like this (rather dumb version, I'm afraid):
    Code:
    -- code removed by request
    The error with your original was that you were trying to return an Integer, but the function was meant to return an Expr.
    Last edited by Twey; 12-01-2008 at 06:20 AM.
    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!

  7. #7
    Join Date
    Oct 2008
    Posts
    20
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default

    Thanks for the code but i think i will just stick to my function. I am pretty bad at haskell and i dont completely understand the code you gave so i rather not use it (in case i get asked questions on it). I will try and do it the way i can and understand it and just keep it as simple as possible even if my program is not going to behave as it is expected.

    I have another question concerning converting an expression to string. I had a look on the web and "Show" function is mentioned.I tried it with simple input but i couldnt convert it to string. Anyone could point me to something more useful?

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

    Default

    You're probably confused by the on function. on is (roughly) defined thusly:
    Code:
    on :: (b -> b -> b) -> (a -> b)
    on g f = \x y -> f x `g` f y
    So,
    Code:
    liftConst = Const . on (fromConst . evaluate)
    is the same as (adding in all the points)
    Code:
    liftConst op a b = Const $ op (fromConst $ evaluate a) (fromConst $ evaluate b)
    It's just a little more elegant.
    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!

  9. #9
    Join Date
    Oct 2008
    Posts
    20
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default

    do u know why it doesnt let me load the data.function module?
    I typed it exactly as posted and it gave me error saying i cant find the module.

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

    Default

    What compiler and version are you using?
    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!

Tags for this Thread

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
  •