I'm used to being able to define optional arguments like so in Python:
def product(a, b=2):
return a * b
Haskell doesn't have default arguments, but I was able to get something similar by using a Maybe:
product a (Just b) = a * b
product a Nothing = a * 2
This becomes cumbersome very quickly if you have more than multiple parameters though. For example, what if I want to do something like this:
def multiProduct (a, b=10, c=20, d=30):
return a * b * c * d
I would have to have eight definitions of multiProduct to account for all cases.
Instead, I decided to go with this:
multiProduct req1 opt1 opt2 opt3 = req1 * opt1' * opt2' * opt3'
where opt1' = if isJust opt1 then (fromJust opt1) else 10
where opt2' = if isJust opt2 then (fromJust opt2) else 20
where opt3' = if isJust opt3 then (fromJust opt3) else 30
That looks very inelegant to me. Is there an idiomatic way to do this in Haskell that is cleaner?