以下 SML 代码取自华盛顿大学课程的家庭作业。(具体来说,它是所提供代码的一部分,以便学生可以使用它来完成课程网页上列出的作业 3上列出的作业 3。 )我不是在这里寻求作业帮助\xe2\x80\x93我想我理解代码的含义。我不太明白的是如何允许柯里化函数根据其自己的部分应用程序来定义。
\n datatype pattern = \n WildcardP\n | VariableP of string\n | UnitP\n | ConstantP of int\n | ConstructorP of string * pattern\n | TupleP of pattern list\n\nfun g f1 f2 p =\n let \n val r = g f1 f2 (* Why does this not cause an infinite loop? *)\n in\n case p of\n WildcardP => f1 ()\n | VariableP x => f2 x\n | ConstructorP(_,p) => r p\n | …Run Code Online (Sandbox Code Playgroud) 所以像
addList :: [int] -> int
addList = foldl1 (+)
Run Code Online (Sandbox Code Playgroud)
为什么这样做?Currying部分.为什么没有变量?
换句话说,有没有一个很好的理由为什么不应该编译?
def f(xs: List[Int]) = xs.foldLeft(0) _ // OK
def f(xs: List[Int]) = (xs :\ 0) _ // OK
def f(xs: List[Int]) = (0 /: xs) _
<console>:15: error: missing arguments for method /: in trait TraversableOnce;
follow this method with `_' if you want to treat it as a partially applied function
Run Code Online (Sandbox Code Playgroud)
以下是一些解决方法:
def f(xs: List[Int]) = xs./:(0) _
def f(xs: List[Int]): ((Int, Int) => Int) => Int = (0 /: xs)
Run Code Online (Sandbox Code Playgroud)
但我的问题主要是关于一般的正确语法.
scala infix-notation currying partial-application associativity
为什么部分应用具有不同签名的函数有效?
以Control.Monad.join为例:
GHCi> :t (=<<)
(=<<) :: Monad m => (a -> m b) -> m a -> m b
GHCi> :t id
id :: a -> a
GHCi> :t (=<<) id
(=<<) id :: Monad m => m (m b) -> m b
Run Code Online (Sandbox Code Playgroud)
为什么它id :: a -> a代替(a -> m b)争论而接受,因为它们明显不同?
我不太清楚在Scala中部分应用函数...我会做一个例子:
def myOperation(x: Int)(y: Int): Int = {
val complexVal = complexCalc(x)
println("complexVal calculated")
complexVal + y
}
def complexCalc(x: Int): Int = x * 2
val partial = myOperation(5)_
println("calculate")
println("result(3): " + partial(3))
println("result(1): " + partial(1))
Run Code Online (Sandbox Code Playgroud)
这个输出将是:
calculate
complexVal calculated
result(3): 13
complexVal calculated
result(1): 11
Run Code Online (Sandbox Code Playgroud)
所以complexVal计算了2次,如果我只计算一次呢?
对于谁有javascript知识的东西:
function myOperation(x) {
var complexVal = complexCalc(x)
return function(y){
complexVal + y
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
那么我之前写的和之间的区别是什么:
def myOperation2(x: Int, y: Int): Int = { …Run Code Online (Sandbox Code Playgroud) 想象一下这样一个功能:
bar :: Foo -> A -> B -> C -> IO ()
Run Code Online (Sandbox Code Playgroud)
该函数IO使用a Foo和其他值执行一些操作.该Foo值必须传递给bar,并可以IO通过以下方式检索:
foo :: X -> IO Foo
Run Code Online (Sandbox Code Playgroud)
现在A,B,C和X都是普通的纯值.我更喜欢这样的bar功能:
bar :: X -> A -> B -> C -> IO ()
Run Code Online (Sandbox Code Playgroud)
并且Foo将bar使用该X值在函数中生成.如果我这样做:
let f = bar myX
Run Code Online (Sandbox Code Playgroud)
f :: A -> B -> C -> IO ().如果我多次调用该函数,X由于部分应用,该值保持不变,但由于它是一种IO效果,因此每次都会生成它.是否有一种 …
在引擎盖方面:堆栈/堆分配,垃圾收集,资源和性能,以下三个有什么区别:
def Do1(a:String) = { (b:String) => { println(a,b) }}
def Do2(a:String)(b:String) = { println(a,b) }
def Do3(a:String, b:String) = { println(a,b) }
Do1("a")("b")
Do2("a")("b")
(Do3("a", _:String))("b")
Run Code Online (Sandbox Code Playgroud)
除了声明中每个采取和返回多少参数的明显表面差异
我是Haskell的初学者,我正在努力把握它.
我有以下问题:
我有一个函数,可以获得5个参数
f x y w z a = x - y - w - z - a
Run Code Online (Sandbox Code Playgroud)
我想申请它,而只是改变变量x从1到10然而y,w,z而且a将永远是相同的.我实现的实现如下,但我认为必须有更好的方法.
假设我想使用:
x from 1 to 10
y = 1
w = 2
z = 3
a = 4
Run Code Online (Sandbox Code Playgroud)
据此,我设法应用如下功能:
map ($ 4) $ map ($ 3) $ map ($ 2) $ map ($ 1) (map f [1..10])
Run Code Online (Sandbox Code Playgroud)
我认为必须有更好的方法将大量缺失参数应用于部分应用的函数,而不必使用太多的映射.
haskell currying partial-application function-composition map-function
从具有多个参数的函数中,我们能否仅对其部分应用一个或两个参数,从而返回采用其余参数的新函数?
使用Ramda的Javascript示例
function buildUri (scheme, domain, path) {
return `${scheme}://${domain}/${path}`
}
const buildHttpsUri = R.partial(buildUri, ['https']);
const twitterFavicon = buildHttpsUri('twitter.com', 'favicon.ico');
Run Code Online (Sandbox Code Playgroud) 我在REPL中创建了部分应用函数的列表,如下所示:
listOfPartiallyAppliedFunctions = map (*) [1..100]
Run Code Online (Sandbox Code Playgroud)
然后,我想通过完成函数应用程序来创建结果列表,我可以通过为map函数提供lambda来轻松地做到这一点,如下所示:
let results = map (\x -> x 4) listOfPartiallyAppliedFunctions
Run Code Online (Sandbox Code Playgroud)
从本质上讲,这意味着将部分应用函数x映射到部分应用函数列表中的4,其中x是列表中每个部分应用函数。
但是,我认为接下来可以写:
let results = map (4) listOfPartiallyAppliedFunctions
Run Code Online (Sandbox Code Playgroud)
由于不需要为地图函数提供lambda,因为它应该知道将4应用于包含在中的部分应用的函数listOfPartiallyAppliedFunctions。
但是,我收到此错误:
• Non type-variable argument in the constraint: Num ((a -> a) -> b)
(Use FlexibleContexts to permit this)
• When checking the inferred type
it :: forall a b. (Num a, Num ((a -> a) -> b), Enum a) => [b]
Run Code Online (Sandbox Code Playgroud)
有人可以帮我解析此错误吗?我以为4是类型构造函数Num的实例?