F 個人學習筆記2 F survey

2021-08-30 01:30:33 字數 3222 閱讀 5472

1、if語句 , f#返回值不需要顯式的寫出返回型別,在寫if等流程控制時,要控制好返回值資訊,如if 語句塊和 else 語句塊的返回值型別要匹配;不能出現if有返回、else無返回;也不能只出現乙個單條件返回,如只出現乙個if語句並且有返回值資訊,而沒有else語句,這樣在不滿足if 情況時則可能不會有返回值。

let fun1 x = //定義乙個返回字串的方法fun1 

if x > 10 then "輸入的值大於10"

//當x > 10 返回大於10的資訊

else    "輸入的值小於等於10"

//相反返回小於等於10  ,因為有返回值 else語句塊不能缺失 ,否則可能出現滿足if條件有返回,不滿足無返回的情況

printfn "%s" (fun1 10)

let num1 = //定義乙個無參的方法num1 ,返回型別為整型

let x = 10

if x > 10 then  10

else x

printfn "%i"    num1

let outputnum x = //定義乙個無返回值的方法

if x >= 10 then printfn "%i" 10

else printfn "%i" x

outputnum 10

let outputnum2 x = //定義乙個無返回值的方法

if x >= 10 then printfn "%i" 10

outputnum2 10

let checknum x = //定義乙個無返回值的方法,判斷輸入引數值是否等於10  

if x = 10 then printfn "%s" "x的值和10相等" //(注意判斷相等是單等於號)

else printfn "%s" "x的值和10不相等"

checknum 10

2、型別約束(或型別注釋) 將引數 或 識別符號用括號括起來,在後面用: type的方式來宣告具體的資料型別

let ( x : int ) = 1 // 申明整形識別符號x

let hello ( name : string ) = //hello 方法的引數name是string型別

printfn "hello %s" name

let ( names : list) = ["a";"b";"c";"d"]

let add (x : int) (y : int) = x + y

3、模式匹配 match 類似於 c# switch語句

let rec luc x =

match x with

| x when x <= 0 -> failwith "value must be greater than 0"

| 1 -> 1

| 2 -> 3

| x -> luc (x - 1) + luc ( x-1 - 2)

let printout x = 

match x with

| x -> printf "%s" x

printout "a"

let getlanguage ( lid : int ) = 

match lid with

| 1 -> "普通話"

| 2 -> "english"

| lid when lid <= 0 -> failwith "出錯啦"

| lid -> "未知"

printfn "%s" (getlanguage -3)

let getmonthdays x = //當多個匹配項的結果都是一樣的時候,可以連續用|來間隔多個 case情況 ,_ 類似sql的萬用字元表示其它匹配值情況

match x with 

| 1 | 3 | 5 | 6 | 8 | 10 | 12 -> 31

| 4 | 7 | 9 | 11 -> 30

| 2 -> 28

| _ -> failwith "月份不正確" //x _都可以匹配

printfn "%i" (getmonthdays 2)

printfn "%i" (getmonthdays 5)

printfn "%i" (getmonthdays 7)

let getmonthdays2 m y =

let checkrun year =

(year % 4 = 0 || year % 100 = 0)&&( year % 400 <> 0)

match m with 

| 1 | 3 | 5 | 6 | 8 | 10 | 12 -> 31

| 4 | 7 | 9 | 11 -> 30

| 2 when (checkrun y) = true -> 29

| 2 when (checkrun y) = false -> 28

| _ -> failwith "月份不正確" //x _都可以匹配

printfn "%i" (getmonthdays2 13 2000)

4、list和pattern match (列表和模式匹配) , 列表在進行模式匹配時可以用head 和 tail來表示列表頭和列尾,理解時可以把列表看成是鍊錶 head 表示頭結點,tail表示除了頭結點外的尾鏈s//如 [ 1 ; 2 ; 3 ; 4 ]  head 是 1  , tail 是 [ 2 ; 3; 4 ]    , 如 [ 1 ] head 是 1 , tail 是 [ ]

let outputlisthead l = 

match l with

| head :: tail -> printfn "%a" head   //在匹配時,case 應該是個list,head::tail,head與tail連線成為list, (::連線符參考list元素與list連線返回新列表,  @用來連線兩個列表)

outputlisthead [1;2;3;4] //輸出 1

let rec outputlist (l : list) =   //遞迴輸出列表中的元素,輸出列表等於輸出列表頭再輸出尾列表

match l with

| head :: tail -> printfn "%a" head ; outputlist tail ;

| -> printfn "%s" "結束";

outputlist [ for i in 1..2..9 -> i ]

個人學習筆記 2

rm 刪除檔案 rmdir 刪除空檔案 man專門用來檢視命令手冊 配合 help cal檢視日曆 cat將乙個檔案內容顯示到螢幕上 cat filename null 黑洞檔案 賦予null 檔案就會被刪除 awk幫助處理檔案內容 f 指定輸入檔案拆分符號 adduser username 新增新...

個人學習WCF筆記(2)

建立服務協定的基本規則 1.定義服務協定 在類或介面上使用seveicercontractattribute屬性標記。2.定義服務操作 在方法上使用operationcontractattribute 屬性對其進行標記 3.引數和返回值 每個操作都有乙個返回值和乙個引數,即使它們為void。可以使用...

git(個人學習2)

1.註冊github賬號 2.建立乙個儲存庫 3.連線 git remote add origin 4.git push u origin master 推送注意事項如果出現 fatal remote origin already exists.解決方案 git remote rm origin 再...