当我执行以下操作时会发生什么?
(define ((func x) y)
(if (zero? y)
((func x) 1)
12))
Run Code Online (Sandbox Code Playgroud)
我明白我可以这样做:
(define curried (func 5))
Run Code Online (Sandbox Code Playgroud)
而现在我可以使用咖喱.我很好奇的是函数的定义.是行吗?
((func x) 1)
Run Code Online (Sandbox Code Playgroud)
用x作为参数创建一个新的lambda,然后在1上调用它?或者它比那更聪明,它只是重新使用现有的.(例如,如果我这样做(curried 0),该((func x) 1)行将等同于(curried 1)- PLAI Scheme是否这样做?)
目前我正在学习Haskell.我们必须确定给定函数的最一般类型,但我还没有得到它.解释器如何确定函数的最一般类型,尤其是lambda表达式?什么是手动确定最常规类型的安全方法?
tx2 = (\x y z -> y.z.z)
tx2::a->(a->b)->(a->a)->b -- my guess
tx2 :: a -> (b -> c) -> (b -> b) -> b -> c -- interpreter solution
Run Code Online (Sandbox Code Playgroud)
如果第一个变量(a)应用于表达式z,则z必须将a作为输入参数,但它在(b-> b)中使用b.y消耗b并生成c,因此最终结果必须为c.但为什么b(作为中间结果?)包含在类型中?如果是这样,为什么它不是 - >(b - > c) - >(b - > b) - > b-> b - > c?
tm2 = (\i -> [sum,product]!!i)
tm2:: Int->[(Integer->Integer->Integer)]->(Integer->Integer->Integer) -- my guess
\i -> [sum,product] !! i :: Num a => Int -> [a] -> a -- interpreter with direct input
tm2 :: …Run Code Online (Sandbox Code Playgroud) Ruby 1.9内置支持currying支持两种处理proc的方法,它采用任意数量的参数:
my_proc = proc {|*x| x.max }
Run Code Online (Sandbox Code Playgroud)
1)curry没有争论:my_proc.curry.你将逗号分隔的参数传递给curried的proc,就像你对普通的proc一样.如果参数的数量是任意的,则无法实现正确的currying(如果某些参数不是splat,则会很有用)
2)curry带参数:my_proc.curry(n)这样,currying就会被应用,好像proc会接受n参数.例如:
my_proc.curry(3).call(2).call(5).call(1) #=> 5
Run Code Online (Sandbox Code Playgroud)
那么,你将如何通过任意数量的参数实现currying?那意味着,如果n没有给出?
我想到的一种方法是通过代理收集参数call然后解析procvia method_missing(如果除了call使用之外的任何方法/ call没有参数使用,请proc使用收集的参数调用),但我仍然在寻找其他方法实现它.
更新
正如Andy H所说,问题是什么时候停止晃动.对于我的目的,如果currying停止/ proc评估何时call调用任何方法或者call在没有参数的情况下调用,那就没关系.
我正在读这个OCaml走过幻灯片,我在这里找到一个有趣的问题:

似乎oops函数会生成编译错误:
the type of this expression contains type variables that cannot be generalized
Run Code Online (Sandbox Code Playgroud)
我不知道原因所以我在我的Mac上使用OCaml版本4.01.0快速对此功能进行了一些测试,令我惊讶的是,在解释此代码时我没有看到任何错误
我不知道为什么幻灯片因为currying而声称它是一个错误,我无法重新创建此错误...
谁能给我一些帮助?
首先,我并不完全熟悉有问题的概念,如果我滥用任何术语,请原谅我.我想知道的是,如果我有类似的东西:
int someGlobal = 7;
int sumThree(int a, int b){
return (a + b + someGlobal);
}
Run Code Online (Sandbox Code Playgroud)
如果可以通过调用部分应用/ curry该函数sumThree(3),那么这将被转换为等效于:
int sumThree(int b){
return (3 + b + someGlobal);
}
Run Code Online (Sandbox Code Playgroud)
或者:
int sumThree(int b){
return (3 + b + 7); //or, return (10+b)
}
Run Code Online (Sandbox Code Playgroud)
其中一个会"更正确"吗?或者,如果它们只是同一种形式的不同形式,那么用什么术语来区分它们中的两种?上面两段代码的不同之处在于,如果someGlobal的值在sumThree(3)被调用之后但在应用函数的其余部分之前发生了变化,则前者会对后者产生不同的结果,因为后者捕获了值(7)当时有些全球人sumThree(3)称之为.
我目前正在阅读Graham Hutton撰写的“在haskell中编程”,并且刚刚接触到curring和函数组合。在练习部分中,有一个从头开始实现curry函数的任务,该任务已存在于前奏模块中。
这是我的实现,但是不起作用。有人可以解释一下(我是函数编程的新手)为什么它不起作用
my_curry :: ((a ,b) -> c) -> (a -> b -> c)
my_curry origFunc = origFunc.combine
where
combine e f = (e, f)
Run Code Online (Sandbox Code Playgroud)
这是错误[已添加]
[1 of 1] Compiling Main ( higher_order.hs, interpreted )
higher_order.hs:92:30:
Couldn't match type `t0 -> (a, t0)' with `(a, b)'
Expected type: a -> (a, b)
Actual type: a -> t0 -> (a, t0)
In the second argument of `(.)', namely `combine'
In the expression: origFunc . combine
In an equation for `my_curry': …Run Code Online (Sandbox Code Playgroud) 我在Racket做了一些练习,遇到了一个我似乎无法查询文档的问题.
我想modulo为一个除数列表生成以下的curries:
(define multlist '[3 5])
(define modfuncs (map (lambda x ;@ make some modulos
(curry modulo x)) multlist))
Run Code Online (Sandbox Code Playgroud)
这会生成一个curried程序列表,听起来很有希望,但是当我尝试测试其中一个时,我收到以下错误:
-> (car modfuncs)
#<procedure:curried>
-> ((car modfuncs) 3)
; modulo: contract violation
; expected: integer?
; given: '(3)
; argument position: 1st
; [,bt for context]
Run Code Online (Sandbox Code Playgroud)
假设这不是一种可怕的方法,我如何取消引用multlist传递给curry/ mapcall 的值,以便这些函数能够正确计算?
在kdb + / q中,如何通过一系列功能通过管道传递数据,以使上一步的输出成为下一步的输入?
例如:
q)t:([]sym:`a`c`b;val:1 3 2)
q)`sym xkey `sym xasc t / how to achieve the same result as this?
Run Code Online (Sandbox Code Playgroud)
我猜想有些变化over或/可以工作:
?? over (xasc;xkey)
Run Code Online (Sandbox Code Playgroud)
优点:如何实现t从右侧插入的方式(本着q语法左右读法的精神)?
(xasc;xkey) ?? t
Run Code Online (Sandbox Code Playgroud) 我正在测试一些currying-in功能,我可以很容易地让它工作:
test = (a) => { return (b) => a+b } // test(5)(6) => 11
Run Code Online (Sandbox Code Playgroud)
使用ES6 destructing参数时,我无法使用相同的功能:
test = ({a}) => { return (b) => a+b } // test(5)(6) => NaN
Run Code Online (Sandbox Code Playgroud)
有没有办法让它发挥作用?为什么第二个测试功能不起作用?
我在理解验证库时遇到了一些麻烦io.vavr.control.Validation.有可能提出过于宽泛的问题,我确实有几个子问题 - 但我相信它们是密切相关的,并且会拼凑起来帮助我理解使用这种验证机制的正确方法.
我从这里的例子开始:https://softwaremill.com/javaslang-data-validation.
Validation<String, ValidRegistrationRequest> validate(RegistrationRequest request) {
return combine(
validateCardId(request.getCardId()),
validateTicketType(request.getTicketType()),
validateGuestId(request.getGuestId())
)
.ap(ValidRegistrationRequest::new)
.mapError(this::errorsAsJson);
}
private Validation<String, Card> validateCardId(String cardId) {
// validate cardId
// if correct then return an instance of entity the cardId corresponds to
}
private Validation<String, TicketType> validateTicketType(String ticketType) {
// validate ticketType
// if known then return enumeration representing the ticket
}
private Validation<String, Guest> validateGuest(String guestId) {
// validate guestId
// if correct then return …Run Code Online (Sandbox Code Playgroud)