Translating a lambda to a normal function in Python

Question:

While trying to understand how to use the lambda, I came across one reply in which the poster said that nothing you can do using lambda you can’t do using normal functions.

I have been trying so hard to call a function from within itself in Python, not expert though, yet I’m learning, and I came across few problems where you need to use recursive functions, call multiple times to get a certain answer.

A guy has used the lambda function to do that, I tried to understand it but I failed, so I though if the functions can be implemented using normal functions, it would be easier to start understanding the lambda from that point on.

Let’s take this sentence for example:

print"n".join(" ".join([(lambda f:(lambda x:f(lambda*r:x(x)(*r)))(lambda x:f(lambda*r:x(x)(*r))))(lambda f:lambda q,n:len(q)<=n and q or f(q[len(q)/2:],n)+f(q[:len(q)/2],n))(k,z+1)for z,k in enumerate(i[:-1].split())]) for i in list(s)[1:])

This has been used in the Facebook hacker cup, I couldn’t solve this problem as I was lost in the loops.

This sentence takes a few words, let’s say “Stackoverflow rocks and it is great”

The problem statement in Facebook is :

You’ve intercepted a series of transmissions encrypted using an interesting and stupid method, which you have managed to decipher. The messages contain only spaces and lowercase English characters, and are encrypted as follows: for all words in a sentence, the ith word (1-based) word is replaced with the word generated by applying the following recursive operation f(word, i):

If the length of word is less than or equal to i,
return word.
Otherwise, return f(right half of word, i) +
f(left half of word, i).

If word is of odd length, it is split such that the right side is longer. You’ve decided to have a little fun with whoever is sending the messages, and to broadcast your own messages encrypted in the same style that they are using.

Input
Your input will begin with an integer N, followed by a newline and then N test cases. Each case consists of an unencrypted sentence containing only spaces and lowercase letters, and cases are newline-separated. There will be no leading or trailing spaces in a sentence and there will be at most 1 space character between any otherwise-adjacent characters

Output
Output, for each case and separated by newlines, the contents of the encrypted sentence after applying the encoding method describe above to it. You may ignore traditional capitalization rules and stick to all lowercase letters.

Constraints
5 ≤ N ≤ 25
Sentences will contain no more than 100 characters.

Asked By: ma2moun

||

Answers:

Python lambdas are simply syntactic sugar. “Regular” functions have the same capabilities such as closures, because, remember, you can define them inside another function, just as lambda does.

def some_func():
  some_expr_using(lambda args: 42)

# becomes:

def some_func():
  def unique_name(args):
    return 42
  some_expr_using(unique_name)

Except that when inspecting the lambda object, its name is set to “<lambda>”, rather than unique_name as above, and other superficial details relating to how the actual source code is spelled rather than as it behaves.

Your code could be written as:

def y(f):
  def a(x):
    def b(*r):
      return x(x)(*r)
    return f(b)
  return a(a)

def fx(f):
  def x(q, n):
    # changed "a and b or c": different semantics if b can be falsy
    if len(q) <= n:
      return q
    else:
      return f(q[len(q) / 2:], n) + f(q[:len(q) / 2], n)
  return x

print "n".join(
  " ".join(y(fx)(k, z + 1) for z, k in enumerate(i[:-1].split()))
  for i in list(s)[1:])

(But only if I’ve translated it correctly; double-check. :P)

This code is an example of a fixed-point combinator, which I only barely understand, and it’s hard to give better names without knowing more context (I didn’t try to decipher the actual problem statement). It can be unraveled to a recursive function which calls itself by name directly.

Answered By: Fred Nurk
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.