我的站点

一个系统软件工程师的随手涂鸦

Date: 二月 15, 2016

Haskell笔记 (18)—— Polymorphic function和oveloaded function

A function is called polymorphic (“of many forms”) if its type contains one or more type variables. E.g.: length :: [a] -> Int

A polymorphic function is called overloaded if its type contains one or more class constraints. E.g.: (+) :: Num a => a -> a -> a.

 

Haskell笔记 (17)—— Value和type

Because Haskell is a purely functional language, all computations are done via the evaluation of expressions (syntactic terms) to yield values (abstract entities that we regard as answers). Every value has an associated type. (Intuitively, we can think of types as sets of values.) Examples of expressions include atomic values such as the integer 5, the character ‘a’, and the function \x -> x+1, as well as structured values such as the list [1,2,3] and the pair (‘b’,4).

Just as expressions denote values, type expressions are syntactic terms that denote type values (or just types). Examples of type expressions include the atomic types Integer (infinite-precision 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, integer pairs).

All Haskell values are “first-class”—they may be passed as arguments to functions, returned as results, placed in data structures, etc. Haskell types, on the other hand, are not first-class. Types in a sense describe values, and the association of a value with its type is called a typing. Using the examples of values and types above, we write typings as follows:

                      5  :: Integer
                     'a' :: Char
                     inc :: Integer -> Integer
                 [1,2,3] :: [Integer]
                 ('b',4) :: (Char,Integer)

The “::” can be read “has type.”

Haskell是一门纯函数式编程语言,所有的计算都是通过对表达式的求值完成的。每个值(value)都有一个相关的类型(type),也可以把类型看做是值的集合。在Haskell中,所有的值都是first-class,而类型却不是。类型和值的集合称之为typing

5  :: Integer

 

Powered by WordPress & Theme by Anders Norén