简体   繁体   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?

Background: I'm working with wxHaskell's fileOpenDialog, which takes 6 non-obvious parameters (curried). 背景:我正在使用wxHaskell的fileOpenDialog,它有6个非显而易见的参数(curried)。 My code is currently: 我的代码目前是:

maybePath <- fileOpenDialog w useLastSelectedDir canSelectReadOnly
                frameName possibleFiles initialDir defaultFilename

with a let statement above that to define all my parameters. 用上面的let语句来定义我的所有参数。 What I would love to do though, is save my parameters somewhere. 我喜欢做的是在某处保存我的参数。 I somewhat understand why Haskell would't support something like say: 我有点理解为什么Haskell不会支持像:

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

However, is there something close to this in the spirit of not repeating myself? 但是,在不重复自己的精神下,是否有接近这一点的东西?

It seems like you would naturally like the input to this function to be a record of parameters: 看起来您自然希望此函数的输入是参数记录:

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

Now, you can use any of the following equivalent syntaxes (some use RecordWildCards): 现在,您可以使用以下任何等效语法(一些使用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 

There is also the (less clean) possibility of writing an uncurry variant (see here ) having more arguments: 还有(不太干净)编写具有更多参数的不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 

Having that, uncurry6 fileOpenDialog will make fileOpenDialog accept a 6-tuple. 有了这个, uncurry6 fileOpenDialog将使fileOpenDialog接受一个6元组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎样才能证明elem z(xs ++ ys)== elem z xs || 你好吗? - How can I prove that elem z (xs ++ ys) == elem z xs || elem z ys? f, g, h :: Kleisli ((-&gt;) e) ab &lt;=&gt; f &gt;&gt;&gt; (g &amp;&amp;&amp; h) = (f &gt;&gt;&gt; g) &amp;&amp;&amp; (f &gt;&gt;&gt; h)? - f, g, h :: Kleisli ((->) e) a b <=> f >>> (g &&& h) = (f >>> g) &&& (f >>> h)? 如何写一个类型为`[(a,b)] - &gt;([a] - &gt; [a]) - &gt; [(a,b)]的函数 - How to write a function of type `[(a,b)] -> ([a] -> [a]) -> [(a,b)]` 如何使用这样的类型:在haskell中键入a = b - &gt; c - How to use types like this: type a = b -> c in haskell 我怎样才能向GHC证明(b~Foo)? - How can I prove to GHC that (b ~ Foo)? 如何编写a-&gt; b - &gt; b - &gt; b类型的函数来折叠树 - How to write a function of type a-> b -> b -> b for folding a tree 有没有像`map2 ::(i - &gt; a) - &gt;(i - &gt; b) - &gt; [i] - &gt; [(a,b)]`? - Is there something like `map2 :: (i -> a) -> (i -> b) -> [i] -> [(a,b)]`? 如何仅使用A到Z和0到9来使用ORD和CHR? - How do I use ORD and CHR with only A to Z and 0 to 9? 此函数是否有任何用例:foo ::(b-&gt; c)-&gt;(a-&gt; b)-&gt;(a-&gt; c) - Are there any use cases for this function : foo :: (b -> c) -> (a -> b) -> (a -> c) Haskell函数来自(a - &gt; [b]) - &gt; [a - &gt; b] - Haskell function from (a -> [b]) -> [a -> b]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM