• Programming In Haskell chapter3


    What is a type ?

    a type is a name for a colletion of related values.

    for example , in haskell the basic type 

    Bool

    contains the two logical values:

    False  True

    ----

    all type errors are found at compile time , which makes programs safer and faster by removing the need for type checks at run time.

    ----

    List Types

    a list is sequence of values of the same type.

    ----

    Tuple Types

    a typle is a sequence of vaues of different type.

    ----

    Function Types

    a function is a mapping from values of one type to values of another type.

    Func<T>  =>  () -> T

    Func<S,T> => S -> T

    Action<T> => T-> IO()

    如果一个函数的参数是必须同时需要的,那么就用tuple  如果不是必须的同时接受,那么就可以应用curry 技术。e.g. 

    add (x,y) = x+y

      add = \(x,y) -> x+y

    add' x y = x+y

      add' = \ x y -> x+y

    Functions that take their arguments one at a time are called curried functions ,celebrating the work of Haskell Curry on such functions.

    Why is Currying Useful ?

    Curried functions are more flexible than functions on tuples ,because useful functions can often be made by partially applying a curried function.

    Polymorphic Functions (generic function in c# )

    a function is called polymorphic ("of many forms ") if its type contains one or more type variables.

    zip:: ([a],[b])->[(a,b)]

    IE..<R> Zip<T,S,R> ( IE..<T> xs,IE..<S> ys ,Func<T,S,R> f)

    type Class

    sum:: Num a => [a] -> a

    note: Constrained type variables can be instantiated to any types that satisfy the constraints

    sum [1,2,3]

    sum [1.1,2.2,3.3]

    sum ['a','b','c'] error!

    in c# Num can be interface.

  • 相关阅读:
    SDUT 2109 找女朋友
    Instant Complexity(模拟,递归)
    Lucky and Good Months by Gregorian Calendar(模拟)
    Wall(Graham算法)
    Beauty Contest(graham求凸包算法)
    A Round Peg in a Ground Hole(判断是否是凸包,点是否在凸包内,圆与多边形的关系)
    Pie(二分)
    Expanding Rods(二分)
    Fishnet(计算几何)
    Building a Space Station(kruskal,说好的数论呢)
  • 原文地址:https://www.cnblogs.com/jiangzhen/p/2367257.html
Copyright © 2020-2023  润新知