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 =
    \begin{cases}
    statement_1, & {condition}_1 \\
    statement_2, & {condition}_2 \\
    statement_3 & \text{otherwise}
    \end{cases}
    \]

  • Decision structure (if ... then) - if, else
    \[
    result =
    \begin{cases}
    statement_1, & \text{if}\ {condition}_1 \\
    statement_2, & \text{if}\ {condition}_2 \\
    statement_3 & \text{otherwise}
    \end{cases}
    \]

  • Decision structure (if ... then) - if, else with multiple lines
    \[
    \text{if (constant-expression)} \\
    \{ \\
    \qquad statement_1 \\
    \qquad statement_2 \\
    \}
    \]

  • Decision structure(selection) - switch, case, default, goto, break
    \[
    result = (switch : {expression})
    \begin{cases}
    statement_1, & \text{case}\ {constant-expression}_1 \\
    statement_2, & \text{case}\ {constant-expression}_2 \\
    statement_3 & \text{otherwise}
    \end{cases}
    \]

  • Loop structure (conditional) - while, continue
    First check the condition to determine if enter the loop.
    \[
    {while}_\text{condition} \text{statement}
    \]

  • Loop structure (conditional) - do, while, continue
    First execute the statement, then check the condition to determine if enter the next loop.
    \[
    {while}^\text{condition} \text{statement}
    \]

  • Loop structure (iteration) - for
    \[
    {for}_\text{i = 1}^\text{ i < n} \text{statement}
    \]

  • Loop structure (iteration) - for with step
    \[
    {for}_\text{i = 1}^\text{ i < n; i += 2} \text{statement}
    \]

  • Loop structure (iteration) - foreach
    \[
    {foreach}_\text{item}^\text{items} \text{statement}
    \]

Keywords

  • break
    \(\bigotimes\)
  • continue
    \(\bigodot\)

function

\[
\text{(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

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

class

\[
\text{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

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

Comment - multiple lines

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

Comment - multiple lines 2

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

Sample

\[
\text{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 \text{ # return value 1}\\
\qquad \qquad var2 \text{ # return value 2}\\
\qquad \qquad y \text{ : result data of training data.} \\
\qquad \qquad y^{(mean)} \text{ : the arithmetic mean along the y.} \\
\qquad \} \\
\qquad \\
\qquad (result1,\ result2)\ func2(param1,\ param2) = \\
\qquad \{ \\
\qquad \qquad result1 =
\begin{cases}
1, & \text{param1 > 0} \\
-1 & \text{otherwise}
\end{cases} \\
\qquad \qquad var2 = 100 \text{ # it is a variable defined in body. }\\
\qquad \qquad result2 = var1 + var2 + param2 \\
\qquad where \\
\qquad \qquad result1 \text{ # return value 1} \\
\qquad \qquad result2 = 0 \text{ # return value 2} \\
\qquad \qquad var1 = 10 \text{ # it is a variable. } \\
\qquad \} \\
\qquad \\
\qquad func3(param1) = \\
\qquad \begin{cases}
\text{# do something ...} \\
\text{# do something ...} \\
\text{# do something ...} \\
\text{# do something ...} \\
\text{# do something ...} \\
\text{# do something ...} \\
\end{cases} \\
\qquad \\
\qquad f4(y) = \frac{sum(y)}{count(y)} \\
\qquad \\
\qquad
\]

References

Represent code in math equations的更多相关文章

  1. C# Math.Round

    不能直接调用Math.Round方法的,这可和Java的不一样哦Math.Round这个函数的解释是将值按指定的小数位数舍入,并不就是四舍五入.这种舍入有时称为就近舍入或四舍六入五成双 C# code ...

  2. How to implement an algorithm from a scientific paper

    Author: Emmanuel Goossaert 翻译 This article is a short guide to implementing an algorithm from a scie ...

  3. Markdown Example

    An h1 header Paragraphs are separated by a blank line. 2nd paragraph. Italic, bold, and monospace. I ...

  4. CS190.1x-ML_lab4_ctr_student

    这次lab主要主要是研究click-through rate (CTR).数据集来自于Kaggle的Criteo Labs dataset.相关ipynb文件见我github. 作业分成5个部分:on ...

  5. 01 Go 1.1 Release Notes

    Go 1.1 Release Notes Introduction to Go 1.1 Changes to the language Integer division by zero Surroga ...

  6. Hexo搭建个人博客及next主题基本配置

    前言 国内一些免费的博客平台比如CSDN.博客园都已经很成熟,功能齐全,已经可以满足我们的需求,帮助我们记录学习过程遇到的问题,还能分享帮助其他人解决问题.为什么还要自己动手去搭建博客呢?首先写博客是 ...

  7. NLNet-Theme for cnblogs

    这篇文档仅作为markdown在cnblogs中的渲染效果展示.第一部分NLNet' Samples为自定义内容的效果展示.NOTE 第二.三部分的Markdown Reference(From Ty ...

  8. 百闻不如一试——公式图片转Latex代码

    写博客时,数学公式的编辑比较占用时间,在上一篇中详细介绍了如何在Markdown中编辑数学符号与公式. https://www.cnblogs.com/bytesfly/p/markdown-form ...

  9. 比特币_Bitcoin 简介

    2008-11   Satoshi Nakamoto  Bitcoin: A Peer-to-Peer Electronic Cash System http://p2pbucks.com/?p=99 ...

随机推荐

  1. Java 10大精华文章收集001

    Java语言与JVM中的Lambda表达式全解 Lambda表达式是自Java SE 5引入泛型以来最重大的Java语言新特性,本文是2012年度最后一期Java Magazine中的一篇文章,它介绍 ...

  2. Android-NDK编译:cocos2d-x(三) eclipse 导入工程

    NDK 编译后,用eclipse导入cocos2d-x工程 菜单[File]-->[New]-->[Project] ,弹出New Project 对话框 窗口下方 选 [Android] ...

  3. JAVA IO NIO

    http://www.cnblogs.com/handsome1013/p/4882862.html http://www.cnblogs.com/dolphin0520/ http://www.cn ...

  4. 看上去很美 国内CDN现状与美国对比

    CDN的理想与现实 多年以前,当<Kingdom of Heaven>这部史诗电影发行的时候,中国的影迷使用电驴和BT来寻找种子,而那个时候,高清也才刚刚进入电影领域,我的同事不惜用自家的 ...

  5. mysql int(1) 与 tinyint(1) 有什么区别?

    From: http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/08/25/2153016.html mysql int(1) tinyint ...

  6. 用gameMaker做个小游戏

    看下面这个课程链接,半小时学会 http://study.163.com/course/courseMain.htm?courseId=352004#/courseMain 这是我做的:http:// ...

  7. LevelDB(v1.3) 源码阅读之 Arena(内存管理器)

    LevelDB(v1.3) 源码阅读系列使用 LevelDB v1.3 版本的代码,可以通过如下方式下载并切换到 v1.3 版本的代码: $ git clone https://github.com/ ...

  8. 使用UIKit制作卡牌游戏(一)ios游戏篇

    转自朋友Tommy 的翻译,自己只翻译了第三篇教程. 译者: Tommy | 原文作者: Matthijs Hollemans写于2012/06/29 原文地址: http://www.raywend ...

  9. SmartJS 系列规划分享和背景介绍

    发布了smartjs后,有朋友问:“没看懂究竟是干嘛的”.唉,打击了,每次我都想高唱其实你不懂我的心. 今天把相关的东西都整理了一遍,给大家介绍一下.里面绝大多数都已经实现过,有些则是有新的思路重做或 ...

  10. zoj 3261 Connections in Galaxy War

    点击打开链接zoj 3261 思路: 带权并查集 分析: 1 题目说的是有n个星球0~n-1,每个星球都有一个战斗值.n个星球之间有一些联系,并且n个星球之间会有互相伤害 2 根本没有思路的题,看了网 ...