haskell基本语法
定义新类型
data EmployeeInfo = Employee Int String String [String] deriving(Read, Show, Eq, Ord)
EmployeeInfo是新的类型名称,或者说是类型标识
Employee是构造函数的名称
*Main> :t Employee Employee :: Int -> String -> String -> [String] -> EmployeeInfo
这个函数就是将输入的各个参数转换为一个EmployeeInfo的对象。
除此之外,deriving列举了EmployeeInfo支持的操作,比如show,read, 比较相等,以及比较大小的操作。
*Main> :t read read :: Read a => String -> a *Main> let thisGuy = read "Employee 12345 \"Daniel King\" \"Boss X\" [\"Colleague A\",\"Colleague B\"]"::EmployeeInfo *Main> :t thisGuy thisGuy :: EmployeeInfo *Main> thisGuy Employee 12345 "Daniel King" "Boss X" ["Colleague A","Colleague B"]
如果只有构造函数,而没有参数,那么与enum的作用是相同的。
或者可以认为“函数”与“值”是相同的概念。
Prelude> data Init = FullInit | PartInit deriving (Show, Read, Eq) Prelude> :t FullInit FullInit :: Init Prelude> :t PartInit PartInit :: Init Prelude> let a = PartInit Prelude> :t a a :: Init Prelude> let b = FullInit Prelude> a == b False Prelude> a == PartInit True Prelude>
Eq Equality operators == and /= Ord Comparison operators < <= > >=; min, max, and compare. Enum For enumerations only. Allows the use of list syntax such as [Blue .. Green]. Bounded Also for enumerations, but can also be used on types that have only one constructor. Provides minBound and maxBound as the lowest and highest values that the type can take. Show Defines the function show, which converts a value into a string, and other related functions. Read Defines the function read, which parses a string into a value of the type, and other related functions.
参考:http://en.wikibooks.org/wiki/Haskell/Classes_and_types
Prelude Control.Monad> :t return return :: Monad m => a -> m a Prelude Control.Monad> :t (>>=) (>>=) :: Monad m => m a -> (a -> m b) -> m b
A monad is a datatype that has two operations: >>= (aka bind) and return (aka unit). return takes an arbitrary value and creates an instance of the monad with it. >>= takes an instance of the monad and maps a function over it. (You can see already that a monad is a strange kind of datatype, since in most programming languages you couldn't write a function that takes an arbitrary value and creates a type from it. Monads use a kind of parametric polymorphism .) In Haskell notation, the monad interface is written class Monad m where return :: a -> m a (>>=) :: forall a b . m a -> (a -> m b) -> m b These operations are supposed to obey certain "laws", but that's not terrifically important: the "laws" just codify the way sensible implementations of the operations ought to behave (basically, that >>= and return ought to agree about how values get transformed into monad instances and that >>= is associative). Monads are not just about state and IO: they abstract a common pattern of computation that includes working with state, IO, exceptions, and non-determinism. Probably the simplest monads to understand are lists and option types: instance Monad [ ] where [] >>= k = [] (x:xs) >>= k = k x ++ (xs >>= k) return x = [x] instance Monad Maybe where Just x >>= k = k x Nothing >>= k = Nothing return x = Just x where [] and : are the list constructors, ++ is the concatenation operator, and Just and Nothing are the Maybe constructors. Both of these monads encapsulate common and useful patterns of computation on their respective data types (note that neither has anything to do with side effects or IO). You really have to play around writing some non-trivial Haskell code to appreciate what monads are about and why they are useful.