[Haskell-cafe] let vs. where
jeff p
mutjida at gmail.com
Fri Nov 16 00:08:55 EST 2007
Hello,
> <quote>
> taxRate = 0.06
>
> total cart = subtotal + tax
> where
> subtotal = sum cart
> taxable = filter isTaxable cart
> tax = (sum taxable) * taxRate
>
> This example defines two functions, taxRate, which returns a constant
> value, and total, which computes the total cost of the list of items
> in a shopping cart. (Although the taxRate definition appears to be
> defining a variable, it's best to think of it as a constant function,
> a function that takes no parameters and always returns the same
> value.) The definition of total is quite expressive, and highlights
> the intent of the function, by isolating and naming important
> sub-expressions in the computation. (total also refers to an isTaxable
> function, not presented here.)
> </quote>
>
This explanation is just wrong.
A function is an expression whose type is an arrow; e.g. Int -> Int.
The type of taxRate is (Fractional t) => t. There is some leeway for
taxRate to be a function if someone provided a Fractional instance for
a function type; but that seems to be beyond the scope of the quoted
text which comes from an introductory explanation.
Furthermore, a constant function is a function which ignores its
argument; e.g. \x -> 0.06
-Jeff
More information about the Haskell-Cafe
mailing list