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

    1、let 关键字用来声明标识符,标识符是值对象的名称,值对象可以是数字、字符串等也可以是方法。但标识符不同与变量,标识符一旦赋值则不可以改变。在同作用域下声明一个已经存在名称的标识符时,则是得到一个新的标识符。
    let i = 1
    let add x y = x + y
    (注:在方法内声明一个与全局标识符同名的标识符时,会隐藏全局标识符的值,但当方法结束后,全局变量标识符的值还是原来的值。)


    2、rec 递归函数在声明要加的关键字 (示例 从1加到100) 

    let rec addToNum x =
    match x with
    |1 -> 1
    |x -> (addToNum x - 1) + x

    printf "%i" (addToNum 100)

    3、fun   function 匿名方法关键字
    let f = fun x y -> x + y  // fun关键字定义的匿名方法参数可以任意个数

    printf "%i" (f 1 2)

    let ff = function x -> function y -> x + y //function定义的匿名方法最多只能有一个参数

    printf "%i" (ff 1 2)

    let fff = function (x,y) -> x + y  //可以用元组将参数打包成一个参数

    4、运算符重载 //但不知道为什么有些可用的运算符串组合会出语法错误

    let (+*)  x y= x * y
    printfn "%A" (2 +* 4)


    5、List 列表 , F# 的列表主要的操作符是[ ] , 列表一旦确定就不能再被改变 , 只会生成新列表 (类似C#字符串String)
    let a = [] //a是一个空元组
    let b = ["a" ; "b"]//b是一个含有两个元素的列表
    let c = "c" :: b //将"c"和列表b连接起来   将单个元素和列表连接用::符号(两个冒号)  
    let d = "d"::"e"::[]
    let e = "c" :: "d" :: d
    let f = d @ e//将两个列表进行连接用@符号

    List.rev list //将list列表元素反向后生成新列表
    List.iter (fun x -> printf "%s" x)  list //遍历list列表,用方法(fun x -> printf "%s" x)来处理


    6、List Comprehensions 列表含概 操作符 .. (双英文句号)  默认只能从小到大 ,数字列表可以指定增长step值,当增长值为负数时可以得到从小到大的数字列表,但字符不行
    let a = [ 1 .. 10 ]  //  [0;1;2;3;4;5;6;7;8;9;10]
    let b = [ 'A' .. 'D'] // [ 'A' ; 'B' ; 'C' ; 'D']
    let c = [1..3..7] //中间的3为增长step值   [1 ; 4 ; 7]
    let d = [1..3..9] //结果还是 [1 ; 4 ; 7] 增长结果值不能直接等于9,取不超过9的最后一个值就是7
    let e = [ 5 .. -1 .. 1] // 倒序列表[ 5 ; 4 ; 3 ; 2 ; 1 ]

    7、用for语句产生List 或 Sequence
    let a = [for x in 0..10 -> x] // [0;1;2;3;4;5;6;7;8;9;10]
    let b = [for x in 0..5 do
    for y in 0..5 -> x,y] //[(0,0);(0,1);(0,2);(0,3);(0,4);(0,5);(1,0);(1,1)........] 
    或者
    let b = [for x in 0..5 do
    for y in 0..5 ->( x,y)] //[(0,0);(0,1);(0,2);(0,3);(0,4);(0,5);(1,0);(1,1)........]  结果用元组

    let a = seq{for x in 0..10 -> x} //  seq[0;1;2;3;4;5;6;7;8;9;10]
    let b = seq{for x in 0..5 do
    for y in 0..5 -> x,y} //seq[(0,0);(0,1);(0,2);(0,3);(0,4);(0,5);(1,0);(1,1)........]

    let c = [ for x in 1 .. 5  do if x%2=1 then yield x ] // [ 1 ; 3 ; 5 ]

    let d = seq[ for x in 1 .. 7 do if x % 2 = 1 then yield x] // seq [ 1 ; 3 ; 5 ; 7 ]




  • 相关阅读:
    View Documentation in Web Using [openoffice.org 3+swftools+flexpaper]
    swfobject.js
    Java 文件上传组件 Apache Commons FileUpload 应用指南
    模拟HTML表单上传文件(RFC 1867)
    Using CURL to download a remote file from a valid URL in c++
    GUN tar for windows Practice
    Using libcurl in VC++
    Zlib Practice
    解决HtmlAgilityPack中文乱码
    Windows 7,Windows Server 2008 MSDTC配置
  • 原文地址:https://www.cnblogs.com/heros/p/1491377.html
Copyright © 2011-2022 走看看