Tagged Questions
132
votes
4answers
10k views
What is the difference between a 'closure' and a 'lambda'?
Could someone explain? I understand the basic concepts behind them but I often see them used interchangeably and I get confused.
And now that we're here, how do they differ from a regular function?
34
votes
13answers
6k views
Coolest C# LINQ/Lambdas trick you've ever pulled?
Saw a post about hidden features in C# but not a lot of people have written linq/lambdas example so... I wonder...
What's the coolest (as in the most elegant) use of the C# LINQ and/or ...
31
votes
3answers
7k views
29
votes
5answers
11k views
List filtering: list comprehension vs. lambda + filter
I happened to find myself having a basic filtering need: I have a list and I have to filter it by an attribute of the items.
My code looked like this:
my_list = [i for i in my_list if i.attribute == ...
25
votes
18answers
3k views
How to replace for-loops with a functional statement in C#?
A colleague once said that God is killing a kitten every time I write a for-loop.
When asked how to avoid for-loops, his answer was to use a functional language. However, if you are stuck with a ...
19
votes
6answers
4k views
Bind Vs Lambda?
I have a question about which style is preferred: std::bind Vs lambda in C++0x. I know that they serve -somehow- different purposes but lets take an example of intersecting functionality.
Using ...
14
votes
5answers
352 views
lambda in python
I'm revisiting some scheme excercises in python (if that makes sense) to find out what python can do in terms of FP. My problem concerns lambda in python :
Can i define a general function in python ...
11
votes
3answers
395 views
Examples of functional or dynamic techniques that can substitute for object oriented Design Patterns
This is somewhat related to Does Functional Programming Replace GoF Design Patterns?
Since the introduction of lambdas and dynamics in C#, are there any of the standard design patterns that could be ...
10
votes
3answers
422 views
Lambda Expressions and Memory Management
How do the Lambda Expressions / Closures in C++0x complicate the memory management in C++? Why do some people say that closures have no place in languages with manual memory management? Is their claim ...
10
votes
1answer
653 views
Is it possible to build a comparatively fast untyped lambda calculus machine?
Pure untyped lambda calculus is a powerful concept. However, building a machine or interpreter for real-world use is often described as (close to) impossible. I want to investigate this. Is it ...
8
votes
1answer
298 views
How to write an empty list using S, K and I combinators?
I know that:
(cons [p] [q]) is ((s ((s i) (k [p]))) (k [q]))
(car [lst]) is ([lst] k)
(cdr [lst]) is ([lst] (k i))
I want to write a list like this
(cons [a] (cons [b] (cons [c] [nil])))
, which ...
7
votes
2answers
153 views
Defining a stack data structure and its main operations in lambda calculus
I'm trying to define a stack data structure in lambda calculus, using fixed point combinators. I am trying to define two operations, insertion and removal of elements, so, push and pop, but the only ...
6
votes
2answers
146 views
What is more pythonic - function composition, lambdas, or something else? [closed]
Given the example below, which is more pythonic? Using function composition, lambdas, or (now for) something completely different? I have to say that the lambdas seem to be more readable, but Guido ...
6
votes
5answers
336 views
How to overload an operator for composition of functionals in C++0x?
Is there a way to overload, say the >> operator for function composition? The operator should work seamlessly on lambdas as well as std::function?
Requirements:
The solution should not ...
6
votes
1answer
143 views
Will I be able to use Clojure functions as Lambdas in Java 8?
I use a number of libraries in Clojure that produce higher order functions that conform to the "clojure.lang.IFn" interface.
It has multiple arity overloads, I.e. the interface looks something like:
...
5
votes
2answers
500 views
why do lambda functions in C++11 not have function<> types?
I am playing around with the C++0x/C++11 functional features. One thing I find odd is that the type of a lambda function is actually NOT a function<> type. What's more, lambda's do not seem to play ...
5
votes
5answers
367 views
Take some data, return a pure piecewise function
Given a list of {x,y} datapoints, return a pure function f (from the reals to the reals) such that f[x]==y for every {x,y} in the data. If x is not one of the x-values then return the y-value for the ...
5
votes
4answers
280 views
How does a lambda function refer to its parameters in python?
I am new in Python. My task was quite simple -- I need a list of functions that I can use to do things in batch. So I toyed it with some examples like
fs = [lambda x: x + i for i in xrange(10)]
...
5
votes
3answers
262 views
Why can't print() be used in a lambda expression?
Why is:
p = lambda s: print(s)
invalid syntax but:
def do_print(s):
print(s)
p = lambda s: do_print(s)
valid?
5
votes
1answer
202 views
Lambda calculus expression implementing function application
I just found the following lambda calculus expression:
(((λ f . (λ x . (f x))) (λ a . a)) (λ b . b))
So that is a function that takes an argument f and returns another function that takes an ...
5
votes
3answers
691 views
Pass functions in F#
Is it possible to pass a reference to a function to another function in F#? Specifically, I'd like to pass lambda functions like
foo(fun x -> x ** 3)
More specifically, I need to know how I would ...
5
votes
3answers
152 views
How to handle lambdas in a pre-lambda compiler
I have some code that can greatly be reduced in complexity by using lambdas. However unfortunately we have to use a compiler that does not fully support C++11 and we cannot easily switch. Now the ...
4
votes
5answers
318 views
Lambda functions
I'm really interested how lambda functions are used. Does it make sense to use them in a modern, high-level programming language like php? If yes, why?
Is this really just a function embedded in a ...
4
votes
5answers
150 views
Lambdas inside list comprehensions
I wanted to have a list of lambdas that act as sort of a cache to some heavy computation and noticed this:
>>> [j() for j in [lambda:i for i in range(10)]]
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9]
...
4
votes
2answers
412 views
Recursive lambdas in F#
Take this example code (ignore it being horribly inefficient for the moment)
let listToString (lst:list<'a>) = ;;' prettify fix
let rec inner (lst:list<'a>) buffer = ;;' prettify fix
...
4
votes
7answers
240 views
Example where lambdas are very useful in Python
I met lambda expressions in Python. I have seen already many easy examples (including examples on SE) where lambda expressions produce functions like adders of whatever but I can't see the real ...
4
votes
2answers
111 views
Failing to deduce type from lambdas in the initializer list
#include <functional>
#include <iostream>
namespace{
//const std::function< void( const int ) > foo[] =
const auto foo[] =
{
[]( const int v ){ ...
4
votes
6answers
2k views
Scope of variables in a delegate
I found the following rather strange. Then again, I have mostly used closures in dynamic languages which shouldn't be suspectable to the same "bug". The following makes the compiler unhappy:
...
4
votes
2answers
378 views
map function in Erlang
In addition to having the map function available with many arities (up to 4), Prolog allows you (under certain circumstances) to map a multiple arity function onto a single list. Say you want to test ...
4
votes
1answer
93 views
Eliminate lambda in Scheme?
Hi guys I need to eliminate this lambda construction for my school asignment. Any ideas how to do that?
(define (foo x)
(letrec
((h
(lambda (y z)
(cond
((null? y) 'undefined)
...