currying

Function composition in Haskell with tuple arguments [duplicate]

我的未来我决定 提交于 2019-12-06 18:23:24
问题 This question already has answers here : Feed elements of a tuple to a function as arguments in Haskell? (3 answers) Closed 4 years ago . Sometimes I have two functions of the form: f :: a -> (b1,b2) h :: b1 -> b2 -> c and I need the composition g. I solve this by changing h to h': h' :: (b1,b2) -> c Can you please show me (if possible) a function m, so that: (h . m . f) == (h' . f) Or another way to deal with such situations. Thanks. 回答1: What you're looking to do is to take a function that

Number of arguments and point-free in Haskell [duplicate]

爱⌒轻易说出口 提交于 2019-12-06 17:30:31
问题 This question already has answers here : Defining a function by equations with different number of arguments (3 answers) Closed 2 years ago . With multiple pattern-matching, different numbers of arguments are impossible, even with point-free! foo True b = b + 2 foo _ = id doesn't work for example. But foo True = (+2) foo _ = id does. Sometimes we can use point-free only in one part of the function, so... Why? Is it too hard for GHC? :'( 回答1: Why? Is it too hard for GHC? No . It is not at all

Implicit currying in Scheme with syntax-rules?

前提是你 提交于 2019-12-06 02:29:54
问题 Jeffrey Meunier has an implicit Curry macro here, which uses defmacro. I was wondering if someone has ever written this with syntax-rules? 回答1: There are a number of curry implementations for Scheme -- none can be as elegant as Haskell, since there functions are always unary functions, so everything can be curried. (But this can of course be implemented in a sufficiently powerful Scheme like Racket.) As for the macro that you've dug up -- it's a pretty bad one: not only does it use an

curry in scheme

被刻印的时光 ゝ 提交于 2019-12-06 01:04:16
问题 I have this curry function: (define curry (lambda (f) (lambda (a) (lambda (b) (f a b))))) I think it's like (define curry (f a b)) . my assignment is to write a function consElem2All using curry ,which should work like (((consElem2All cons) 'b) '((1) (2 3) (4))) >((b 1) (b 2 3) (b 4)) I have wrote this function in a regular way: (define (consElem2All0 x lst) (map (lambda (elem) (cons x elem)) lst)) but still don't know how to transform it with curry . Can anyone help me? thanks in advance

From Haskell to Python: how to do currying?

ぐ巨炮叔叔 提交于 2019-12-06 00:43:52
I recently started coding in Python and I was wondering if it's possible to return a function that specializes another function. For example, in Haskell you can create a function that adds 5 to any given number like this: sumFive = (+5) Is it somehow possible in Python? I think the other answers are misunderstanding the question. I believe the OP is asking about partial application of a function, in his example the function is (+) . If the goal isn't partial application, the solution is as simple as: def sumFive(x): return x + 5 For partial application in Python, we can use this function:

How to combine Curry() with Vectorize()?

你说的曾经没有我的故事 提交于 2019-12-05 12:28:35
Consider the following function: addAmount <- function(x, amount) { stopifnot(length(x) == 1) return(x + amount) } It can be used to add some amount to x : > addAmount(x = 5, amount = 3) [1] 8 > addAmount(x = 2, amount = 3) [1] 5 However, x must be of length 1: > addAmount(x = 7:9, amount = 3) Error: length(x) == 1 is not TRUE I added this restriction intentionally for exemplification. Using Vectorize , it is possible to pass in a vector for x : > Vectorize(addAmount)(x = 7:9, amount = 3) [1] 10 11 12 So far, so good. However, I'd like to turn my addAmount function into a "add 3" function,

How are functions curried?

梦想与她 提交于 2019-12-05 11:33:58
问题 I understand what the concept of currying is, and know how to use it. These are not my questions, rather I am curious as to how this is actually implemented at some lower level than, say, Haskell code. For example, when (+) 2 4 is curried, is a pointer to the 2 maintained until the 4 is passed in? Does Gandalf bend space-time? What is this magic? 回答1: Short answer: yes a pointer is maintained to the 2 until the 4 is passed in. Longer than necessary answer: Conceptually, you're supposed to

Ruby Reverse Currying: Is this possible?

有些话、适合烂在心里 提交于 2019-12-05 10:00:43
Concerning currying in Ruby 1.9.x, I've been using it in some places, and can be translated like basically supporting default parameters to the proc arguments: p = proc {|x, y, z|x + y + z} p.curry[1] #=> returns a lambda p.curry[1, 2] #=> returns a lambda p.curry[1, 2, 3] #=> 6 p2 = p.curry[1, 2] p2.(2) #=> 5 p2.(4) #=> 7 very handy, right? thing is, I would like to be able to curry in reverse, that means, fill the last argument of my proc with a random value. Like this: p = proc{|x, y| x - y }.curry[1] p.(4) my desired result would be 3. this returns -3. i think there's no direct way of

Python currying with any number of variables

冷暖自知 提交于 2019-12-05 07:40:49
I am trying to use currying to make a simple functional add in Python. I found this curry decorator here . def curry(func): def curried(*args, **kwargs): if len(args) + len(kwargs) >= func.__code__.co_argcount: return func(*args, **kwargs) return (lambda *args2, **kwargs2: curried(*(args + args2), **dict(kwargs, **kwargs2))) return curried @curry def foo(a, b, c): return a + b + c Now this is great because I can do some simple currying: >>> foo(1)(2, 3) 6 >>> foo(1)(2)(3) 6 But this only works for exactly three variables. How do I write the function foo so that it can accept any number of

F# parameter passing

谁说胖子不能爱 提交于 2019-12-05 06:02:28
I've always thought that F# had two different ways to pass arguments, curry style and tuple style. Is this actually correct? Isn't it simply one style , curry style, and arguments can either be simple values or tuples. e.g. someFunc (a,b) = isn't this a function with one curry style argument which happens to be a tuple ? Thus allowing me to pass tuples to this function using the pipleline operator? (where the elements of the tuple is named) (1,2) |> someFunc Is this correct? This will work just fine - the difference is when you have let f (a,b) = ... let f2 a b = ... then you can create a