#眉標=VS2010、F#、Functional Programming #副標=Visual Studio 2010開發平台(2) #大標=程式開發語言新成員─F# #作者=文/圖 王寧疆 ===<反灰>============= let num = 10 let str = "F#" ================ ===<反灰>============= let intList = [ 1; 2; 3; 4; 5; 6; 7 ] let strList = [ "one"; "two"; "three" ] ================ ===<反灰>============= let squareIt = fun n -> n * n let doubleIt = fun n -> 2 * n let funList = [ squareIt; doubleIt ] ================ ===<反灰>============= let squareIt = fun n -> n * n let BMICalculator = fun ht wt -> (float wt / float (squareIt ht)) * 703.0 let funTuple = (squareIt, BMICalculator) ================ ===<反灰>============= let num = 10 let moreMixedTuple = ( num, "two", 3.3, squareIt ) ================ ===<反灰>============= let funAndArgTuple2 = ((fun n -> n * n), 10)//第一個元素為函數,第二個元素為數值的Tuple printfn "%d" ((fst funAndArgTuple2) (snd funAndArgTuple2)) //將funAndArgTuple2集合的第二個元素傳給第一個元素當做參數,並印出執行的結果 ================ ===<反灰>============= let integerList = [ 1; 2; 3; 4; 5; 6; 7 ] //存放整數元素的集合 let squareIt = fun n -> n * n //可以將參數內容值進行平方運算的squareIt函數 let squareAll = List.map squareIt integerList //將integerList集合的元素經過squareIt函數運算的結果儲存到squareAll集合 printfn "%A" squareAll //印出squareAll集合中運算的結果 ================ ===<反灰>============= let checkFor item = let functionToReturn = fun lst -> List.exists (fun a -> a = item) lst functionToReturn //假設應用程式中擁有存放整數元素的integerList集合: let = [ 1; 2; 3; 4; 5; 6; 7 ] //將傳遞7當做參數給checkFor函數,執行得到的函數存放到checkFor7: let checkFor7 = checkFor 7 //利用checkFor7存放的參數,判斷integerList集合中是否有內容值為7的元素,並印出查詢結果: printfn "%A" (checkFor7 integerList) ================ ===<反灰>============= type Person(firstName: string, lastName: string, age: int) = static member Nobody = Person("", "", 0) member this.FirstName = firstName member this.LastName = lastName member this.Age = age override this.ToString() : string = System.String.Format("[Person {0} {1} ({2} years old)]", firstName, lastName, age) ================ ===<反灰>============= let people = [| Person("Ted", "Neward", 37); Person("Amanda", "Laucher", 27); Person("Matthew", "Podwysocki", 35); Person("Rachel", "Appel", 35); Person.Nobody; Person("Scott", "Allen", 38); Person("Luke", "Hoban", 18); |] ================ ===<反灰>============= for per in people do printf "Matching %A: " per match per.FirstName with | "Ted" -> printfn "Hey, Ted!" | "Amanda" -> printfn "Hey, Amanda!" | x -> printfn "Hey, %s!" x ================ ===<反灰>============= Matching [Person Ted Neward (37 years old)]: Hey, Ted! Matching [Person Amanda Laucher (27 years old)]: Hey, Amanda! Matching [Person Matthew Podwysocki (35 years old)]: Hey, Matthew! Matching [Person Rachel Appel (35 years old)]: Hey, Rachel! Matching [Person (0 years old)]: Hey, ! Matching [Person Scott Allen (38 years old)]: Hey, Scott! Matching [Person Luke Hoban (18 years old)]: Hey, Luke! ================ ===<反灰>============= ChangeInt myDelegate = x => x * 2; Console.WriteLine("{0}", myDelegate(5)); ================ ===<反灰>============= int[] source = new[] { 3, 8, 4, 6, 1, 7, 9, 2, 4, 8 }; foreach (int i in source.Where( x => { if (x <= 3) return true; else if (x >= 7) return true; return false; } )) Console.WriteLine(i); ================ ===<反灰>============= 3 8 1 7 9 2 8 ================ ===<反灰>============= int SumOf(int fromValue, int toValue) { int result = 0; for(int i = fromValue; i <= toValue; i++) result += i; return result; } ================ ===<反灰>============= let rec SumOf fromValue toValue = if fromValue > toValue then 0 else (SumOf (fromValue + 1) toValue) + fromValue ================ ===<反灰>============= bool IsPrime(int num) { int sqrt = 1 + sqrtf(num); for (int i=2; i PrimeRoots; PrimeRoots.push_back(2); PrimeRoots.push_back(3); PrimeRoots.push_back(5); PrimeRoots.push_back(7); int start = 0; int tens = 10; while(true) { int end = PrimeRoots.size(); for (int i=1; i<10; i++) { for (int pos = start; pos< end; pos++) { int newprime = tens * i + PrimeRoots[pos]; if (IsPrime(newprime)) { PrimeRoots.push_back(newprime); if (PrimeRoots.size()==target) return newprime; } } } start = end; tens *= 10; } } ================ ===<反灰>============= static bool IsPrime(int num) { int sqrt = 1 + (int)System.Math.Sqrt(num); for (int i = 2; i < sqrt; i++) { if (num % i == 0) return false; } return true; } static int NthLeftTruncatablePrime(int target) { var primeRoots = new List() { 2, 3, 5, 7 }; int start = 0; int tens = 10; while (true) { int end = primeRoots.Count; for (int i = 1; i < 10; i++) { for (int pos = start; pos < end; pos++) { int newprime = tens * i + primeRoots[pos]; if (IsPrime(newprime)) { primeRoots.Add(newprime); if (primeRoots.Count == target) return newprime; } } } start = end; tens *= 10; } } ================ ===<反灰>============= let IsPrime n = if n = 1 then false else let max = n |> float |> sqrt |> int let rec Test = function | x when x > max -> true | x -> if (n % x) = 0 then false else Test (x+1) Test 2 let NthLeftTruncatablePrime n = let oneDigitPrimes = [2;3;5;7] seq { let primes = ref oneDigitPrimes for tens = 1 to 8 do let acc = ref [] for i=1 to 9 do let newPrime = i * pown 10 tens let primes' = ref !primes while (!primes').Length > 0 do let x = newPrime+(!primes').Head if IsPrime x then acc := x :: !acc yield x primes' := (!primes').Tail done done primes := !acc |> List.rev done } |> Seq.append oneDigitPrimes |> Seq.nth (n-1) ================ ===<反灰>============= let IsPrime n = if n = 1 then false else let max = n |> float |> sqrt |> int let rec Test = function | x when x > max -> true | x -> if (n % x) = 0 then false else Test (x+1) Test 2 let NthLeftTruncatablePrime n = let oneDigitPrimes = [2;3;5;7] seq { let primes = ref oneDigitPrimes for tens = 1 to 8 do let acc = ref [] for i=1 to 9 do let newPrime = i * pown 10 tens let primes' = ref !primes while (!primes').Length > 0 do let x = newPrime+(!primes').Head if IsPrime x then acc := x :: !acc yield x primes' := (!primes').Tail done done primes := !acc |> List.rev done } |> Seq.append oneDigitPrimes |> Seq.nth (n-1) ================ ===<反灰>============= open System.Windows.Forms //引入System.Windows.Forms名稱空間 let form = new Form(Visible=false,TopMost=true,Text="F#函數程式設計") //建立Form類別的物件 let text = new TextBox(Dock=DockStyle.Fill, Multiline=true, ScrollBars=ScrollBars.Vertical, Text="使用F#製作的記事本") //建立TextBox類別的物件 form.Controls.Add(text) //將TextBox類別的物件加入到form物件的Controls集合中 form.ShowDialog() //顯示form物件 ================