lambda recursion

C

Chris Forone

hello group,

can a lambda function call itself for recursion purposes? thanks for
answering!

cheers, chris
 
C

Chris Forone

Am 28.04.2013 18:00, schrieb Chris Forone:
hello group,

can a lambda function call itself for recursion purposes? thanks for
answering!

cheers, chris

ok, it can because one can name it...

cheers, chris
 
S

Stefan Ram

Chris Forone said:
Am 28.04.2013 18:00, schrieb Chris Forone:
ok, it can because one can name it...

When the Y combinator is used, it does not have to be named IIRC.
 
C

Chris Forone

Am 28.04.2013 18:02, schrieb Chris Forone:
Am 28.04.2013 18:00, schrieb Chris Forone:

ok, it can because one can name it...

cheers, chris
or it cant :)

"error: 'split' is not captured"

cheers, chris
 
C

Chris Forone

Am 29.04.2013 12:29, schrieb Andy Champ:
In that case we'll need a code sample.

Hack away at your code until you have the smallest _compilable_ program
that shows the problem. You never know on the way you may spot the
problem; if you don't, post the code up here and you'll get an answer.

Andy

#include <array>
#include <functional>
#include <iostream>
#include <numeric>

int main()
{
// five 3d vectors (xyz)
std::array<float, 5 * 3> store =
{
1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f
};

std::function<void (std::size_t, std::size_t)> split =
[&](std::size_t first, std::size_t second)
{
if (first < second)
{
std::size_t middle((second - first) / 2);

// vector addidion
std::transform(&store[first], &store[first + 3], &store[second],
&store[middle], std::plus<float>());

// normalize new vector
std::transform(&store[middle], &store[middle + 3], &store[middle],
std::bind(std::divides<float>(), std::placeholders::_1,
std::sqrt(std::inner_product(&store[middle], &store[middle + 3],
&store[middle], 0.0f))));

// if you uncomment, gcc gives error, vc 2012 express compiles
// and crashes after start
//split(first, middle); split(middle, second);
}
};

split(0, 12);

std::copy(store.begin(), store.end(),
std::eek:stream_iterator<float>(std::cout, " "));
}
 
C

Chris Forone

Am 29.04.2013 12:40, schrieb Chris Forone:
Am 29.04.2013 12:29, schrieb Andy Champ:
In that case we'll need a code sample.

Hack away at your code until you have the smallest _compilable_ program
that shows the problem. You never know on the way you may spot the
problem; if you don't, post the code up here and you'll get an answer.

Andy

#include <array>
#include <functional>
#include <iostream>
#include <numeric>

int main()
{
// five 3d vectors (xyz)
std::array<float, 5 * 3> store =
{
1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f
};

std::function<void (std::size_t, std::size_t)> split =
[&](std::size_t first, std::size_t second)
{
if (first < second)
{
std::size_t middle((second - first) / 2);

// vector addidion
std::transform(&store[first], &store[first + 3], &store[second],
&store[middle], std::plus<float>());

// normalize new vector
std::transform(&store[middle], &store[middle + 3], &store[middle],
std::bind(std::divides<float>(), std::placeholders::_1,
std::sqrt(std::inner_product(&store[middle], &store[middle + 3],
&store[middle], 0.0f))));

// if you uncomment, gcc gives error, vc 2012 express compiles
// and crashes after start
//split(first, middle); split(middle, second);
}
};

split(0, 12);

std::copy(store.begin(), store.end(),
std::eek:stream_iterator<float>(std::cout, " "));
}
pleas change line

std::size_t middle((second - first) / 2);

to

std::size_t middle((second + first) / 2);
 
C

Chris Forone

Am 29.04.2013 12:40, schrieb Chris Forone:
Am 29.04.2013 12:29, schrieb Andy Champ:
In that case we'll need a code sample.

Hack away at your code until you have the smallest _compilable_ program
that shows the problem. You never know on the way you may spot the
problem; if you don't, post the code up here and you'll get an answer.

Andy

#include <array>
#include <functional>
#include <iostream>
#include <numeric>

int main()
{
// five 3d vectors (xyz)
std::array<float, 5 * 3> store =
{
1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f
};

std::function<void (std::size_t, std::size_t)> split =
[&](std::size_t first, std::size_t second)
{
if (first < second)
{
std::size_t middle((second - first) / 2);

// vector addidion
std::transform(&store[first], &store[first + 3], &store[second],
&store[middle], std::plus<float>());

// normalize new vector
std::transform(&store[middle], &store[middle + 3], &store[middle],
std::bind(std::divides<float>(), std::placeholders::_1,
std::sqrt(std::inner_product(&store[middle], &store[middle + 3],
&store[middle], 0.0f))));

// if you uncomment, gcc gives error, vc 2012 express compiles
// and crashes after start
//split(first, middle); split(middle, second);
}
};

split(0, 12);

std::copy(store.begin(), store.end(),
std::eek:stream_iterator<float>(std::cout, " "));
}
sorry group for the chaos...

now i see the error in the line if (first < second)

i will fix it and then try again

cheers, chris
 
C

Chris Forone

Am 29.04.2013 12:40, schrieb Chris Forone:
Am 29.04.2013 12:29, schrieb Andy Champ:
In that case we'll need a code sample.

Hack away at your code until you have the smallest _compilable_ program
that shows the problem. You never know on the way you may spot the
problem; if you don't, post the code up here and you'll get an answer.

Andy

#include <array>
#include <functional>
#include <iostream>
#include <numeric>

int main()
{
// five 3d vectors (xyz)
std::array<float, 5 * 3> store =
{
1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f
};

std::function<void (std::size_t, std::size_t)> split =
[&](std::size_t first, std::size_t second)
{
if (first < second)
{
std::size_t middle((second - first) / 2);

// vector addidion
std::transform(&store[first], &store[first + 3], &store[second],
&store[middle], std::plus<float>());

// normalize new vector
std::transform(&store[middle], &store[middle + 3], &store[middle],
std::bind(std::divides<float>(), std::placeholders::_1,
std::sqrt(std::inner_product(&store[middle], &store[middle + 3],
&store[middle], 0.0f))));

// if you uncomment, gcc gives error, vc 2012 express compiles
// and crashes after start
//split(first, middle); split(middle, second);
}
};

split(0, 12);

std::copy(store.begin(), store.end(),
std::eek:stream_iterator<float>(std::cout, " "));
}

if (first + 3 < second) does it... sorry for that :)

cheers, chris
 
I

Ike Naar

Am 29.04.2013 12:40, schrieb Chris Forone:
[snip]
// five 3d vectors (xyz)
std::array<float, 5 * 3> store =
{
1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f
};
[snip]
split(0, 12);
[snip]

if (first + 3 < second) does it... sorry for that :)

Are you sure?
Consider, for instance, what happens when the array has 4*3 elements
and split(0,9) is called (hint: at the next level of recursion,
split(0,4) and split(4,9) will be called).
It looks like you need to calculate middle in such a way that it
is a multiple of 3.
 
C

Chris Forone

Am 29.04.2013 13:41, schrieb Ike Naar:
Am 29.04.2013 12:40, schrieb Chris Forone:
[snip]
// five 3d vectors (xyz)
std::array<float, 5 * 3> store =
{
1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f
};
[snip]
split(0, 12);
[snip]

if (first + 3 < second) does it... sorry for that :)

Are you sure?
Consider, for instance, what happens when the array has 4*3 elements
and split(0,9) is called (hint: at the next level of recursion,
split(0,4) and split(4,9) will be called).
It looks like you need to calculate middle in such a way that it
is a multiple of 3.
yes, i know this. i am only experimenting...

cheers, chris
 
C

Chris Forone

Am 29.04.2013 16:49, schrieb Andy Champ:
Not for me. You get a stack overflow from infinite recursion.

When called with (4,12) it passes the test. Middle is set to 4, so the
second recursive split call is with parameters (4, 12). After a while,
bang.

I can't say I know what your code is _supposed_ to do, but I'm sure it
isn't doing it!

Andy
yes, my post was to hasty. the call with (4, 12) will end with the calls
(4, 8) and (8, 12) because of post on 13:07. i will not post some kind
of experimental code in the future anymore. :)

cheers, chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top