C.5 Essential Operators and Expressions

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 set of 11 code lines in Haskell with the functions o r d and c h r.
    Description

    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 set of five code lines in Haskell for importing a module.
    Description

    A function within a module can also be individually imported:

    A set of seven code lines in Haskell for importing a function individually within a module.
    Description

    Selected functions within a module can be collectively imported:

    A set of three code lines in Haskell for importing selected functions collectively within a module.
    Description
    Continuation of the code in Haskell for importing selected functions collectively within a module, consisting of two lines.
    Description
  • String concatenation. The ++ append operator is used for string concatenation:

    A set of two code lines in Haskell with the append operator.
    Description

    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.

    A set of six code lines in Haskell with various arithmetic operators.
    Description
  • Comparison. The infix binary operators == (equal to), <, >, <=, >=, and /= (not equal to) compare two integers, floating-point numbers, characters, or strings:

    A set of six code lines in Haskell with infix binary operators for comparison.
    Description
  • 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):

    A set of six code lines in Haskell with infix Boolean operators.
    Description
  • Conditionals. Use ifthenelse expressions:

    A set of two code lines in Haskell that uses the if-then-else expression.
    Description

    There is no if expression without an else because all expressions must return a value.

  • Comments.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.126.211