open System open System.Threading // Learn more about F# at http://fsharp.net let a = 44 printfn "%A" a let s = "abcde" printfn "%A" s printfn "%A" s.Length let t = 2, 3, 4, 5 printfn "%A" t let t2 = (2, "abc", 4, 5) printfn "%A" t2 let (_,x2,_,_)=t2 printfn "%A" x2 let l1 = [] printfn "%A" l1 let l2 = [14;35;41;55;66] printfn "%A" l2 printfn "%A" l2.Head printfn "%A" l2.Tail.Head let cs = 1 :: [] let cs2 = [2 ; 4] :: [cs] printfn "%A" cs printfn "%A" cs2 let x1 :: sx1 = l2 printfn "%A" x1 printfn "%A" sx1 let l3 = l2 @ cs printfn "%A" l3 let l4 = [2..a] printfn "%A" l4 let l5 = ['a'..'z'] printfn "%A" l5 let l6 = [2..3..a] printfn "%A" l6 let l7 = [for x in 1..10 -> x * x] printfn "%A" l7 let l8 = [for x in 1..10 -> (x,x*x*x)] printfn "%A" l8 type MyEnum = | First = 0 | Second = 1 let e1 = MyEnum.Second type Count = int let (n : Count) = 5 type Expr = | Num of int | Add of Expr * Expr | Mul of Expr * Expr | Var of string let expT1 = Add(Var "a",Mul(Num 2,Var "b")) let envA = Map.ofList [ "a",1 ; "b",2 ; "c",3 ] type intt = | I1 of int | I2 of intt list let lx1 : intt = I2([I1(1);I1(2)]) let lx2 : intt = I2([I1(1);I2([I1(7);I1(7)])]) // ********* type PList = | E of int * string * int | V of PList List let data = V( [ E((1,"a",20)); E((2,"b",30)); V( [ E((3,"c",20)); E((4,"d",30)); ] ) ] ) printfn "%A" data //---- let rec costit (fm: PList) : int = match fm with | E(qty,_,cost) -> (qty * cost) | V(recs) -> costit2 recs and costit2 (ls : PList list) : int = match ls with | [] -> 0 | head :: tail -> (costit head) + (costit2 tail) let resa = costit data printfn "%A" resa let bb = List.map sqrt [1.;2.;3.;4.] printfn "%A" bb //---- let rec costitx (part: PList) : int = match part with | E(qty,_,cost) -> (qty * cost) | V(reclist) -> List.reduce (fun acc elem -> acc + elem) (List.map costitx reclist) let resax = costitx data printfn "%A" resax //************************************************************ type MyType = | Nd of string * decimal * decimal | Ld of string * decimal * MyType List let v1 = Nd("abc",3m,2m) let v2 : MyType = Ld("bcd",4m,[]) let v3 : MyType = Ld("bcd",4m,[Nd("a",1m,1m);Nd("b",2m,2m)]) let data2 : MyType = Ld("root",1.04m, [ Ld("LN",0.5m,[ Nd("L1",1m,2m) Ld("QN",2m,[ Nd("L5",5m,10m) Nd("L6",6m,12m) ]) Nd("L2",2m,4m) ]) Ld("RN",0.6m,[ Nd("L3",3m,6m) Nd("L4",4m,8m) ]) ]) printfn "%A" data2 let getq (p : MyType) = 3 let v4 = Nd("aa",3m,4m) printfn "%A" (getq v4) let getq' p = match p with | Nd(name,qty,price) -> qty | Ld(name,qty,ls) -> qty printfn "%A" (getq' v4) //**************************************** let rec cost p = match p with | Nd(name,qty,price) -> qty * price | Ld(name,qty,ls) -> qty * (cost2 ls) and cost2 ls = match ls with | [] -> 0m | hd :: tl -> (cost hd) + (cost2 tl) printfn "%A" (cost data2) //**************************************** let fn1 f a b = (f a) + (f b) let rx1 = fn1 (fun n -> n * n) 2 3 printfn "%A" rx1 let rx2 = fn1 (fun n -> n * n * n * n + 4532) 2 3 printfn "%A" rx2 let rx3 = fn1 (fun n -> 3) "abc" "def" printfn "%A" rx3 let add' = fun x -> x + x let rx4 = fn1 add' 33 44 printfn "%A" rx4 let add1 x = x + x let add2 x = x + x let add3 = add1 let add2' = add' let check item op = if op item then "OK" else "NG" let cx1 = check 4 (fun x -> x % 2 = 0) printfn "%A" cx1 let cx2 = check 4 (fun x -> x>3) printfn "%A" cx2 printfn "%A" (check 2 (fun x -> x>3)) let add x y = x + y let add5 = add 5 printfn "%A" (add5 3) let rec filter f ls = match ls with | [] -> [] | x :: xs -> (if f x then [x] else []) @ (filter f xs) printfn "%A" (filter (fun x -> x < 10) [1;3;10;20]) printfn "%A" (filter (fun x -> x % 2 = 0) [1;3;10;20]) let rec mapcar f ls = match ls with | [] -> [] | x :: xs -> f x :: (mapcar f xs) printfn "%A" (mapcar (fun x -> x+5) [1;3;10;20]) let rec reduce f s ls = match ls with | [] -> [] | x :: xs -> reduce f (f s x) xs printfn "%A" (reduce (fun r v -> r + v) 0 [1;3;10;20]) printfn "%A" (reduce (+) 0 [1;3;10;20]) printfn "%A" (List.map (fun x -> x + 4) [1;3;10;20]) let rs1 = [1;3;10;20] |> ((fun x -> x + 4) |> List.map) printfn "%A" rs1 let rs2 = [1;3;10;20] |> List.map (fun x -> x + 4) |> List.reduce (+) printfn "%A" rs2 let ee4 = [("Cats",4);("Dogs",5);("Mice",3);("Elephants",2)] |> List.filter (fun (nm : string,x) -> nm.Length <= 4) printfn "%A" ee4 let showVals() = let r = Random() let rec dl() = printfn "%A" r.Next Thread.Sleep(1000) dl() dl() let rss = [("Cats",4);("Dogs",5);("Mice",3);("Elephants",2)] |> List.filter (fun (nm,x) -> nm.Length <= 4) printfn "%A" rss