![]() ![]() This behavior of the = operator is the basis of something called 'Pattern matching', which many functional programming languages have, although Erlang's way of doing things is usually regarded as more flexible and complete than alternatives. The comparison will consequently succeed and the variable will keep the value in memory. What this operator does when mixed with variables is that if the left-hand side term is a variable and it is unbound (has no value associated to it), Erlang will automatically bind the right-hand side value to the variable on the left-hand side. ** exception error: no match of right hand side value 48 If they're the same, it returns the value: ![]() The = operator (not the variables) has the role of comparing values and complaining if they're different. It's a correct observation, but the explanation is a bit more complex and depends on the = operator. The first thing these commands tell us is that you can assign a value to a variable exactly once then you can 'pretend' to assign a value to a variable if it's the same value it already has. ** exception error: no match of right hand side value 2 ** exception error: no match of right hand side value 3 The basic behavior of variables can be demonstrated with these 7 expressions (note that variables begin with an uppercase letter): ![]() If you have read the intro to this book, you'll know that variables can't be variable in functional programming. If you want to express integers in other bases than base 10, just enter the number as Base#Value (given Base is in the range 2.36):Īwesome! Erlang has the power of the calculator you have on the corner of your desk with a weird syntax on top of it! Absolutely exciting! Invariable Variablesĭoing arithmetic is alright, but you won't go far without being able to store results somewhere. Note that we can use several operators in a single expression, and mathematical operations obey the normal precedence rules. However, if you want to have the integer-to-integer division, use div, and to have the modulo operator, use rem (remainder). Integers and floating values are pretty much the only types of data Erlang's mathematical operators will handle transparently for you. You should have noticed Erlang doesn't care if you enter floating point numbers or integers: both types are supported when dealing with arithmetic. Open the Erlang shell as described in the previous chapters and let's type them things! This is certainly unusual syntax for most people and it comes from the days Erlang was implemented directly in Prolog, a logic programming language. You can separate expressions with commas, but only the result of the last one will be shown (the others are still executed). In the Erlang shell, expressions have to be terminated with a period followed by whitespace (line break, a space etc.), otherwise they won't be executed. Reading it is strongly advised as it explains the building blocks for all the programs you'll write with Erlang later on. There are a few basic data types built in the language, and as such, this chapter will cover most of them. Erlang is a relatively small and simple language (in the way C is simpler than C++).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |