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

การสร้างตัวแปรต่างๆ Options · View
paedotnet
Posted: Wednesday, October 01, 2008 1:06:49 AM

Rank: มือเทพ
Groups: Member

Joined: 12/6/2007
Posts: 353
Location: bkk
การ Comment 
ถ้าเป็นการ comment บรรทัดเดียวจะใช้เครื่องหมาย //
เช่น // This is comment
แต่ถ้าเป็นการ Command หลายๆบรรทัดจะใช้เครื่องหมาย

(*
line1
line2
line3
...
...
*)

การสร้างตัวแปร

จะใช้ let แล้วตามด้วยชื่อตัวแปร เช่น


let i = 10


(ในภาษา F# จะไม่มี ; ต่อท้ายแต่จะใช้วิธีการขึ้นบรรทัดใหม่เพื่อแสดงการจบการทำงานแต่ละคำสั่ง)

การสร้างfunction ให้กับตัวแปรแบบมีการรับค่า parameter เข้ามา
รูปแบบ
let fn_name variable_name = value
ตัวอย่างถ้าต้องการ function ชื่อ mFunction และมีการรับค่า parameter x เข้ามาก็เขียนได้ดังนี้


let mFunction x  


และถ้าต้องการกำหนดค่าให้กับ parameter x ก็เขียนดังนี้


let mFunction x = x * x //กำหนดให้ x คูณกับ x


ถ้าต้องการเรียกใช้ mFunction ก็สามารถใช้ชื่อ function แล้วตามด้วย ค่า parameter ที่ต้องการจะส่งไป
ตัวอย่างโค้ด

#light //จะต้องไว้ข้างบนสุดเสมอ
let mFunction x = x * x
printfn "Result is %i" (mFunction 3) //คล้ายๆกับ ภาษา C คือจะ function printfn จะต้องระบุ pattern format ด้วย
System.Console.ReadKey(true) //รอรับค่าจาก keyboard


ตัวอย่างการบวก-ลบ ค่าเมื่อมีการรับค่า parameter เข้ามาสองตัว

#light
let AddFunction x y = x+y
printfn "AddFunction : %i" (AddFunction 10 5)
System.Console.ReadKey(true)

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

การสร้าง String,character
ข้อมูลที่เป็น string สามารถใช้ "string_data" ได้
ส่วนข้อมูลที่เป็น character สามารถใช้ ' character_data' ได้
ตัวอย่างการแสดง Hello

#light
printfn "Hello"
System.Console.ReadKey(true)

ในกรณีที่มีคำสั่งหลายบรรทัดใน function เดียวกัน สามารถเขียนได้ดังนี้


let function_name() =
command1
command2
...
...

ตัวอย่างเช่น

#light
let TestScope() =
   System.Console.WriteLine("Enter number")
   let a = 3
   let b = 5
   let result = a + b
   printfn "%d" result
TestScope()
System.Console.ReadLine()

ในตัวอย่างนี้สังเกตุว่าการประกาศ let TestScope กับ การเรียกใช้ จะต้องมี indent ตรงกันนะครับ
ผลลัพธ์ดังรูป 


 

การแสดงผลค่าต่างๆ
เราสามารถใช้  คำสั่ง Console.Write หรือ Console.WriteLine เหมือนใน C# ได้ดังนี้


   System.Console.Write("hello")
   System.Console.WriteLine("Hello")


และนอกจากนี้ ยังสามารถใช้คำสั่ง printfn ในการแสดงผลได้
การรับค่าต่างๆ
เราสามารถใช้คำสั่ง Console.Read หรือ Console.ReadLine ได้ดังนี้


 System.Console.Read()
   System.Console.ReadLine()

 

การสร้าง ฟังก์ชั่นซ้อนฟังก์ชั่น

#light
let Function1() =
    let Function2() =
       let a = 10
       let b = 20
       System.Console.WriteLine(a+b)
    Function2() //เรียกใช้ Function2
    System.Console.WriteLine("Hello")
Function1() //เรียกใช้ Function1
System.Console.ReadLine()

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

การสร้าง List
List ใน F# จะเป็นกลุ่มของข้อมูล (เหมือนกับ Array)
รูปแบบ จะใช้เครื่องหมาย [ และ ] ดังนี้
การสร้าง list แบบเรียงลำดับของตัวเลข let n =[1..10]
การสร้าง list แบบเรียงลำดับของตัวอักษร let c =['a'..'z']
การสร้าง list แบบว่างเปล่า(Empty List) let e =[]
การสร้าง list ที่เก็บสระ a,e,i,o,u    let q = ['a','e','i','o','u']
ในการเชื่อม element แต่ละ element เข้าด้วยกันใน list เราสามารถใช้เครื่องหมาย ;ได้ดังนี้
let m =["Ant";"Bird"]
ในการรวบรวมข้อมูลที่ต่างๆชนิดกันมาไว้ในกลุ่มเดียวกันสามารถเขียนได้ดังนี้
let f  = (1,"Hello",true)

การวนลูปเพื่อแสดงค่าของ list
let g = {for i in 1..20 -> i*i} // โค้ดนี้จะวนลูปค่าของ i ตั่งแต่ 1 ถึง 20 แล้วแสดงค่าผลคูณของ i

การวนลูป แบบมีเงื่อนไข
let h = {for i in 1..20 when i%2=0 -> i} //ในตัวอย่างนี้จะวนลูปตั่งแต่ 1 ถึง 20 แล้วแสดงค่าที่เป็น เลขคู่

 

ตัวอย่างการสร้าง list และการแสดงข้อมูลใน list ออกมา

#light
let a = [1..10]
let b = ['a'..'z']
let c = []
let d = ['a','e','i','o','u']
let e = ["Ant";"Bird"]
let f = (1,"HellO",true)
let g = {for i in 1..20 -> i*i}
let h = {for i in 1..20 when i%2=0 -> i}
printfn "%A\n" a
printfn "%A" b
printfn "%A" c
printfn "%A" d
printfn "%A" e
printfn "%A" f
printfn "%A" g
printfn "%A" h
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