• Represent code in math equations


    [comment]: # Represent code in math equations

    Introduce

    The article shows a way to use math equations to represent code's logical.

    Key ideas

    logical first

    Get readers to see the logical first, so prefer to keep a 'where' section to describe variables separately.

    Pure function conception

    I am thinking, in a function, whether we can just use

    • Only has a 'if/else' statement
    • Only has a 'switch/case' statement
    • Only has a 'for' statement
    • Only has a 'foreach' statement
    • Only has a 'while' statement
    • Only has statements without 'if/else', 'switch/case', 'for', 'foreach' or 'while'.

    Decision Structures

    • Decision structure - simple

    [result = egin{cases} statement_1, & {condition}_1 \ statement_2, & {condition}_2 \ statement_3 & ext{otherwise} end{cases} ]

    • Decision structure (if ... then) - if, else

    [result = egin{cases} statement_1, & ext{if} {condition}_1 \ statement_2, & ext{if} {condition}_2 \ statement_3 & ext{otherwise} end{cases} ]

    • Decision structure (if ... then) - if, else with multiple lines

    [ ext{if (constant-expression)} \ { \ qquad statement_1 \ qquad statement_2 \ } ]

    • Decision structure(selection) - switch, case, default, goto, break

    [result = (switch : {expression}) egin{cases} statement_1, & ext{case} {constant-expression}_1 \ statement_2, & ext{case} {constant-expression}_2 \ statement_3 & ext{otherwise} end{cases} ]

    • Loop structure (conditional) - while, continue
      First check the condition to determine if enter the loop.

    [{while}_ ext{condition} ext{statement} ]

    • Loop structure (conditional) - do, while, continue
      First execute the statement, then check the condition to determine if enter the next loop.

    [{while}^ ext{condition} ext{statement} ]

    • Loop structure (iteration) - for

    [{for}_ ext{i = 1}^ ext{ i < n} ext{statement} ]

    • Loop structure (iteration) - for with step

    [{for}_ ext{i = 1}^ ext{ i < n; i += 2} ext{statement} ]

    • Loop structure (iteration) - foreach

    [{foreach}_ ext{item}^ ext{items} ext{statement} ]

    Keywords

    • break
      (igotimes)
    • continue
      (igodot)

    function

    [ ext{(variable_1 [, ..., variable_n]) function_name(parameter_1 [, ..., parameter_k])} = \ [{] \ qquad statement_1 \ qquad statement_2 \ qquad ... \ qquad statement_n \ where \ qquad variable_1 = ... \ qquad ... \ qquad variable_m = ... \ [}] ]

    function in one body

    [ ext{(variable_1 [, ..., variable_n]) function_name(parameter_1 [, ..., parameter_l])} = \ qquad egin{cases} statement_1 \ statement_2 \ where \ qquad variable_1 = ... \ qquad ... \ qquad variable_m = ... \ end{cases} \ ]

    class

    [ ext{class class_name[(inherited class name)]} = \ [{] \ [where] \ qquad field_1 = ... \ qquad ... \ qquad field_m = ... \ [functions]\ qquad function_1 \ qquad function_2 \ qquad ... \ qquad function_n \ [}] ]

    Comment - single line

    [ ext{ # input some comments} \ ext{ : input some comments} ]

    Comment - multiple lines

    [''' \ ext{ this is} \ ext{ multiple lines comments} \ ''' ]

    Comment - multiple lines 2

    [""" \ ext{ this is} \ ext{ multiple lines comments} \ """ ]

    Sample

    [ ext{class firstclass} = \ qquad field1 = 1 \ qquad field2 = true \ qquad \ qquad func1(param1, y) \ qquad { \ qquad qquad var1, var2 = func2(1, 2) = \ qquad qquad func3(var1) \ qquad qquad y^{(mean)} = f4(y) \ qquad where \ qquad qquad var1 ext{ # return value 1}\ qquad qquad var2 ext{ # return value 2}\ qquad qquad y ext{ : result data of training data.} \ qquad qquad y^{(mean)} ext{ : the arithmetic mean along the y.} \ qquad } \ qquad \ qquad (result1, result2) func2(param1, param2) = \ qquad { \ qquad qquad result1 = egin{cases} 1, & ext{param1 > 0} \ -1 & ext{otherwise} end{cases} \ qquad qquad var2 = 100 ext{ # it is a variable defined in body. }\ qquad qquad result2 = var1 + var2 + param2 \ qquad where \ qquad qquad result1 ext{ # return value 1} \ qquad qquad result2 = 0 ext{ # return value 2} \ qquad qquad var1 = 10 ext{ # it is a variable. } \ qquad } \ qquad \ qquad func3(param1) = \ qquad egin{cases} ext{# do something ...} \ ext{# do something ...} \ ext{# do something ...} \ ext{# do something ...} \ ext{# do something ...} \ ext{# do something ...} \ end{cases} \ qquad \ qquad f4(y) = frac{sum(y)}{count(y)} \ qquad \ qquad ]

    References

  • 相关阅读:
    HTML5
    HTML5
    HTML5
    HTML5
    HTML5
    HTML5
    HTML5
    HTML5
    HTML5
    53.Maximum Subarray
  • 原文地址:https://www.cnblogs.com/steven-yang/p/5711167.html
Copyright © 2020-2023  润新知