• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            算法學社
            記錄難忘的征途
            posts - 141,comments - 220,trackbacks - 0

            Haskell's Type System

                Haskell的數(shù)據(jù)類型有三個特點:
                1. 強類型
                2. 靜態(tài)類型
                3. 可自動推斷
                額,我按照自己的理解解釋一下吧...
                強類型不支持自動類型轉(zhuǎn)換,為了讓代碼可靠一些...
                靜態(tài)類型是每個表達式或者變量的類型都是靜態(tài)不變的...
                Haskell可以自動檢測表達式中常數(shù)的數(shù)據(jù)類型...

            common basic types

               char: Unicode字符
               bool:True & False
               Int : 在32位機器上是32位,在64位機器上是64位
               Integer: 高精度整形
               Double:64位浮點型

            Function Application

            一些函數(shù)需要注意的地方,就是如果你要把一個函數(shù)的值傳到另一個函數(shù)里,需要加一個括號以改變優(yōu)先級
             1 ghci>odd 3
             2 True
             3 ghci>even 6
             4 True
             5 ghci>compare (sqrt 2) 1
             6 GT
             7 ghci>compare (sqrt 2) 1 == GT
             8 True
             9 ghci>compare sqrt 2 1 == GT
            10 
            11 <interactive>:16:1:
            12     The function `compare' is applied to three arguments,
            13     but its type `a0 -> a0 -> Ordering' has only two
            14     In the first argument of `(==)', namely `compare sqrt 2 1'
            15     In the expression: compare sqrt 2 1 == GT
            16     In an equation for `it': it = compare sqrt 2 1 == GT

            Composite Data Types: Lists and Tuples

            字符串就是一個字符列表,head 函數(shù)可以返回列表的第一個元素,tail可以返回列表的除了第一個元素的所有元素。
            1 ghci>head "figo"
            2 'f'
            3 ghci>tail "figo"
            4 "igo"
            5 ghci>tail [1,2,3]
            6 [2,3]

            還有另一種類型叫做“元組”。類似于C++的pair,連用法都極其相似。

            ghci>snd (19910905,"hanfei")
            "hanfei"
            ghci>fst (19910905,"hanfei")
            19910905

            Function Types and Purity

            :type 命令可以查看一個函數(shù)傳入的參數(shù)類型和返回的參數(shù)類型
            Haskell的函數(shù)只允許調(diào)用傳入的參數(shù),禁止使用全局變量。 這樣的函數(shù)叫做“純函數(shù)”。(略感坑爹)
            1 ghci>:type lines
            2 lines :: String -> [String]
            3 ghci>lines "figo\nis\nstupid\nhahaha"
            4 ["figo","is","stupid","hahaha"]

            Haskell Soucre Files, and Writing Simple Functions

            Haskell源文件的后綴是.hs,這樣的話vim就會有語法高亮了...
            定義一個函數(shù)
            1 -- add.hs by figo 05.16.2012
            2 add a b = a + b
            我們可以用:load add.hs來加載這個函數(shù)
            1 ghci>:load add.hs
            2 [1 of 1] Compiling Main             ( add.hs, interpreted )
            3 Ok, modules loaded: Main.
            4 ghci>:type add
            5 add :: Num a => a -> a -> a
            6 ghci>add 1 2
            7 3

            Conditional Evaluation

            利用if來寫一個drop函數(shù):
            1 -- myDrop.hs by figo 05.16.2012
            2 myDrop n xs = if n <= 0 || null xs
            3             then xs
            4             else myDrop (n-1)(tail xs)
            里面的類型都是自動檢測的... 額... 不用像C++那樣提前聲明
            測試一下剛寫的東東
            1 ghci>:load myDrop.hs 
            2 [1 of 1] Compiling Main             ( myDrop.hs, interpreted )
            3 Ok, modules loaded: Main.
            4 ghci>myDrop 2 "figo"
            5 "go"
            6 ghci>myDrop 1 ["hanfei","figo","hello"]
            7 ["figo","hello"]

            Polymorphism in Haskell

            Haskell的多態(tài)性指的是,如果一個函數(shù)可以把某參數(shù)當做任意類型處理。就像C++的template一樣。
            ghci>:type fst
            fst :: (a, b) -> a

            Exercises


            寫一個函數(shù),功能是返回元素的倒數(shù)第二個值。
            呃... 只能寫成這樣子了... 好渣... 求大神指點
            1 -- lastButOne.hs by figo 05.16.2012
            2 lastButOne xs = if(length (xs) <= 2)
            3                 then head xs
            4                 else lastButOne (tail xs)
            5 
            posted on 2012-05-16 19:59 西月弦 閱讀(1645) 評論(3)  編輯 收藏 引用 所屬分類: 讀書筆記(Haskell)

            FeedBack:
            # re: Real World Haskell 讀書筆記(二) Types and Functions
            2012-05-16 23:45 | OwnWaterloo
            lastButOne xs = case xs of { [] -> Nothing ; y:[] -> Nothing ; x:y:[] -> Just x ; x:xs -> lastButOne xs}

            或者:
            lastButOne [] = Nothing
            lastButOne y:[] = Nothing
            lastButOne x:y:[] = Just x
            lastButOne x:xs = lastButOne xs


            《real world haskell》不怎么講語言的……  回復  更多評論
              
            # re: Real World Haskell 讀書筆記(二) Types and Functions
            2012-05-17 21:39 | 西月弦
            @OwnWaterloo
            多謝指教... 有沒有什么比較好關(guān)于Haskell或者函數(shù)式編程的書籍推薦一下?
            我是超級新手阿....  回復  更多評論
              
            # re: Real World Haskell 讀書筆記(二) Types and Functions
            2012-05-19 11:31 | OwnWaterloo
            @西月弦
            Haskell我也是新手,感覺收獲最大的是 http://learnyouahaskell.com/
            其次是 http://www.haskell.org/tutorial/ 內(nèi)容不多,但信息量很大……
            real world haskell對語言的介紹根本不夠看懂它里面提供的代碼……

            函數(shù)式編程沒專門看過什么數(shù)據(jù)……

            PS:cppblog的通知郵件被gmail當作垃圾了……
              回復  更多評論
              
            一本一本久久a久久综合精品蜜桃 一本一道久久综合狠狠老 | 久久精品国产一区二区三区日韩| 亚洲精品无码久久久久| 久久久无码一区二区三区| 91久久精品91久久性色| 7国产欧美日韩综合天堂中文久久久久| 狠狠综合久久综合中文88 | 久久久久99精品成人片欧美| 丁香狠狠色婷婷久久综合| 久久久久久青草大香综合精品| 久久天天婷婷五月俺也去| 国产成人精品久久免费动漫| 久久这里只有精品首页| 波多野结衣久久一区二区| 久久亚洲精品无码AV红樱桃| 久久精品国产亚洲AV不卡| 亚洲国产精品久久久天堂 | 亚洲精品97久久中文字幕无码| 亚洲欧美日韩久久精品第一区| 品成人欧美大片久久国产欧美| 无码人妻久久一区二区三区免费| 国产精品丝袜久久久久久不卡| 久久亚洲精精品中文字幕| 色欲综合久久躁天天躁| 精品99久久aaa一级毛片| 久久久久久午夜成人影院| 久久精品aⅴ无码中文字字幕不卡| 99久久亚洲综合精品网站| 色婷婷久久综合中文久久蜜桃av | 一日本道伊人久久综合影| 国内精品久久久久久中文字幕| 国产精品青草久久久久婷婷| 亚洲AV日韩精品久久久久久久| 午夜视频久久久久一区| 久久亚洲天堂| 色婷婷噜噜久久国产精品12p | 国产高潮国产高潮久久久91| 狠狠干狠狠久久| 91视频国产91久久久| 2021精品国产综合久久| 国产精品美女久久久|