我习惯于在Python中定义可选参数:
def product(a, b=2):
return a * b
Haskell没有默认参数,但我可以通过使用Maybe:
获得类似的东西product a (Just b) = a * b
product a Nothing = a * 2
如果您有多个参数,这会很快变得麻烦。例如,如果我想做这样的事情该怎么办:
def multiProduct (a, b=10, c=20, d=30):
return a * b * c * d
我必须有八个multiProduct定义来解释所有情况。
相反,我决定采用这个:
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
对我来说,这看起来非常不优雅。在Haskell中有一种惯用的方法可以做得更干净吗?
答案 0 :(得分:72)
也许一些不错的符号在眼睛上会更容易:
(//) :: 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)
如果您需要多次使用这些参数,我建议使用@ pat的方法。
6年后编辑
使用ViewPatterns
,您可以将默认设置放在左侧。
{-# 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
答案 1 :(得分:33)
这是在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 -}
然后
{-# 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"
更新:哎呀,我被接受了。老实说,我认为luqui's answer是最好的一个:
opt2 replicate 3 'f'
看看我的意思)答案 2 :(得分:14)
我不知道解决潜在问题的更好方法,但您的示例可以更简洁地写成:
multiProduct req1 opt1 opt2 opt3 = req1 * opt1' * opt2' * opt3'
where opt1' = fromMaybe 10 opt1
opt2' = fromMaybe 20 opt2
opt3' = fromMaybe 30 opt3
答案 3 :(得分:14)
答案 4 :(得分:7)
当参数过于复杂时,一种解决方案是仅为参数创建数据类型。然后,您可以为该类型创建默认构造函数,并仅在函数调用中填写要替换的内容。
示例:
$ 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
}
答案 5 :(得分:1)
这是使隐式参数看起来像可选参数的一种方式:
{-# LANGUAGE Rank2Types, ImplicitParams #-}
multiProduct :: (Num x) => x -> ((?b::x) => x) -> ((?c::x) => x) -> ((?d::x) => x) -> x
multiProduct a b c d = let ?b=10 ; ?c=20 ; ?d=30
in a * b * c * d
test1 = multiProduct 1 ?b ?c ?d -- 6000
test2 = multiProduct 2 3 4 5 -- 120
答案 6 :(得分:0)
mcandre和Ionuț提到的记录方法的可能改进/修改是使用镜片:
{-# LANGUAGE -XTemplateHaskell #-}
data Dog = Dog {
_name :: String,
_breed :: String,
_barks :: Int
}
makeLenses ''Dog
defaultDog :: Dog
defaultDog = Dog {
_name = "Dog",
_breed = "Beagle",
_barks = 2
}
bark :: (Dog -> Dog) -> IO ()
bark modDog = do
let dog = modDog defaultDog
replicateM_ (barks dog) $ putStrLn $
(name dog) ++ " (" ++ (breed dog) ++ "): Ruff!"
main :: IO ()
main = do
bark $ (name .~ "Snoopy") . (barks .~ 2)
bark $ (name .~ "Wishbone") . (breed .~ "Terrier") . (barks .~ 3)
或者
bark :: Dog -> IO ()
bark dog = do
replicateM_ (barks dog) $ putStrLn $
(name dog) ++ " (" ++ (breed dog) ++ "): Ruff!"
main :: IO ()
main = do
bark $ name .~ "Snoopy" $ barks .~ 2 $ defaultDog
bark $ name .~ "Wishbone" $ breed .~ "Terrier" $ barks .~ 3 $ defaultDog
有关(.~)
的含义,请参见here。