Throughout
the tutorial, the lines after the “>” sign is a line of jaskell code which
represents an exprssion.
The
following lines without “>” at the beginning is the result of the previous
expression.
Boolean
Boolean value is either true or false. The not function can be used to invert a boolean variable.
Ø
not true
false
Comparison operators such as ==, >, <, !=, <>, >=, <= all result in a boolean value.
Logical operators and and or can be applied to boolean values.
Ø
1==2 or 1>0
true
Ø
1>0 and 1<1
false
and and or are short-circuited. i.e.
Ø
true or fail
true
will not evaluate fail at all.
Ø
false and fail
false
will not evaluate fail either.
Integer
Integers in Jaskell can be of arbitrary length.The following are valid integer literals:
1 0 15 1098764900494576343877465
The following arithmetic operators can be applied to integer:
+ - * / %
Number
Numbers in Jaskell include integers and numbers with digits to the right of the decimal point. The following are valid number literals:
1 15 1.111 3.1415926
Numbers in Jaskell are fixed-point numbers.
Character
Jaskell supports character data. A character literal is enclosed in a pair of single quotes. backslash is used to escape single quote character, backslash character and other special characters such as horizontal tab character (\t), line feed character (\r), return character (\n) etc..
‘a’ ‘ ‘
‘\\’ ‘\’’
String
A Jaskell string is a sequence of characters. A string literal can be represented by a list of characters enclosed between a pair of double quote characters. Similar to character literal, backslash is used to escape, For example::
Ø
“abc”
abc
Ø
“I said:\” that’s what I said\””, “\tHi, Sam, \r\n\t\t This is
me.\r\n”
I said:”that’s
what I said”,
Hi, Sam,
This
is me
Jaskell string can span more than 1 line. The line break characters is also part of the string literal value.
For example:
Ø
“Hi, Sam,
Ø
Tthis is me
Ø
“
Hi, Sam,
This is me
Jaskell also supports free-style string literal (here-docs) where no escaping is required. <<< and >>> are used to indicate such string literal.
Here’s the gory details:
<<< and the word directly after it is treated as the opening quote of the string literal;
the same word and the >>> directly after it is treated as the closing quote of the string literal.
a “word” is defined as (a-zA-Z0-9_)*
i.e. a word is an integer or any valid ascii variable name or empty.
Plus, the same line break or the non-alphanum character directly following the open quote word has to appear directly before the closing quote word to indicate the end of this string literal.
Exceptions apply to ‘<’, ‘>’, ‘(‘, ‘)’, ‘[‘, ‘]’, ‘{‘ and ‘}’. For these special characters, ‘>’ closes ‘<’; ‘(‘ closes ‘)’, ‘[‘ closes ‘]’, ‘{‘ closes ‘}’ and vice versa.
For example:
Ø
<<<|This is my string!|>>>
This is my string!
Ø
<<<
Ø
This is my string with “, \, <<< and >>>!
Ø
>>>
This is my string with “, \, <<< and >>>!
Ø
<<<STRING[This is my string!]STRING>>>
This is my string!
The free style string literal is ideal for embedding a chunk of text in jaskell script.
For example, you can embed a chuck of jaskell code inside a jaskell string:
Ø
<<<MYJASKELL
Ø
this is my jaskell code:
Ø
<<<STRING[This is my string!]STRING>>>
Ø
MYJASKELL>>>
this is my jaskell
code:
<<<STRING[This is my string!]STRING>>>
String Interpolation
String interpolation allows jaskell expressions embedded in a string literal to be evaluated and the result added in the string value in place of the expression.
Interpolatec sub-expression is indicated by a ‘$’ character.
Jaskell supports two forms of interpolation:
1. a variable name directly following the ‘$’ character,
Ø “hello $name” where name=”Tom”..
hello Tom
2. An expression enclosed in a pair of curly braces directly following the ‘$’ character.
Ø
“Total cost = ${resource_cost + outsourcing_cost} where
Ø
resource_cost = 10000;
Ø
outsourcing_cost=100000;
Ø
..
Total cost=110000
If a ‘$’ is not followed by ‘{‘ or a variable name, it is treated as a real ‘$’ character.
Since the ‘$’ character is used to indicate string interpolation, sometimes it needs to be escaped when we want a real ‘$’ character.
Ø
“The code is a$b” where
b=10..
The code is a10
Ø “The code is a$$b” where b=10..
The code is a$b
String interpolation starts after the string literal is parsed and the escapes by ‘\’ is done.
Ø
“my string is ${\”my string\”}”
my string is “my string”
The free style string literal (here-docs) indicated by “<<<” and “>>>” is also free of interpolation.
Ø
<<<|$a and ${a} are not interpolated.|>>>
$a and ${a} are not
interpolated.
To enable interpolation inside here-docs, use “$$<” and “>$$” instead of “<<<” and “>>>”.
The rules of “$$<” is same as “<<<” except it supports interpolation.
Ø
$$<
Ø
Hi $your_name,
Ø
My name is
$my_name. How are you?
Ø
Sincerely,
Ø
$my_name
Ø
>$$ where
Ø
your_name = “Sam”;
Ø
my_name = “John”;
Ø
..
Hi Sam,
My name
is John. How are you?
Sincerely,
John
String operation
String supports the following operation:
1. Concatenation. Two strings can be concatenated with the “+” operator.
Ø
“hello” + “ world”
hello world
2. Get the length of the array with operator “#”.
Ø
#“hello”
5
3. Get the nth character in a string with operator “@”
Ø
“hello”@0
h
Comparison between strings follows dictionary order.
Ø
“b” > “abc”
true
Identifier (Variable)
A jaskell identifier starts with any alpha character or underscore, and followed by 0 or more alphanumeric characters, finally it may end with 0 or more single quote characters. The following are valid Jaskell identifiers:
tom ABC X1 myvar’ myvar’’ __this_var__ __this_var_’
The underscore character ‘_’ is reserved for other use. (wildcard in pattern match). We’ll cover it in later sections.