簡體   English   中英

給定函數g :: a - > b - > ... - > z,我怎樣才能像h ::(a,b,...) - > z一樣使用它?

[英]Given a function g :: a -> b -> … -> z, how can I use it like h :: (a, b, …) -> z?

背景:我正在使用wxHaskell的fileOpenDialog,它有6個非顯而易見的參數(curried)。 我的代碼目前是:

maybePath <- fileOpenDialog w useLastSelectedDir canSelectReadOnly
                frameName possibleFiles initialDir defaultFilename

用上面的let語句來定義我的所有參數。 我喜歡做的是在某處保存我的參數。 我有點理解為什么Haskell不會支持像:

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

但是,在不重復自己的精神下,是否有接近這一點的東西?

看起來您自然希望此函數的輸入是參數記錄:

{-# 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 

還有(不太干凈)編寫具有更多參數的不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 fileOpenDialog將使fileOpenDialog接受一個6元組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM