lambda recursion

Discussion in 'C++' started by Chris Forone, Apr 28, 2013.

  1. Chris Forone

    Chris Forone Guest

    hello group,

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

    cheers, chris
    Chris Forone, Apr 28, 2013
    #1
    1. Advertising

  2. Chris Forone

    Chris Forone Guest

    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
    Chris Forone, Apr 28, 2013
    #2
    1. Advertising

  3. Chris Forone

    Stefan Ram Guest

    Chris Forone <> writes:
    >Am 28.04.2013 18:00, schrieb Chris Forone:
    >>can a lambda function call itself for recursion purposes? thanks for
    >>answering!

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


    When the Y combinator is used, it does not have to be named IIRC.
    Stefan Ram, Apr 28, 2013
    #3
  4. Chris Forone

    Chris Forone Guest

    Am 28.04.2013 18:02, schrieb 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

    or it cant :)

    "error: 'split' is not captured"

    cheers, chris
    Chris Forone, Apr 28, 2013
    #4
  5. Chris Forone

    Chris Forone Guest

    Am 28.04.2013 23:50, schrieb Andy Champ:
    > On 28/04/2013 17:21, Chris Forone wrote:
    >> or it cant :)
    >>
    >> "error: 'split' is not captured"

    >
    > Are you using MS Visual Studio? Some versions have problems in capturing
    > variables from outer scopes.
    >
    > Andy

    i use mingw gcc 4.8.0 from page http://sourceforge.net/projects/mingwbuilds.

    chris
    Chris Forone, Apr 29, 2013
    #5
  6. Chris Forone

    Chris Forone Guest

    Am 29.04.2013 12:29, schrieb Andy Champ:
    > On 29/04/2013 08:46, Chris Forone wrote:
    >> Am 28.04.2013 23:50, schrieb Andy Champ:
    >>> On 28/04/2013 17:21, Chris Forone wrote:
    >>>> or it cant :)
    >>>>
    >>>> "error: 'split' is not captured"
    >>>
    >>> Are you using MS Visual Studio? Some versions have problems in capturing
    >>> variables from outer scopes.
    >>>
    >>> Andy

    >> i use mingw gcc 4.8.0 from page
    >> http://sourceforge.net/projects/mingwbuilds.
    >>
    >> chris

    >
    > 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, " "));
    }
    Chris Forone, Apr 29, 2013
    #6
  7. Chris Forone

    Chris Forone Guest

    Am 29.04.2013 12:40, schrieb Chris Forone:
    > Am 29.04.2013 12:29, schrieb Andy Champ:
    >> On 29/04/2013 08:46, Chris Forone wrote:
    >>> Am 28.04.2013 23:50, schrieb Andy Champ:
    >>>> On 28/04/2013 17:21, Chris Forone wrote:
    >>>>> or it cant :)
    >>>>>
    >>>>> "error: 'split' is not captured"
    >>>>
    >>>> Are you using MS Visual Studio? Some versions have problems in
    >>>> capturing
    >>>> variables from outer scopes.
    >>>>
    >>>> Andy
    >>> i use mingw gcc 4.8.0 from page
    >>> http://sourceforge.net/projects/mingwbuilds.
    >>>
    >>> chris

    >>
    >> 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);
    Chris Forone, Apr 29, 2013
    #7
  8. Chris Forone

    Chris Forone Guest

    Am 29.04.2013 12:40, schrieb Chris Forone:
    > Am 29.04.2013 12:29, schrieb Andy Champ:
    >> On 29/04/2013 08:46, Chris Forone wrote:
    >>> Am 28.04.2013 23:50, schrieb Andy Champ:
    >>>> On 28/04/2013 17:21, Chris Forone wrote:
    >>>>> or it cant :)
    >>>>>
    >>>>> "error: 'split' is not captured"
    >>>>
    >>>> Are you using MS Visual Studio? Some versions have problems in
    >>>> capturing
    >>>> variables from outer scopes.
    >>>>
    >>>> Andy
    >>> i use mingw gcc 4.8.0 from page
    >>> http://sourceforge.net/projects/mingwbuilds.
    >>>
    >>> chris

    >>
    >> 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
    Chris Forone, Apr 29, 2013
    #8
  9. Chris Forone

    Chris Forone Guest

    Am 29.04.2013 12:40, schrieb Chris Forone:
    > Am 29.04.2013 12:29, schrieb Andy Champ:
    >> On 29/04/2013 08:46, Chris Forone wrote:
    >>> Am 28.04.2013 23:50, schrieb Andy Champ:
    >>>> On 28/04/2013 17:21, Chris Forone wrote:
    >>>>> or it cant :)
    >>>>>
    >>>>> "error: 'split' is not captured"
    >>>>
    >>>> Are you using MS Visual Studio? Some versions have problems in
    >>>> capturing
    >>>> variables from outer scopes.
    >>>>
    >>>> Andy
    >>> i use mingw gcc 4.8.0 from page
    >>> http://sourceforge.net/projects/mingwbuilds.
    >>>
    >>> chris

    >>
    >> 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
    Chris Forone, Apr 29, 2013
    #9
  10. Chris Forone

    Ike Naar Guest

    On 2013-04-29, Chris Forone <> wrote:
    > 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.
    Ike Naar, Apr 29, 2013
    #10
  11. Chris Forone

    Chris Forone Guest

    Am 29.04.2013 13:41, schrieb Ike Naar:
    > On 2013-04-29, Chris Forone <> wrote:
    >> 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
    Chris Forone, Apr 29, 2013
    #11
  12. Chris Forone

    Chris Forone Guest

    Am 29.04.2013 16:49, schrieb Andy Champ:
    > On 29/04/2013 12:15, Chris Forone wrote:
    >> if (first + 3 < second) does it... sorry for that :)

    >
    > 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
    Chris Forone, Apr 30, 2013
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xah Lee

    A Lambda Logo Tour

    Xah Lee, Apr 5, 2006, in forum: Java
    Replies:
    5
    Views:
    495
    Alex Hunsley
    Apr 7, 2006
  2. Roman Suzi
    Replies:
    13
    Views:
    585
    Bengt Richter
    Jan 7, 2005
  3. Steve Dogers

    lambda vs non-lambda proc

    Steve Dogers, Mar 30, 2009, in forum: Ruby
    Replies:
    1
    Views:
    153
    Sean O'Halpin
    Mar 30, 2009
  4. Replies:
    8
    Views:
    710
    John Reye
    Apr 26, 2012
  5. Haochen Xie
    Replies:
    4
    Views:
    229
    Haochen Xie
    Mar 17, 2013
Loading...

Share This Page