Python Forum
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Incorrect lambda
#1
Hi! I'm trying to make a map with "factorial" func, but with a sum and odd numbers. For example, 1=1, 3=3+1, 5=5+3+1 and so on.
Now I have something like this, but unfortunately it works with errors, but I don't understand why. When I try to pass in function (x-2) instead of (x-1), I get "maximum recursion depth exceeded" Sad
down_sum = lambda x: x and x + down_sum(x - 1) or 0
squares = list(map(down_sum, [1, 3, 5, 7]))
Reply
#2
Because with x - 2 and odd numbers for input, you skip 0. But 0 is your terminating condition, so you would recurse infinitely, but you get stopped by the recursion limit.

p.s. Assigning a lambda to a variable is generally considered a bad idea. You should define a function.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Aug-13-2018, 12:16 AM)ichabod801 Wrote: Because with x - 2 and odd numbers for input, you skip 0. But 0 is your terminating condition, so you would recurse infinitely, but you get stopped by the recursion limit.

p.s. Assigning a lambda to a variable is generally considered a bad idea. You should define a function.

Thanks! Now I have working code.
def down_sum(x):
    if x == 1:
        return 1
    return x + down_sum(x - 2)


squares = list(map(down_sum, [1, 3, 5, 7]))
But still interesting. Was it possible to make such a recursive lambda in a map?
Reply
#4
It's only possible to create a recursive lambda if you assign in to a variable name. Otherwise you have no way to make the recursive call, you need that name. But lambda's are designed for situations where you're not assigning to a name. That's why you shouldn't assign them to names, you should just define a function.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code is returning the incorrect values. syntax error 007sonic 6 1,136 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  error 1102 (42000) incorrect database name 's' Anldra12 4 1,651 Jun-08-2022, 09:00 AM
Last Post: Anldra12
  openpyxl incorrect delete rows VladislavM 6 4,029 Jul-19-2021, 08:54 AM
Last Post: VladislavM
  Incorrect Type Error milkycow 4 2,828 Jun-25-2021, 06:04 AM
Last Post: milkycow
  Why is 0.1 * 0.2 arithmetically incorrect? Pedroski55 2 2,180 Nov-25-2020, 12:01 AM
Last Post: snippsat
  user input producing incorrect boolean al_Czervik 4 3,023 Mar-05-2020, 09:50 PM
Last Post: al_Czervik
  Incorrect time format KoSik 5 2,938 Aug-15-2019, 05:10 PM
Last Post: KoSik
  Incorrect code output (File not being created) Hass 4 2,874 Jan-01-2019, 04:10 AM
Last Post: ichabod801
  mysql.connector - incorrect integer value Milo 3 11,090 Aug-14-2017, 06:29 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020