currying

Implicit currying in Scheme with syntax-rules?

耗尽温柔 提交于 2019-12-04 07:45:41
Jeffrey Meunier has an implicit Curry macro here , which uses defmacro. I was wondering if someone has ever written this with syntax-rules? 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 unhygienic macro, it's also calling eval explicitly, and relies on an implementation of environments etc. But it

Haskell function application and currying

戏子无情 提交于 2019-12-04 07:38:35
问题 I am always interested in learning new languages, a fact that keeps me on my toes and makes me (I believe) a better programmer. My attempts at conquering Haskell come and go - twice so far - and I decided it was time to try again. 3rd time's the charm, right? Nope. I re-read my old notes... and get disappointed :-( The problem that made me lose faith last time, was an easy one: permutations of integers. i.e. from a list of integers, to a list of lists - a list of their permutations: [int] ->

In F#, how do you curry ParamArray functions (like sprintf)?

瘦欲@ 提交于 2019-12-04 06:54:31
In F#, how do you curry a function that accepts a variable number of parameters? I have code like this...(the log function is just an example, the exact implementation doesn't matter) let log (msg : string) = printfn "%s" msg log "Sample" It gets called throughout the code with sprintf formatted strings, ex. log (sprintf "Test %s took %d seconds" "foo" 2.345) I want to curry the sprintf functionality in the log function so it looks like... logger "Test %s took %d seconds" "foo" 2.345 I've tried something like let logger fmt ([<ParamArray>] args) = log (sprintf fmt args) but I cannot figure out

Is a section the result of currying?

ε祈祈猫儿з 提交于 2019-12-04 05:47:11
问题 In Programming in Haskell by Hutton In general, if # is an operator, then expressions of the form (#) , (x #) , and (# y) for arguments x and y are called sections, whose meaning as functions can be formalised using lambda expressions as follows: (#) = \x -> (\y -> x # y) (x #) = \y -> x # y (# y) = \x -> x # y What are the difference and relation between "section" and "currying"? Is a section the result of applying the currying operation to a multi-argument function? Thanks. 回答1: Left

What are the benefits of currying?

孤街醉人 提交于 2019-12-04 03:53:44
I don't think I quite understand currying, since I'm unable to see any massive benefit it could provide. Perhaps someone could enlighten me with an example demonstrating why it is so useful. Does it truly have benefits and applications, or is it just an over-appreciated concept? (There is a slight difference between currying and partial application , although they're closely related; since they're often mixed together, I'll deal with both terms.) The place where I realized the benefits first was when I saw sliced operators: incElems = map (+1) --non-curried equivalent: incElems = (\elems ->

What is the point of multiple parameter clauses in function definitions in Scala?

烂漫一生 提交于 2019-12-04 03:52:24
I'm trying to understand the point of this language feature of multiple parameter clauses and why you would use it. Eg, what's the difference between these two functions really? class WTF { def TwoParamClauses(x : Int)(y: Int) = x + y def OneParamClause(x: Int, y : Int) = x + y } >> val underTest = new WTF >> underTest.TwoParamClauses(1)(1) // result is '2' >> underTest.OneParamClause(1,1) // result is '2' There's something on this in the Scala specification at point 4.6 . See if that makes any sense to you. NB: the spec calls these 'parameter clauses', but I think some people may also call

Partial application of operators

一个人想着一个人 提交于 2019-12-04 02:32:08
问题 If I want to add a space at the end of a character to return a list, how would I accomplish this with partial application if I am passing no arguments? Also would the type be? space :: Char -> [Char] I'm having trouble adding a space at the end due to a 'parse error' by using the ++ and the : operators. What I have so far is: space :: Char -> [Char] space = ++ ' ' Any help would be much appreciated! Thanks 回答1: Doing what you want is so common in Haskell it's got its own syntax, but being

How do you curry the 2nd (or 3rd, 4th, …) parameter in F# or any functional language?

亡梦爱人 提交于 2019-12-04 00:17:33
I'm just starting up with F# and see how you can use currying to pre-load the 1st parameter to a function. But how would one do it with the 2nd, 3rd, or whatever other parameter? Would named parameters to make this easier? Are there any other functional languages that have named parameters or some other way to make currying indifferent to parameter-order? Typically you just use a lambda: fun x y z -> f x y 42 is a function like 'f' but with the third parameter bound to 42. You can also use combinators (like someone mentioned Haskell's "flip" in a comment), which reorder arguments, but I

How do I write a function that returns another function?

佐手、 提交于 2019-12-03 18:26:01
问题 In Python, I'd like to write a function make_cylinder_volume(r) which returns another function. That returned function should be callable with a parameter h , and return the volume of a cylinder with height h and radius r . I know how to return values from functions in Python, but how do I return another function ? 回答1: Try this, using Python: import math def make_cylinder_volume_func(r): def volume(h): return math.pi * r * r * h return volume Use it like this, for example with radius=10 and

Currying Example in Scala

强颜欢笑 提交于 2019-12-03 17:21:49
Is the following a good example of currying? def sum(a: Int, b: Int) : (Int => Int) = { def go(a: Int) : Int = { a + b; } go } I half understand the below results, but how could I write (or maybe how I should've written) sum() in a curried way? scala> sum(3,4) res0: Int => Int = <function1> scala> sum(3,4).apply(2) res1: Int = 6 scala> sum(3,4).apply(3) res2: Int = 7 4lex1v Currying mechanism was introduced in Scala to support type inference. For example foldLeft function in the standard lib: def foldLeft[B](z: B)(op: (B, A) => B): B Without currying you must provide types explicitly: def