• <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>
            算法學(xué)社
            記錄難忘的征途
            posts - 141,comments - 220,trackbacks - 0

            Haskell's Type System

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

            common basic types

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

            Function Application

            一些函數(shù)需要注意的地方,就是如果你要把一個(gè)函數(shù)的值傳到另一個(gè)函數(shù)里,需要加一個(gè)括號(hào)以改變優(yōu)先級(jí)
             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

            字符串就是一個(gè)字符列表,head 函數(shù)可以返回列表的第一個(gè)元素,tail可以返回列表的除了第一個(gè)元素的所有元素。
            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 命令可以查看一個(gè)函數(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就會(huì)有語法高亮了...
            定義一個(gè)函數(shù)
            1 -- add.hs by figo 05.16.2012
            2 add a b = a + b
            我們可以用:load add.hs來加載這個(gè)函數(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來寫一個(gè)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)
            里面的類型都是自動(dòng)檢測(cè)的... 額... 不用像C++那樣提前聲明
            測(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)性指的是,如果一個(gè)函數(shù)可以把某參數(shù)當(dāng)做任意類型處理。就像C++的template一樣。
            ghci>:type fst
            fst :: (a, b) -> a

            Exercises


            寫一個(gè)函數(shù),功能是返回元素的倒數(shù)第二個(gè)值。
            呃... 只能寫成這樣子了... 好渣... 求大神指點(diǎn)
            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 西月弦 閱讀(1654) 評(píng)論(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》不怎么講語言的……  回復(fù)  更多評(píng)論
              
            # re: Real World Haskell 讀書筆記(二) Types and Functions
            2012-05-17 21:39 | 西月弦
            @OwnWaterloo
            多謝指教... 有沒有什么比較好關(guān)于Haskell或者函數(shù)式編程的書籍推薦一下?
            我是超級(jí)新手阿....  回復(fù)  更多評(píng)論
              
            # 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對(duì)語言的介紹根本不夠看懂它里面提供的代碼……

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

            PS:cppblog的通知郵件被gmail當(dāng)作垃圾了……
              回復(fù)  更多評(píng)論
              
            人妻精品久久久久中文字幕| 久久精品国产清自在天天线| 亚洲国产高清精品线久久| 亚洲精品乱码久久久久久不卡| 久久丫忘忧草产品| 国产高潮国产高潮久久久91| 奇米综合四色77777久久| 久久99热这里只有精品国产| 久久久久久九九99精品| 久久免费视频1| 国产亚洲色婷婷久久99精品91| 亚洲午夜久久久影院| 久久久久久亚洲精品不卡| 久久精品国产影库免费看 | 精品午夜久久福利大片| 欧美粉嫩小泬久久久久久久| 九九久久99综合一区二区| 久久SE精品一区二区| 欧美精品一区二区久久| 久久精品嫩草影院| 久久久无码精品亚洲日韩蜜臀浪潮 | 亚洲精品乱码久久久久久按摩 | 狠狠色丁香久久综合五月| 久久人人爽人人爽人人片AV不| 久久播电影网| 久久精品国产亚洲AV不卡| 久久青草国产手机看片福利盒子| 久久国产色AV免费看| 久久天天躁狠狠躁夜夜avapp| 久久婷婷五月综合97色直播| 国产69精品久久久久APP下载| 久久免费观看视频| 亚洲国产高清精品线久久| 色婷婷久久综合中文久久一本| 久久久久18| 亚洲国产成人久久精品99| 国产精品久久久久免费a∨| 精品无码久久久久国产动漫3d| 武侠古典久久婷婷狼人伊人| 无码国内精品久久综合88| 一本久久a久久精品亚洲|