zoukankan      html  css  js  c++  java
  • 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.
    

      

  • 相关阅读:
    msql 触发器
    微信模板消息推送
    微信朋友朋友圈自定义分享内容
    微信退款
    异步调起微信支付
    微信支付
    第一次作业
    【Linus安装MongoDB及Navicat】
    【前端】ES6总结
    【开发工具】Pycharm使用
  • 原文地址:https://www.cnblogs.com/long123king/p/3844327.html
Copyright © 2011-2022 走看看