Real World Haskell學習篇-第1章: 入門

来源:http://www.cnblogs.com/burnet/archive/2016/05/05/5462723.html
-Advertisement-
Play Games

1. 初識解釋器ghci 1.1 查看幫助: :? 1.2 修改提示符: :set prompt ghci>>> 1.3 加自己指定模塊: :module + Data.Ratio 2. 基本交互 2.1 基本算術運算 中綴表達式: 首碼表達式: 2.2 算術中的負數 -8其實並不是直接表示負數8, ...


1. 初識解釋器ghci

  1.1  查看幫助: :?

  1.2  修改提示符: :set prompt ghci>>>

  1.3  加自己指定模塊: :module + Data.Ratio

2. 基本交互

  2.1 基本算術運算

    中綴表達式:

1 ghci>>> 3 ^ 3
2 27
3 ghci>>> 2 + 4
4 6
5 ghci>>> 5 / 3
6 1.6666666666666667

    首碼表達式:

1 ghci>>> (^) 3 3
2 27
3 ghci>>> (/) 5 2
4 2.5
5 ghci>>> (+) 1 9

  2.2 算術中的負數

1 ghci>>> -8
2 -8
3 ghci>>> 1 + -4
4 
5 <interactive>:57:1:
6     Precedence parsing error
7         cannot mix ‘+’ [infixl 6] and prefix `-' [infixl 6] in the same infix expression
8 ghci>>> 1 + (-3)
9 -2

-8其實並不是直接表示負數8, 而是利用一元操作符'-'對8取負, 所以第3行不能與中綴表達式一起使用, 除非加上()。

1 ghci>>> 55*-2
2 
3 <interactive>:60:3:
4     Not in scope: ‘*-5     Perhaps you meant one of these:
6       ‘*>’ (imported from Prelude), ‘**’ (imported from Prelude),
7       ‘*’ (imported from Prelude)

這樣是另一種不認識的操作符。

  2.3 布爾運算(True False)和比較運算

3種運算符: && || not    Haskell中True不是1, False不是0.

 1 ghci>>> True && False
 2 False
 3 ghci>>> True || False
 4 True
 5 ghci>>> not False
 6 True
 7 ghci>>> True && 1
 8 
 9 <interactive>:75:9:
10     No instance for (Num Bool) arising from the literal ‘111     In the second argument of ‘(&&)’, namely ‘112     In the expression: True && 1
13     In an equation for ‘it’: it = True && 1
14 ghci>>> 1 == 1
15 True
16 ghci>>> 4 > 6
17 False
18 ghci>>> 33 <= 50
19 True

  2.4 運算符優先順序和結合性

可以有命令 :info 查看指定操作符的優先順序值, 1表示最低, 9 表示最高。

 1 ghci>>> 2 + 3 * 5 ^ 2
 2 77
 3 ghci>>> :info (+)
 4 class Num a where
 5   (+) :: a -> a -> a
 6   ...
 7       -- Defined in ‘GHC.Num’
 8 infixl 6 +
 9 ghci>>> :info (*)
10 class Num a where
11   ...
12   (*) :: a -> a -> a
13   ...
14       -- Defined in ‘GHC.Num’
15 infixl 7 *
16 ghci>>> :info (^)
17 (^) :: (Num a, Integral b) => a -> b -> a     -- Defined in ‘GHC.Real’
18 infixr 8 ^
View Code

  2.5 變數的定義

使用ghci的let定義臨時變數

1 ghci>>> pi
2 3.141592653589793
3 ghci>>> e
4 
5 <interactive>:84:1: Not in scope: ‘e’
6 ghci>>> let e = exp 1
7 ghci>>> e
8 2.718281828459045
View Code

exp 1 表示調用指數函數exp,參數為1, 不必使用()。

3. 列表(List)

  列表長度可以是任意的。

  空的列表就是[]。

  列表中的元素必須相同類型。

 1 ghci>>> [1,2,3,4]
 2 [1,2,3,4]
 3 ghci>>> []
 4 []
 5 ghci>>> ['foo','rt']
 6 
 7 <interactive>:89:2:
 8     Syntax error on 'foo'
 9     Perhaps you intended to use TemplateHaskell
10     In the Template Haskell quotation 'foo'
11 ghci>>> ["foo","rt"]
12 ["foo","rt"]
13 ghci>>> [True,False,1,"str"]
14 
15 <interactive>:91:15:
16     Couldn't match expected type ‘Bool’ with actual type ‘[Char]’
17     In the expression: "str"
18     In the expression: [True, False, 1, "str"]
19     In an equation for ‘it’: it = [True, False, 1, ....]
View Code

  可以用列舉符號 .. 表示一系列的列表元素, 也可以根據前面的元素步長,自動填充後面省略的元素, 但是float類型可能會涉及到四捨五入的情況:

 1 ghci>>> [1..10]
 2 [1,2,3,4,5,6,7,8,9,10]
 3 ghci>>> [1,4,7..20]
 4 
 5 <interactive>:93:7: parse error on input ‘..’
 6 ghci>>> [1,4,7,..20]
 7 
 8 <interactive>:94:8: parse error on input ‘..’
 9 ghci>>> [1,4..20]
10 [1,4,7,10,13,16,19]
11 ghci>>> [1,8..20]
12 [1,8,15]
13 ghci>>> [1.2..2.3]
14 [1.2,2.2]
15 ghci>>> [1.2..2.6]
16 [1.2,2.2]
17 ghci>>> [1.2..3.6]
18 [1.2,2.2,3.2]
19 ghci>>> [1.0..1.8]
20 [1.0,2.0]
View Code

  當然你可以用[1..], 省略終點的方式產生一個無窮數列。

3.1 列表操作符 (連接)

  使用 ++ 連接N個相同元素的列表。

  使用 : 在某列表頭部加入。(格式必須: 前面是單個元素,後面是一個列表)

 1 ghci>>> [] ++ [False,True] ++ [False]
 2 [False,True,False]
 3 ghci>>> 100:[2,3,4,5] ++ [6,8,4]
 4 [100,2,3,4,5,6,8,4]
 5 ghci>>> [2,3]:1
 6 
 7 <interactive>:105:1:
 8     Non type-variable argument in the constraint: Num [[t]]
 9     (Use FlexibleContexts to permit this)
10     When checking that ‘it’ has the inferred type
11       it :: forall t. (Num t, Num [[t]]) => [[t]]
View Code

4. 字元串和字元

  字元串就是很多個單字元組成的列表, 所以可以列表的連接操作。

  putStrLn 是輸出字元串的函數。 \n \r 與C語言一樣轉意。

  ""與[]是相同的。

 1 ghci>>> "Hello Haskell"
 2 "Hello Haskell"
 3 ghci>>> putStrLn "We are in ghci console.\n See here."
 4 We are in ghci console.
 5  See here.
 6 ghci>>> 'c'
 7 'c'
 8 ghci>>> let a = ['h','e','l','l','o']
 9 ghci>>> a
10 "hello"
11 ghci>>> a == "hello"
12 True
13 ghci>>> [] == ""
14 True
15 ghci>>> 'w':"Haskell"
16 "wHaskell"
17 ghci>>> "Frist" ++ "Second"
18 "FristSecond"
View Code

5. 類型表示

  Haskell中,所有的類型以大寫字母開始,變數以小寫字母開始。

  可以使用 :set +t 是ghci返回結果的類型, :unset +t。

  可以使用 :type var/expression 顯示變數或表達式的類型, 並不參與計算。

 1 ghci>>> :set +t
 2 ghci>>> 'c'
 3 'c'
 4 it :: Char
 5 ghci>>> "foo"
 6 "foo"
 7 it :: [Char]
 8 ghci>>> 7^80
 9 40536215597144386832065866109016673800875222251012083746192454448001
10 it :: Num a => a
11 ghci>>> :m +Data.Ratio
12 ghci>>> 11 % 33
13 1 % 3
14 it :: Integral a => Ratio a
15 ghci>>> 11 % 3.3
16 
17 <interactive>:121:1:
18     No instance for (Fractional a0) arising from a use of ‘it’
19     The type variable ‘a0’ is ambiguous
20     Note: there are several potential instances:
21       instance Integral a => Fractional (Ratio a)
22         -- Defined in ‘GHC.Real’
23       instance Fractional Double -- Defined in ‘GHC.Float’
24       instance Fractional Float -- Defined in ‘GHC.Float’
25     In the first argument of ‘print’, namely ‘it’
26     In a stmt of an interactive GHCi command: print it
27 ghci>>> :unset +t
28 ghci>>> type 'c'
29 
30 <interactive>:123:6: parse error on input ‘'
31 ghci>>> :type 'c'
32 'c' :: Char
33 ghci>>> :type 1+8
34 1+8 :: Num a => a
View Code

6. 行計數程式 (WC.hs)

 1 main = interact wordCount 2 where wordCount input = show (length (lines input)) ++ "\n" 

然後新建一個quux.txt文件進行測試(內容隨便寫寫)。

打開shell命令行切換到當前目錄, 執行: runghc WC < quux.txt

即可得到txt文件的行數。

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 用 Entity Framework 進行 增,刪,改。都是基於Model進行的,且Model都是有狀態追蹤的。這樣Entity Framework才能正常增,刪,改。 有時候,要根據某個欄位,批量更新或者刪除數據,用Entity Framework就會顯得很是繁瑣,且不高效。 Entity Fra ...
  • UDP特點: 面向無連接,把數據打包發過去,收不收得到我不管 數據大小有限制,一次不能超過64k,可以分成多個包 這是個不可靠的協議 速度很快 視頻直播,凌波客戶端,feiQ都是UDP協議 TCP特點: 面向連接,對方必須在 三次握手完成連接,我:在嗎;你:我在;我:我知道了 大數據量傳輸 速度稍慢 ...
  • 網路通信的步驟, 1.找到對方的ip 2.數據發送到對方指定的應用程式上,為了標識這些應用程式,用數字進行標識,這個數字就是埠 3.定義通信規則,這個規則就稱為協議 國際組織定義了通用協議 TCP/IP 網路模型 OSI參考模型 網路分成7層,應用層 ==> 表示層 ==> 會話層 ==> 傳輸層 ...
  • 今天我們來介紹一下C語言操作資料庫的方法,這裡我們使用的是ODBC方式。環境是WIN7+VC6。其他環境也差不多,具體情況具體分析。 首先是環境的配置以及數據源的添加。這裡就不去解釋了,相關資料網上有很多。需要註意的是這裡不可以直接使用控制面板中的ODBC,我們需要打開C:\Windows\SysW ...
  • import java.util.ArrayList;import java.util.Scanner; /* * 題目描述: * 讀入數據string[ ],然後讀入一個短字元串。要求查找string[ ]中和短字元串的所有匹配,輸出行號、匹配字元串。 * 匹配時不區分大小寫,並且可以有一個用中括 ...
  • 全文純手打,不喜勿噴~ OOP封裝 //封裝一個類:用來管理普通人的特性和行為 //1、類的概念:人類在認識客觀世界時,發現某些事物具有共同特性,共同結構,共同行為。 // 為了方便,我們就將它們集合起來,並抽象出一個概念類管理它們,這個概念就是類。 //2、類的組成:標準類由:實例變數、構造器、設 ...
  • 一、遞歸函數 概念:遞歸演算法是一種直接或者間接的調用自身演算法的過程。在電腦編寫程式中,遞歸演算法對解決一大類問題是十分有效的。 特點: ①遞歸就是在過程或者函數里調用自身。 ②在使用遞歸策略時,必須有一個明確的遞歸條件,稱為遞歸出口。 ③遞歸演算法解題通常顯得很簡潔,但遞歸演算法解題的效率較低。所以一般 ...
  • 問題? Java7新增了關於文件屬性信息的一些新特性,通過java.nio.file.*包下麵的類可以實現設置或者讀取文件的元數據信息(比如最後修改時間,創建時間,文件大小,是否為目錄等等)。尤其是UserDefinedFileAttributeView,可以用來自定義文件的元數據信息。於是在自己的 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...