Syntactic sugars in programming languages

Source: Internet
Author: User
Tags define local

What is the grammatical sugar? As I now understand, if a language does not have a grammar, it can still express some semantics in a more general way. The introduction of this syntax is simply to make the semantics of the expression more convenient. Then this syntax is called the grammar sugar.

Scheme has two keywords lambda, let, my current understanding is that they are grammatical sugar.

Lambda

Lambda is used to define an anonymous function. Sometimes, a function is used only once in one place, and it is completely unnecessary to define a name for it, and then we use lambda to define an anonymous function.

If there is no lambda, then when we define the function, we use the following method:

(define (pi-sum a b)  (define (pi-term x)    (/ 1.0 (* x (+ x 2))))  (define (pi-next x)    (+ x 4))  (sum pi-term a pi-next b))

A small function, such as pi-term and Pi-next, can be used to define an anonymous function entirely through lambda.

(define (pi-sum a b)  (sum     (lambda (x) (/ 1.0 (* x (+ x 2))))    a     (lambda (x) (+ x 4))     b))
Let

Let is used to define local variables, in fact there is no local variables, we can also express the corresponding semantics, but with a let, it is more convenient to define. Since there is no prior experience in functional language programming, there is no knowing what to do without local variables, so look at an example.

Calculation

f(x,y) = x(1+xy)^2 + y(1-y) + (1+xy)(1-y)

We can pass

a = 1 + xyb = 1 - yf(x, y) = xa^2 + yb + ab

To define this function, where a and B are equivalent to local variables, how do we define this function without the local variable syntax?

1 + xy   and  1-y   passed in as the actual parameter:

(define (f x y)  (define (f-helper a b)    (+ (* x (square a))       (* y b)       (* a b)))  (f-helper (+ 1 (* x y))            (- 1 y)))

And if you introduce the syntax sugar, it becomes intuitive and much easier to define:

(define (f x y)  (let ((a (+ 1 (* x y)))        (b (- 1 y)))    (+ (* x (square a))       (* y b)       (* a b))))

As can be seen here, the let scope is divided into two parts, part of the definition of local variables, and the other part of the function body.

Through this part of the study of SICP, I understand what is essentially something, what is the grammatical sugar. Essentially things are definitions of functions and the way they are evaluated, they are simple and powerful, and most of the semantics can be defined by them. Just for convenience, we introduced syntactic sugars to make it easier to express semantics.

The source of the word lambda, also introduced in the book, is a historical reason, derived from the lambda calculus of Alonzo Church. Lambda calculus provides a solid mathematical foundation for studying function definitions and functional applications. I also vaguely remember that in the programming language class, the teacher taught us to use lambda calculus to define natural numbers, addition algorithms, True/false, If/else, and so on. As you can see, these essential things can express very, very many things and function exceptionally strong.

Note: This is my study SICP after the summary and thinking, examples also come from this book.

Syntactic sugars in programming languages

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.