.NET Logo
Welcome Guest Search | Active Topics | Members | Log In | Register

ตัวอย่างการใช้งาน operator,map, record , if ,for-in ,for-to,while Options · View
paedotnet
Posted: Wednesday, October 01, 2008 2:49:39 PM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 354
Location: bkk

ตัวอย่าง operator ต่างๆใน F#

ชื่อ                    สัญลักษณ์
Append             @
Addition             +
Subtraction        -
Multiply              *
Division              /
Modulus            %
Concatenate      ^  
BitwiseAnd         &&&
BitwiseOr           |||
UnaryPlus            ~+  
UnaryNegation      ~~ 
Equality                  =
LessThanorequal    <=
greaterthanorequal  >=
greaterthan              >
lessthan                   <
dereference             !
piperight                   |>
pipeleft                     <|
Quatation                  <@ @>
AdditionAssignment   +=
SubstractionAssignment  -=
MultiplyAssignment         *=
DivisionAssignment        /=
RangeStep                    .. ..
Range                               ..

ตัวอย่างการใช้ operator

#light
// การใช้ Append //
let list1 = [1;2;3]
let list2 = [4;5;6]
let t_append = list1 @ list2  //รวม list 2 list เข้าด้วยกัน
printfn "%A" t_append
// การใช้ Addition,Substraction,Moltiply,Division,modulus //

let a = 30
let b = 20
System.Console.WriteLine(a+b) //การบวกค่า
System.Console.WriteLine(a-b) //การลบค่า
System.Console.WriteLine(a*b) //การคุณ
System.Console.WriteLine(a/b) //การหารเอาจำนวนเศษ
System.Console.WriteLine(a%b) //การหารเอาส่วน
//  การใช้งาน Concatenate //
let c = "Hello"
let d = "jame"
System.Console.WriteLine(c ^ d)

// การใช้  BitwiseAnd,BitwiseOr //
let e = 1010
let f = 0011
System.Console.WriteLine(e &&& f)//and
System.Console.WriteLine(e ||| f)//or
// การใช้ Quatation //
let g =10
let h = <@g@>
System.Console.WriteLine(h)
System.Console.ReadLine()


การใช้งาน cons operator
cons operator ใช้สัญลักษณ์ คือ ::
โดยจะทำหน้าที่เชื่อม element เข้ากับ list
ตัวอย่าง


 #light
let a = 2
let b = 3
let list_a = a :: b :: [4;5]
printfn "%A" list_a
System.Console.ReadLine()


การ map function
ตัวอย่างเช่นถ้าเราต้องการหาค่าของ3 คูณ กับ 3เราก้สามารถเขียนได้ดังนี้

#light
let M_Function x = x*x
printfn "%A" (M_Function 3)
System.Console.ReadLine()


ซึ่งผลลัพธ์จะได้ 9 สำหรับวิธีนี้จะเป็นการส่งค่าไปเพียงตัวเดียว(3) แต่ถ้าเราต้องการส่งค่าหลายๆค่า
ไปที่ function พร้อมๆกันแล้วแสดงผลลัพธ์ออกมาพร้อมๆกัน ก็จะต้องใช้วิธีที่เรียกว่า Map Function
ตัวอย่างโค้ด
ในตัวอย่างนี้จะส่งค่า 1,2,3,4 ไปที่ M_Function แล้วให้แสดงผลโดยนำค่าแต่ละค่าคูณกับตัวมันเอง

#light
let M_Function x = x*x
let map1 =List.map M_Function [1;2;3;4] //ส่งค่า 1,2,3,4 ไปที่ M_Function
printfn "%A" map1
System.Console.ReadLine()

ผลลัพธ์ ดังรูป

นอกจากจะสร้างฟังก์ชั่นขึ้นมาเองก่อนแล้วค่อย map เหมือนวิธีแรกแล้ว ยังสามารถที่จะใช้คำสั่ง fun ได้ดังนี
#light
let map1 =List.map (fun i -> i*i)[1;2;3;4]
printfn "%A" map1
System.Console.ReadLine()
ผลลัพธ์ของตัวอย่างนี้จะเหมือนอันแรกเพียงแต่ไม่ต้องสร้างฟังก์ชั่นขึ้นมา โดยจะใช้ fun แล้วสั่งให้นำค่าแต่ละค่า
คูณกับตัวมันเอง (i*i)

การสร้าง Record
Record เป็นกลุ่มของข้อมูลที่ประกอบไปด้วยข้อมูลหลายๆชนิดมารวมกันในกลุ่มเดียวซึ่งเราจะสร้างชนิดของข้อมูลไว้ใน Record แล้วพอนำไปใช้
เราก็เรียกใช้ชือข้อมูลเหล่านั้น

ตัวอย่างการใช้ Record

#light
type M_Record = {Id : int ; Name : string ; IsMember : bool}
let a = { Id = 1 ; Name="John" ; IsMember = true}
let b = { Id = 2 ; Name="jame" ; IsMember = false}
printfn "Id : %d , Name : %s , IsMember : %b " a.Id a.Name a.IsMember
System.Console.ReadLine()

ผลลัพธ์ดังรูป 

การใช้ Condition ใน F#
การใช้งาน if then else
รูปแบบ
if เงื่อนไข then
คำสั่ง
else
คำสั่ง
ตัวอย่าง

#light
let a = 10
let b = 20
let c =
 if a > b then
 "a > b"
 else
 "a < b"
printfn "%s" c
System.Console.ReadLine()

ผลลัพธ์จะได้ดังรูป

การใช้งาน if ซ้อน if

ตัวอย่าง


#light
let a = 10
let b = 20
let c =
 if a >= 10 then
   if b >= 20 then
   "b >= 20"
   else
   "b < 20"
 else
   "a < 10"
printfn "%s" c
System.Console.ReadLine()

การใช้งาน for
รูปแบบ for ค่าเริ่มต้น  to ค่าสุดท้าย do
คำสั่ง
ตัวอย่าง


#light
for i = 0 to 10  do
print_any i
System.Console.ReadLine()

ผลลัพธ์แสดงดังรูป

การใช้งาน for in
รูปแบบ
for ตัวแปร in  array_list do
คำสั่ง
ตัวอย่าง

#light
let str =[|"a";"b";"c"|]
for s in str do
printfn "%s" s
System.Console.ReadLine()


การใช้งาน while
รูปแบบ
while เงื่อนไข do
การเพิ่มค่า

#light
let TestWhile() =
 let c =ref 0
 while (!c < 20) do
  printfn "%d" !c
  c := !c + 1 //เป็น reference type ใช้ !
TestWhile()
System.Console.ReadLine()


ผลลัพธ์ดังรูป

                

 

               


               


               

 

 



[With great power comes great responsibility]
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFVision Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.8 (NET v2.0) - 3/29/2008
Copyright © 2003-2008 Yet Another Forum.net. All rights reserved.


Sponsored by