Post 8 – Conditional Expression & Let Bindings

Tram Ho

After rewriting the sumNumberArray program in JS , I realized that the Pattern Matching syntax is very neat but also has certain limitations. That is, this syntax cannot do quantitative testing of the data with the comparison statements < , == , > , etc. So we will need to note another syntax that can be used. The name is a Conditional Expression .

Conditional Expression

The name sounds familiar already. Definitely has something to do with if..else . However, we should note that this is an expression , not a structure . The logic that works here is based on the condition of the data to choose the return value, not the statement to execute.

http://localhost:8000/src/Main.elm

Let Bindings

In addition, we have added a binding syntax to be able to give a concise return expression with completely new identifier names relative to the parameters – then attach explanatory definitions to the names. that identifier.

http://localhost:8000/src/Main.elm

In fact, this syntax looks pretty Imperative , but is the only binding option in Elm . In another language called Haskell , people often use a syntax that has the opposite interpretation like this:

Deductive writing with the resulting expression placed on the same line as the sub-program definition expression on the left side will make it possible to extract the general definition information of the sub-program very quickly in the first line. Then, if absolutely necessary, we can continue reading the binding that explain the elements that appear in the original expression.

Well, in any environment where there is a tool, we will take advantage of that tool. Just use the let..in syntax a lot and you will get used to it. Just read the expression in.. first. However, for that reason, we must be careful not to write multiple let..in expressions in the same definition code. Because of this, it will be difficult to generalize information when reading. Ideally, we should have only one pair of let..in and possibly multiple expressions of the same level after the let to account for the essentials.

http://localhost:8000/src/Main.elm

Conditional Expression in JS

(unpublished) [Declarative Programming + Elm] Lesson 9 – …

Share the news now

Source : Viblo