zoukankan      html  css  js  c++  java
  • F#个人学习笔记2(F# survey)

    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<string>) = ["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<int>) =   //递归输出列表中的元素,输出列表等于输出列表头再输出尾列表
        match l with
        | head :: tail -> printfn "%A" head ; outPutList tail ;
        | [] -> printfn "%s" "结束";

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



  • 相关阅读:
    windows 安装 make
    go lang AES 加密
    Microsoft .NET Framework 5.0
    Prometheus+Grafana+Alertmanager实现告警推送教程 ----- 图文详解
    ElasticSearch实战系列九: ELK日志系统介绍和安装
    1024快乐,加班使我快乐,福报如圣光醍醐灌顶!
    react-redux笔记
    (转)Vuex、Flux、Redux、Redux-saga、Dva、MobX
    React笔记
    SQLServer设置客户端使用IP地址登录
  • 原文地址:https://www.cnblogs.com/heros/p/1492953.html
Copyright © 2011-2022 走看看