Page 4 of 5 FirstFirst ... 2345 LastLast
Results 31 to 40 of 44

Thread: Haskell Help

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

    Default

    What do you mean to do? x + 1 is not equal to x.
    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!

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

    Default

    Use precedence and associativity to reduce the number of parentheses in the string produced by toString, e.g.
    toString (Mult (Plus (Var "x") (Const 1)) (Var "y"))
    = "(x + 1) * y"
    toString (Plus (Plus (Var "x") (Const 1)) (Var "y"))
    = "x + 1 + y"
    toString (Plus (Var "x") (Plus (Const 1) (Var "y")))
    = "x + (1 + y)"
    toString (Plus (Var "x") (Mult (Const 1) (Var "y")))
    = "x + 1 * y"

    i am struggling with this? the code provided does not simplify all the above? please help thanks

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

    Default

    I jazzed up my simplification code. Perhaps it's just me (as I've never done it before), but I highly suspect that there is a much better way of coding simplifications.
    Code:
    -- code removed by request

    Examples
    Code:
    *Expr> let [x, y, z] = map (Var . return) "xyz"
    *Expr>
    *Expr> let test  = toString . simplify
    *Expr>
    *Expr> test $ -(-x) + x + (99 + y) * x
    "(2x + (x * (99 + y)))"
    *Expr>
    *Expr> test $ -1 - x + 22 * x * (y + y + y) * (2 * z) + 9
    "(8 - x + (132 * x * y * z))"
    *Expr>
    *Expr> test $ x * y * 9 * x * 0 * x
    "0"
    *Expr>
    *Expr> test $ x - x
    "0"
    Last edited by Twey; 12-01-2008 at 06:17 AM. Reason: Remove code — sorry Trin, I saved it if you want it back.
    Trinithis

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

    Default

    Ahh, lists! I should have thought of that.

    I'm still uncertain whether doing actual calculation is against the rules or not. I suspect it might be, but then perhaps 'evaluation' only means the variable lookup and interpretation. That does seem to be the focus of the eval function.

    Note to you non-GHC folks:
    Code:
    import Data.Function (on)
    import Data.Maybe (fromMaybe)
    import Data.List (sort, partition, intercalate)
    should be
    Code:
    import Maybe (fromMaybe)
    import List (sort, partition, intercalate)
    
    (g `on` f) x y = f x `g` f y
    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. #35
    Join Date
    Oct 2008
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey View Post
    toString :: Expr -> Expr — not too tricky — half the exercise is done for you already. The only thing to watch out for is making sure that no brackets appear at the top level. To this end, we'll actually define two functions: toString, which performs the conversion of complex operations without brackets, and toString', which performs everything else.
    Code:
    -- code removed by request

    could you add to the above so when i put the below expressions in i get the results shown?

    need to keep this simple. thanks

    Use precedence and associativity to reduce the number of parentheses in the string produced by toString, e.g.

    toString (Mult (Plus (Var "x") (Const 1)) (Var "y"))
    = "(x + 1) * y"
    toString (Plus (Plus (Var "x") (Const 1)) (Var "y"))
    = "x + 1 + y"
    toString (Plus (Var "x") (Plus (Const 1) (Var "y")))
    = "x + (1 + y)"
    toString (Plus (Var "x") (Mult (Const 1) (Var "y")))
    = "x + 1 * y"
    Last edited by Twey; 12-01-2008 at 06:15 AM.

  6. #36
    Join Date
    Nov 2008
    Location
    UK
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Sorry for not replying, been pretty hectic this week. Ignore my last post too, got confused on something.

    Ok I think I am done for my simplify and toString operations ( !!!), and I am going to leave them for the time being.

    I am now working on my eval operation, however in your example Twey, what does the env do? Was slightly confused on that.

    Quote Originally Posted by Twey View Post
    eval :: [(Variable, Integer)][/icode] — this one's a little bit trickier, since it involves looking something up as well. Luckily, there's a built-in function for this: lookup :: (Eq a) => a -> [(a, b)] -> Maybe b. There's even a built-in to handle the conversion from Maybe Integer to Integer for us: it's called fromMaybe, and it's in the package Maybe, whence we will import it (beware: imports must go at the top of the module, just underneath the module declaration). Apart from the Var case, this is all straightforward too:
    Code:
    -- code removed by request
    Thanks m8
    Last edited by Twey; 12-01-2008 at 06:15 AM.

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

    Default

    fromMaybe :: a -> Maybe a -> a takes two parameters, a value to provide in the Nothing case and a Maybe, and returns either the value of the Just or the provided value. That is to say:
    Code:
    fromMaybe _ (Just x) = x
    fromMaybe x Nothing  = x
    This is handy, because lookup :: a -> [(a, b)] -> Maybe b returns a Maybe b, with Nothing representing the case where no appropriate pair can be found; that is to say,
    Code:
    lookup _ []            = Nothing
    lookup x ((a, b) : xs)
        | x == a           = Just b
        | otherwise        = lookup x xs
    So, we pair these two up (eval env (Var x) = fromMaybe 0 (lookup x env)) and get a function that looks up the variable in the environment and, if it doesn't exist, returns 0.
    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. #38
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Nicely found. Yes, I will remove the complete code (but leave the suggestions and explanations).

    I wonder if you would enlighten me as to the original source of this exercise? I must say, I've amassed a fair amount of curiosity about it by now.
    Last edited by Twey; 12-01-2008 at 06:24 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!

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

    Default

    If only my school (Berkeley) taught functional programming with Haskell instead of Scheme, I would be soo happy.

    As a side note, I think I have a better idea for simplification. Instead of transforming to a list, transform it to a graph, where edges represent either addition or multiplication between entities. I'm going to try it out when I have time.
    Trinithis

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

    Default

    Well, Scheme's simple syntax does make a lot of sense for a teaching language. There's much less to get in the way. Think yourself lucky you get taught functional programming at all It's disturbingly rare.
    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
  •