LECTURE 5

1.the diagram in the environment  when use height order function

2.3 steps happened when call a new function

  1. 1.create a new frame
  2. 2.bind the formal parameter with the argument
  3. 3.execute the body and return the value

3.rules about how to decide a frame's parent function

  1. 1.depended on the current  environment when the function was created
  2. 2.the parent of the function is the frame in which it was defined
  3. 3.the parent of the frame is the parent function called(which calls the function and create the frame)
  4. 4.function' parent is frame ,and a frame's function is function
def print_sum(x):
    print(x)
    def next_sum(y)
        return print_sum(x+y)
    return next_sum
print_sum(1)(3)(5)
##expect 
##line one :1
##line two :4
##line three :9

look the example up here the print_sum is define on the global frames so no matter how many times you call it ,it is a same frames. but look at at the next_sum which is def in the print_sum function each time you define and then call it you get a new function and they have different parent frames.

the second point is that be careful with the return of the next_sum. it return a value print_sum(x+y), it not it the final result so continue to the function print_sum, and it's final return is another function next_sum, a new function which is different from the one before. so pay a attention to the return value have a parentheses or not. having means it's a value even it is empty. if not, it's a value. there is a other example below

def student_coffee(coffee_computation):
    coffee = 5
    students = coffee_computation(coffee)
    return students
coffee = 1 
result = student_coffee(lambda coffee:coffee+10)
print(result)

the lambda function here is a function but not a function call,look there is no parentheses here.just a function. so the only thing happened is a lambda function def in global frames.

what's more the first example is also called self reference function.

curry function

curry function is a complex function which only take one formal parameter as input.and do it multi times.

you may want to use it when you cooperate with others or re-use a function but only input one parameter, and the other as a default.


def operate(f):
    def operand1(x):
        def operand2(y):
            return f(x,y)
        return operand2
    return operand1
    
sum_operate = operate(sum)
sum_two = sum_operate(2)
sum_tree = sum_two(3)
print(sum_tree)
sum_big = sum_two(2019)
print()sum_big
##expect 
##line one :5
##line two :2021
##line three :

a simple example just as what i explain before the use of curry function

lambda function

lambda function has no turn statement so it's body is also do the return function (effort) ,but can only return simple expression. like def function the frame will not show up util you call it. you can directly call lambda function like (lambda:3)() the output will be number 3.

lambda's father frame rule is the same as a def function here is an example below

x = lambda x:lambda :x
c = x(80)
c
c() 
##expect:
##line one:Function
##line two:80

when the name is bound with the return of a function and the function have no return. try to type the name in command line, result should be nothing. through it is type of none but actually none means nothing. unless you use print(name) to show the type of the diagram name

print_lambda = lambda z: print(z)
one_thousand = print_lambda(1000)
nothing can be saw

LECTURE (1-4)

1.*args

  1. 1.*args reference the argument you pass into the function it could be a single int data or mul-number and etc.
  2. remember use *args but not args when you want use it as a not exactly argument
  3. 2.the star references the every single argument in the tuple so ,you will operate the argument one by one when you try to use the star args rather than using the single args symbol.
  4. 3.
    def type_of_argument(*args):
        print(type(*args))
        print(args)
        print(*args)
    type_of_argument(1,2,3)
    ##expect 
    ##line one :<class 'tuple'>
    ##line two :(1, 2, 3)
    ##line two :1 2 3
  5. 4.look the code above you will have a better understand of the *args(i write the example in python)