- let posc = String.index "abcdef" 'c'
-
-
- let xA = 1
- let xB = 2
- let xC = xA + xB
- let twist x = 10 - x
- do Printf.printf "res = %d/n" (twist (xC+4))
-
-
- do Printf.printf "stop"
- let inc x = x + 1
- let rec fact n = if n=0 then 1 else n * fact (n-1)
- do Printf.printf "inc(4)=%d/n" (inc 4)
- do Printf.printf "fact=f(5)=%d/n" (fact (inc 4))
- let one_x x=x*x+2*x+5
- do Printf.printf "one_x(4)=%d/n" (one_x 4)
- let one_xy x y=x*x+y*y+5
- let rec hcf a b =
- if a=0 then
- b
- else
- if a<b then
- hcf a (b-a)
- else
- hcf (a-b) b
-
-
-
- let pA = (1,2,3)
- let pB = (1,"fred",3.1415)
- let swap (a,b) = (b,a)
- let rec hcf2 (a,b) =
- if a = 0 then b
- else if a<b then hcf2 (a,b-a)
- else hcf2 (a-b,a)
- let bA = true
- let bB = false
- let bC = not bA && (bB || false)
- let hsgA=true
- let hsgB=false
- let hsgC=not hsgA && (hsgB || false)
- let sA = "hello"
- let sB = "world"
- let sC = sA + " " + sB
- let sC2 = String.concat " " [sA;sB]
- do Printf.printf "sC = %s, sC2 = %s/n" sC sC2
- let s3 = Printf.sprintf "sC = %s, sC2 = %d/n" sC sC2.Length
- let h_sA="hello"
- let h_sb="world"
- let h_sC=h_sA+" "+h_sb
- let h_sC2=String.concat "" [h_sA;h_sb]
- do Printf.printf "sC=%s,sC2=%s/n" h_sC h_sC2
- let xsA = [ ]
- let xsB = [ 1;2;3 ]
- let xsC = 1 :: [2;3]
- do print_any xsC
- let rec sumList xs =
- match xs with
- | [] -> 0
- | y::ys -> y + sumList ys
-
- let y = sumList [1;2;3]
- let xsD = xsA @ [1;2;3]
- let xsE = 99 :: xsD
- let arr = Array.create 4 "hello"
- do arr.(1) <- "world"
- do arr.(3) <- "don"
- let nA = Array.length arr
- let nB = arr.Length
- let front = Array.sub arr 0 2
-
-
- let inc2 x = x + 2
- let inc3 = fun x -> x + 3
- let ysA = List.map inc2 [1;2;3]
- let ysB = List.map inc3 [1;2;3]
- let ysC = List.map (fun x -> x+4) [1;2;3]
- let pipe1 = [1;2;3] |> List.map (fun x -> x+4)
- let pipe2 =
- [1;2;3]
- |> List.map (fun x -> x+4)
- |> List.filter (fun x -> x>5)
- let processor = List.map (fun x -> x+4) >> List.filter (fun x -> x>5)
- type expr =
- | Num of int
- | Add of expr * expr
- | Sub of expr * expr
- | Mul of expr * expr
- | Div of expr * expr
- | Var of string
-
- let lookup id (env : (string * int) list) = List.assoc id env
-
- let rec eval env exp = match exp with
- | Num n -> n
- | Add (x,y) -> eval env x + eval env y
- | Sub (x,y) -> eval env x - eval env y
- | Mul (x,y) -> eval env x * eval env y
- | Div (x,y) -> eval env x / eval env y
- | Var id -> lookup id env
-
- let envA = [ "a",1 ;
- "b",2 ;
- "c",3 ]
-
- let expT1 = Add(Var "a",Mul(Num 2,Var "b"))
- let resT1 = eval envA expT1
- type card = { name : string;
- phone : string;
- ok : bool }
-
- let cardA = { name = "Alf" ; phone = "+44.1223.000.000" ; ok = false }
- let cardB = {cardA with phone = "+44.1223.123.456"; ok = true }
- let string_of_card c =
- c.name + " phone: " + c.phone + (if not c.ok then " (unchecked)" else "")
- let cardC = { new card
- with name = "Alf"
- and phone = "+44.1223.000.000"
- and ok = false }
- type IPeekPoke = interface
- abstract member Peek: unit -> int
- abstract member Poke: int -> unit
- end
-
- type widget = class
- val mutable state: int
- member x.Poke(n) = x.state <- x.state + n
- member x.Peek() = x.state
- member x.HasBeenPoked = (x.state <> 0)
- new() = { state = 0 }
- end
-
- type wodget = class
- val mutable state: int
- interface IPeekPoke with
- member x.Poke(n) = x.state <- x.state + n
- member x.Peek() = x.state
- end
- member x.HasBeenPoked = (x.state <> 0)
- new() = { state = 0 }
- end
- do Printf.printf "stop"
-
posted @
2008-09-17 20:10
sqlite例子
阅读(
173)
评论()
收藏
举报