2017-01-27 5 views
-4

これらのラムダ関数は結果を返します13なぜですか?誰かが私を理解するのを助けてくれますか?ありがとう、この機能は何をしますか?多くのラムダ

two_to_one = lambda g: (lambda y: g(y, y)) 
one_to_two = lambda f: (lambda x, y: f(x)+f(y)) 
h=one_to_two(two_to_one (lambda x, y: x*y)) 
print (h(3, 2)) 

が出力:

13 
+0

よし感謝を取得します。 –

+2

@DavidPitts画像を削除してテキストとして投稿するだけです。 – MYGz

+2

コンピュータの役割を果たします。鉛筆をつかんで、コンピュータがそれを実行し、値が何であるかのように各ステップを書き留めます。 – tdelaney

答えて

3

内訳のバージョン:

def two_to_one(foog): 
    def foo1(y): 
     return foog(y,y) 
    return foo1 

def one_to_two(foof): 
    def foo2(x,y): 
     return foof(x)+foof(y) 
    return foo2 

def foo3(x,y): 
    return x*y 

h = one_to_two(two_to_one(foo3)) 

print h(3,2) 
関数が呼び出されますどのように

Step1: 
two_to_one(foo3) returns foo1. Now foog = foo3 

Step2: 
one_to_two(foo1) returns foo2. Now foof = foo1 

Step3: 
h = foo2 

Step4: 
h(3,2) will call foo2(3,2) 

Step5: 
foo2(3,2) calls: 

#foof(x) foog(x,x) 
foo1(3) --> foo3(3,3) --> 3*3 
          + --> 13 #return this from foo2 
foo1(2) --> foo3(2,2) --> 2*2 
#foof(y) foog(y,y) 

Step6: 
print 13 #print return value of h(3,2) 
+0

私はそれを掲載しようとしていました!説明的なテキストは良いでしょう。これらの関数は、それらのパラメータにバインドされているがまだ実行されていない内部関数をクロージャを返します。したがって、 'h'は関数であり、' h(3,2) 'はそれを呼び出します。 – tdelaney

+0

@tdelaneyこれは非常に混乱しています:D私は流れを把握していました。私は改善します – MYGz

+0

@tdelaney私は今をあきらめます。 – MYGz

3

このスクリプトは、リットルを使用していますアンビエントクロージャを作成するためのambda - 関数が呼び出されるたびに使用される変数にバインドされた関数。それが何であるかを理解するために、すべてを明示的にし、コードをプリントで捨てて何が起こっているのかを見ることができます。興味深いことに、すべての計算がh(3, 2)これを実行する

# two_to_one = lambda g: (lambda y: g(y, y)) 
def two_to_one(g): 
    """Return a function `func(y)` that applies input function `g` 
    in the formula: g(y, y) 
    """ 
    def _two_to_one_closure(y): 
     print("_two_to_one_closure calls: {}({})".format(g.__name__, y)) 
     rc = g(y, y) 
     print("_two_to_one_closure returns", rc) 
     return rc 
    return _two_to_one_closure 

# one_to_two = lambda f: (lambda x, y: f(x)+f(y)) 
def one_to_two(f): 
    """Return a function `func(y)` that applies input function `f` 
    in the formula: f(x) + f(y) 
    """ 
    def _one_to_two_closure(x, y): 
     print("_one_to_two_closure calls: {}({}) + {}({})".format(
      f.__name__,x,f.__name__,y)) 
     rc = f(x) + f(y) 
     print("_one_to_two_closure returns", rc) 
     return rc 
    return _one_to_two_closure 

# h=one_to_two(two_to_one (lambda x, y: x*y)) 
def _anon(x, y): 
    print("_anon", x ,y) 
    rc = x*y 
    print("_anon returns", rc) 
    return rc 

g = two_to_one(_anon)  # create a _two_to_one_closure that will 
print("g", g)    # call _anon when called 

h = one_to_two(g)   # create a _one_to_two_closure that will 
print("h", h)    # call `g` when called 

# print (h(3, 2)) 
print("do the calculations on 3, 2") 
i = h(3, 2)    # call h, which is a _one_to_two_closure 
print(i) 

で行われ、あなたはあなたの助けのための

g <function two_to_one.<locals>._two_to_one_closure at 0x7fca5165a1e0> 
h <function one_to_two.<locals>._one_to_two_closure at 0x7fca5165a268> 
do the calculations on 3, 2 
_one_to_two_closure calls: _two_to_one_closure(3) + _two_to_one_closure(2) 
_two_to_one_closure calls: _anon(3) 
_anon 3 3 
_anon returns 9 
_two_to_one_closure returns 9 
_two_to_one_closure calls: _anon(2) 
_anon 2 2 
_anon returns 4 
_two_to_one_closure returns 4 
_one_to_two_closure returns 13 
13 
_two_to_one_closure 2 
_anon 2 2 
13 
+0

ありがとう、私は順序を理解するために多くの助けた。 –

関連する問題