Haskell was designed to have a terse syntax. For instance, in what follows notice that a ; (semicolon) is almost never required in a Haskell program; the cons operator has been reduced from cons in Scheme to :: in ML to : in Haskell; and the reserved words define, lambda, |, and end do not appear in function declarations and definitions. While programs written in a functional style are already generally more concise than their imperative analogs, “[a]lthough it is difficult to make an objective comparison, Haskell programs are often between two and ten times shorter than programs written in other current languages” (Hutton 2007, p. 4).
Character conversions. The ord and chr functions in the Data.Char module are used for character conversions:
A function within a module (i.e., a collection of related functions, types, and type classes) can be invoked with its fully qualified name (lines 1 and 3) or, once the module in which it resides has been loaded (line 5), with its unqualified name (lines 6, 8, and 10). From within a Haskell program file (or at the read-eval-print prompt of the interpreter), a module can be imported as follows:
A function within a module can also be individually imported:
Selected functions within a module can be collectively imported:
String concatenation. The ++ append operator is used for string concatenation:
In Haskell, a string is a list of characters (i.e., [Char]).
Arithmetic. The infix binary operators +, -, and * only accept two values whose types are members of the Num type class; the prefix unary minus operator negate only accepts a value whose type is a member of the Num type class; the infix binary division operator / only accepts two values whose types are members of the Fractional type class; the prefix binary division operator div only accepts two values whose types are members of the Integral type class; and the prefix binary modulus operator mod only accepts two values whose types are members of the Integral type class.
Comparison. The infix binary operators == (equal to), <, >, <=, >=, and /= (not equal to) compare two integers, floating-point numbers, characters, or strings:
Boolean operators. The infix operators || (or), && (and), and not are the or, and, and not boolean operators with their usual semantics. The operators || and && use short-circuit evaluation (or lazy evaluation, as discussed in Chapter 12):
Conditionals. Use if–then–else expressions:
There is no if expression without an else because all expressions must return a value.
Comments.
Single-line comments:
Multi-line comments:
Nested multi-line comments:
3.147.126.211