LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Haskell: Integral Parameter (https://www.linuxquestions.org/questions/programming-9/haskell-integral-parameter-878912/)

hydraMax 05-05-2011 12:14 AM

Haskell: Integral Parameter
 
Noob Haskell question: Let's say that I have a function that calculates the cost of booking airline tickets according to some market-driven formula:

Code:

-- total cost for n numbers of persons traveling together
cost 0 = 0
cost 1 = 1000
cost n = if n < 0 then error "cost of negative travelers invalid"
        else if n >= 10 then 700 * n
              else 1000 * n - 500

Now, the only thing that seems lacking here is that I know that in /all/ cases, n is going to be a whole number. But how to I specify that?

After some experimentation I discovered that if I passed in a special option to the compiler I could do this:

Code:

cost (n :: Integer) = if n < 0 then error "cost of negative travelers invalid"
        else if n >= 10 then 700 * n
              else 1000 * n - 500

However, I actually want it to be any whole number type (i.e., Int or Integer) so this is not satisfactory.


All times are GMT -5. The time now is 01:58 AM.