1

背景:我正在使用 wxHaskell 的 fileOpenDialog,它需要 6 个非显而易见的参数(curried)。我的代码目前是:

maybePath <- fileOpenDialog w useLastSelectedDir canSelectReadOnly
                frameName possibleFiles initialDir defaultFilename

上面有一个let声明来定义我的所有参数。不过,我想做的是将我的参数保存在某个地方。我有点理解为什么 Haskell 不支持这样的说法:

myParams = ( ... ) -- tuple of params
maybePath <- fileOpenDialog myParams

但是,本着不重蹈覆辙的精神,有没有与此相近的东西?

4

2 回答 2

2

似乎您自然希望此函数的输入成为参数记录:

{-# LANGUAGE RecordWildCards #-} 

-- Defined by your library 
foo :: String -> Int -> IO () 
foo = ... 

data Opts = Opts { optString :: String, optInt :: Int } 
bar :: Opts -> IO () 
bar Opts{..} = foo optString optInt 

现在,您可以使用以下任何等效语法(有些使用 RecordWildCards):

main = do 
  let optString = <...>
      optInt    = <...>
  bar Opts{..} 

main = do 
  let x = <...>
      y = <...>
      myParams = Opts x y 
  bar myParams 

main = do 
  bar $ Opts 
    { optString = <...> 
    , optInt    = <...> 
    } 

main = do 
  let optString = <...>
      optInt    = <...>
      myParams  = Opts{..} 
  bar myParams 
于 2015-12-05T03:52:28.250 回答
1

还有(不太干净)编写具有更多参数的uncurry变体(参见此处)的可能性:

uncurry6 :: (a -> b -> c -> d -> e -> f -> g) -> ((a,b,c,d,e,f) -> g)
uncurry6 fun (a,b,c,d,e,f) = fun a b c d e f 

有了这个,uncurry6 fileOpenDialogfileOpenDialog接受一个 6 元组。

于 2015-12-05T20:01:49.527 回答