Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Haskell: Defining function types..

Options
  • 08-10-2009 8:20pm
    #1
    Registered Users Posts: 2,234 ✭✭✭


    Hi There,

    I'm pretty new to Haskell atm. I'm struggling with most things but one thing I can't get my head around is defining function types. I can define a function like so: sum a b = a+b and it works fine.

    Why must we define/declare/type [??] the function beforehand like so:
    sum :: Integer->Integer->Integer

    This above example works with or without the type declaration but I am having trouble with making a function that takes in integers and returns a float.
    How do I get the following to return a float?
    sum :: Integer->Integer->???
    sum a b = 2.0*(a+b)
    

    If I change ??? to Integer I get the error "Instance of Fractional Integer required for definition of sum"

    If I change it to Float I get the error "Type error in explicitly typed binding"

    Any ideas?Thanks..!


Comments

  • Registered Users Posts: 1,916 ✭✭✭ronivek


    sum :: (Fractional a) => a -> a -> a

    That should work fine; it just needed to be bumped up a level of abstraction basically. Now it will work for all numbers that can be represented fractionally.


  • Registered Users Posts: 2,234 ✭✭✭techguy


    Cool, thanks i'll give that a go


Advertisement