Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better way to have optional function arguments in Haskell?

Tags:

haskell

People also ask

How do you make an argument in a function optional?

You can assign an optional argument using the assignment operator in a function definition or using the Python **kwargs statement. There are two types of arguments a Python function can accept: positional and optional. Optional arguments are values that do not need to be specified for a function to be called.

How many arguments does a function have in Haskell?

Every function in Haskell officially only takes one parameter.

How do you pass arguments in Haskell?

You have to pass file1 etc. to runQuery like every other function argument: main = do (file1:file2:file3:_) <- getArgs checkdata command <- getLine runQuery file1 file2 file3 (words command) runQuery file1 file2 file3 ("queryname":parameter1:parameter2) = do ... Well that was simple.

Can we have default arguments in functions if so how?

In C++ programming, we can provide default values for function parameters. If a function with default arguments is called without passing arguments, then the default parameters are used. However, if arguments are passed while calling the function, the default arguments are ignored.


Perhaps some nice notation would be easier on the eyes:

(//) :: Maybe a -> a -> a
Just x  // _ = x
Nothing // y = y
-- basically fromMaybe, just want to be transparent

multiProduct req1 opt1 opt2 opt3 = req1 * (opt1 // 10) * (opt2 // 20) * (opt3 // 30)

If you need to use the parameters more than once, I suggest going with @pat's method.

EDIT 6 years later

With ViewPatterns you can put the defaults on the left.

{-# LANGUAGE ViewPatterns #-}

import Data.Maybe (fromMaybe)

def :: a -> Maybe a -> a
def = fromMaybe

multiProduct :: Int -> Maybe Int -> Maybe Int -> Maybe Int -> Int
multiProduct req1 (def 10 -> opt1) (def 20 -> opt2) (def 30 -> opt3)
  = req1 * opt1 * opt2 * opt3

Here's yet another way to do optional arguments in Haskell:

{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, FlexibleContexts #-}
module Optional where

class Optional1 a b r where 
  opt1 :: (a -> b) -> a -> r

instance Optional1 a b b where
  opt1 = id

instance Optional1 a b (a -> b) where
  opt1 = const

class Optional2 a b c r where 
  opt2 :: (a -> b -> c) -> a -> b -> r

instance Optional2 a b c c where
  opt2 = id

instance (Optional1 b c r) => Optional2 a b c (a -> r) where
  opt2 f _ b = \a -> opt1 (f a) b

{- Optional3, Optional4, etc defined similarly -}

Then

{-# LANGUAGE FlexibleContexts #-}
module Main where
import Optional

foo :: (Optional2 Int Char String r) => r
foo = opt2 replicate 3 'f'

_5 :: Int
_5 = 5

main = do
  putStrLn $ foo        -- prints "fff"
  putStrLn $ foo _5     -- prints "fffff"
  putStrLn $ foo _5 'y' -- prints "yyyyy"

Update: Whoops, I got accepted. I honestly think that luqui's answer is the best one here:

  • the type is clear, and easy to read, even for beginners
  • same for type errors
  • GHC doesn't need hints to do type inference with it (try opt2 replicate 3 'f' in ghci to see what I mean)
  • the optional arguments are order-independent

I don't know of a better way to solve the underlying problem, but your example can be written more succinctly as:

multiProduct req1 opt1 opt2 opt3 = req1 * opt1' * opt2' * opt3'
    where opt1' = fromMaybe 10 opt1
          opt2' = fromMaybe 20 opt2
          opt3' = fromMaybe 30 opt3

When arguments get too complex, one solution is to create a data type just for the arguments. Then you can create a default constructor for that type, and fill in only what you want to replace in your function calls.

Example:

$ runhaskell dog.hs 
Snoopy (Beagle): Ruff!
Snoopy (Beagle): Ruff!
Wishbone (Terrier): Ruff!
Wishbone (Terrier): Ruff!
Wishbone (Terrier): Ruff!

dog.hs:

#!/usr/bin/env runhaskell

import Control.Monad (replicateM_)

data Dog = Dog {
        name :: String,
        breed :: String,
        barks :: Int
    }

defaultDog :: Dog
defaultDog = Dog {
        name = "Dog",
        breed = "Beagle",
        barks = 2
    }

bark :: Dog -> IO ()
bark dog = replicateM_ (barks dog) $ putStrLn $ (name dog) ++ " (" ++ (breed dog) ++ "): Ruff!"

main :: IO ()
main = do
    bark $ defaultDog {
            name = "Snoopy",
            barks = 2
        }

    bark $ defaultDog {
            name = "Wishbone",
            breed = "Terrier",
            barks = 3
        }