haskell-98-tutorial.pdf

(835 KB) Pobierz
haskell-tutorial.dvi
of expressions include atomic values such as the integer 5 , the character ’a’ , and the function
integers), Char (characters), Integer->Integer (functions mapping Integer to Integer ), as well
as the structured types [Integer] (homogeneous lists of integers) and (Char,Integer) (character,
103154318.001.png
The \ :: " can be read \has type."
inc can be dened by the single equation:
ration (x4.4.1), with whichwe can declare an explicit typing for inc :
For pedagogical purposes, when we wish to indicate that an expression e
duces," to another expression or value e
) e
so the expression ’a’+’b’ is ill-typed. The main advantage of statically typed languages is well-
it is enforced byHaskell’s lexical syntax. In fact, the case of the other characters matters, too: foo ,
103154318.002.png
integers (e.g. [1,2,3] ), lists of characters ( [’a’,’b’,’c’] ), even lists of lists of integers, etc., are
all members of this family. (Note, however, that [2,’b’] is not avalid example, since there is no
single typethatcontains both 2 and ’b’ .)
[Identiers suchas a above are called type variables, and are uncapitalized to distinguish them
from specic types suchas Int . Furthermore, since Haskell has only universally quantied types,
ply write [a] in the example above. In other words, all type variables are implicitly universally
explaining the principles of polymorphism. The list [1,2,3] in Haskell is actually shorthand for
the list 1:(2:(3:[])) , where [] is the empty list and : is the inx operator that adds its rst
write this list as 1:2:3:[] .
empty list is 0, and the length of a list whose rst element is x and remainder is xs is 1 plus the
length of xs ." (Note the naming convention used here; xs is the plural of x , and should be read
x:xs . In a function application these patterns are matched against actual parameters in a fairly
one element, binding x to the rst element and xs to the rest of the list). If the match succeeds,
containing elements of anytype, for example [Integer] , [Char] ,or [[Integer]] .
) 3
length [[1],[2],[3]] ) 3
Here are two other useful polymorphic functions on lists that will be used later. Function head
103154318.003.png
Unlike length , these functions are not dened for all possible values of their argument. A runtime
others in the sense that the set of values they dene is larger. For example, the type [a] is more
substitution for a . With regard to this generalization ordering, Haskell’s type system possesses two
all instances of the expression". For example, the principal type of head is [a]->a ; [b]->a , a->a ,
or even a are correct types, but too general, whereas something like [Integer]->Integer is too
of examples ( x 4.2.1).
The type being dened here is Bool , and it has exactly twovalues: True and False . Type Bool is
Because of the single constructor, a type like Point is often called a tuple type, since it is essentially
103154318.004.png
Zgłoś jeśli naruszono regulamin