Interpreter based on text substitution: Let expression, Boolean type, if expression

Source: Internet
Author: User
Tags expression

Let-expression

Let expressions are used to declare a variable. For example, we are writing a program to simulate the game of craps. A dice has 6 sides. So this program has been using this number 6 times. One day, we suddenly changed our mind to play 12-sided dice. So we have to look up the source code, the inside of the 6 change to 12. For a larger program, this is the beginning of the disaster. Sometimes we miss out on a few 6, and sometimes we change the number of points that are not the dice to 12. This disaster is called "Magic Numbers". The way to avoid magic numbers is generally to declare a variable--such as a variable (a\)--to make this variable equal to 6 (\ (a=6\)). The Let expression for this example contains three elements: variable \ (a\), to give the variable a value of 6, and the program principal \ (m\). I write this let expression as follows: \[({let} \; a \; 6 \; M) \] Generally, define let expressions as follows: \[({let} \; X \; N \; M) \] This is a let expression of a single variable.

Or the example of a roll of dice. One way to avoid magic numbers is to define a function, the parameter of the function is the number of faces of the dice \ (a\), the function body is the program body \ (m\): \[\lambda a.m \] Then call this function with parameter 6: \[(\lambda a.m \; 6) \] The above expression and let table In contrast, we can see that let expression is just a function call syntax sugar: \[({let} \; X \; N \; M) = (\lambda x.m \; N) \] Based on the principle of simplicity, I do not intend to add let expressions to the syntax of the interpreter, but rather to have letting expressions join the language in the form of macros. So another function translate is written to expand the let expression.

The interpreter invokes the translate to do the conversion, and then calls the value-of for evaluation.

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.