1 / 15

Functional Language

Functional Language. ภาษาโปรแกรมเชิงหน้าที่. ลักษณะของภาษา. ใช้หลักการของฟังก์ชันทางคณิตศาสตร์ square(2) บ 2*2 บ 4 รวมฟังก์ชันที่มีอยู่แล้วเข้าด้วยเป็นฟังก์ชันใหม่ F (x) บ G(x) o H(x) บ G(H(x)) เขียนเป็น Lambda Expression ได้ดังนี้ F บ l (G,H). l (x).G(H(x)).

diella
Download Presentation

Functional Language

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functional Language ภาษาโปรแกรมเชิงหน้าที่

  2. ลักษณะของภาษา • ใช้หลักการของฟังก์ชันทางคณิตศาสตร์ square(2) บ 2*2 บ 4 • รวมฟังก์ชันที่มีอยู่แล้วเข้าด้วยเป็นฟังก์ชันใหม่ F(x) บ G(x) o H(x) บ G(H(x)) เขียนเป็น Lambda Expression ได้ดังนี้ Fบ l(G,H).l(x).G(H(x)) Composite Function

  3. ตัวอย่าง • กำหนด square บ lx.x*x • จงหา to-the-fourth บ square o square บ l(square,square).lx.square(square(x)) บ lx.(lx.x*x(lx.x*x(x))) บ lx.(lx.x*x(.x*x)) บ lx.((.x*x)(x*x)) บ (2*2) * (2*2) บ 4*4

  4. องค์ประกอบของภาษา (Components) • ใช้การเรียกฟังก์ชันมาทำงาน แทนการระบุในรูปคำสั่ง • เป็นลักษณะการทำงานตามหน้าที่ (Functional) • มีองค์ประกอบดังนี้ • ฟังก์ชันเบื้องต้น (Primitive Function) • รูปแบบของฟังก์ชัน (Functional Forms) • วิธีดำเนินการ (Application Operation) • ออบเจกต์ (Object)

  5. A Simple Functional Programming Language • John Backus ได้คิดภาษา FP เป็นภาษาเชิงหน้าที่ ในปี 1970 • เป็นภาษาที่มีการทำงานไม่ขึ้นกับสถาปัตยกรรมของเครื่อง • มีออบเจกต์ n ตัว คือ x1, x2 ,…, xnเขียนแทนด้วย < x1, x2 ,…, xn > • สร้างขึ้นโดยใช้หลักการของฟังก์ชันทางคณิตศาสตร์ • มีฟังก์ชันเบื้องต้นและรูปแบบฟังก์ชันดังนี้

  6. ฟังก์ชันเบื้องต้น (Primitive Functions) • Selection Operations FIRST : < x1 , x2 ,…, xn > บ x1 LAST : < x1 , x2 ,…, xn > บ xn TAIL : < x1 , x2 ,…, xn > บ < x2 ,…, xn > i : < x1 , x2 ,…, xn > บ xi , 1 < i < n

  7. ฟังก์ชันเบื้องต้น (ต่อ) • Structuring Operations ROTR : < x1 , x2 ,…, xn > บ < xn , x1 ,…, xn-1 > ROTL : < x1 , x2 ,…, xn > บ < x2 , x3 ,…, xn , x1 > LENGTH: < x1 , x2 ,…, xn > บ n CONS : < x, <x1 , x2 ,…, xn >> บ <x, x1 , x2 ,…, xn >

  8. ฟังก์ชันเบื้องต้น (ต่อ) • Arithmetic Operations + : < x, y > บ x+ y - : < x, y > บ x- y * : < x, y > บ x*y / : < x, y > บ x/y • Identity ID : xบ x

  9. ฟังก์ชันเบื้องต้น (ต่อ) • Predicate Functions ATOM: xบ T if x atom NULL : xบ T if x O EQ : <x,y>บ T if x = y AND : <x,y>บ T if both are T OR : <x,y>บ T if one is T NOT : <x,y>บ T if x is F

  10. รูปแบบฟังก์ชัน (Functional Forms) • Composition ( f o g ) : x บ f : ( g : x ) ROTL o CONS :< x1, <x2 , x3>> บ ROTL : CONS :< x1, <x2 , x3>> บ ROTL :< x1, x2 , x3> บ < x2, x3 , x1>

  11. รูปแบบฟังก์ชัน (ต่อ) • Construction [f1, f2 ,…, fn] : x บ < f1 : x1 ,…, fn : x> [MIN, MAX, AVG, MED] : <0,1,2,3> บ <MIN: <0,1,2,3>,MAX:<0,1,2,3>, AVG:<0,1,2,3>,MED:<0,1,2,3>> บ <0,3,1.5,2>

  12. รูปแบบฟังก์ชัน (ต่อ) • Insert /f : x บ if x is <x1> then x1 else if x is the sequence <x1, x2 , ..., xn > and n>= 2 then f : <x1, /f : <x2 , ..., xn >

  13. รูปแบบฟังก์ชัน (ต่อ) /+ : <1,2,3,4> บ + : <1, /+: <2,3,4>> บ + : <1, +: <2, /+ : <3,4>>> บ + : <1, +: <2, + : <3, /+ : <4>>>> บ + : <1, +: <2, + : <3,4>>> บ + : <1, +: <2, 7>> บ + : <1, 9>> บ 10

  14. LISP (List Processor) • Objects ATOM, LIST • Function • Identity function QUOTE • Selection Operations CAR, CDR • Structuring Operations CONS • Predicate Functions ATOM, NULL, EQ, COND • DEFINE(ADD(LAMBDA(X Y) (PLUS X Y))) • Functional forms (LAMBDA(x) (SQUARE(SQUARE x)) 5 )

  15. ข้อแตกต่างระหว่าง Imperative VS Functional • แนวคิดพื้นฐานของการสร้างโปรแกรม • เวลาที่ใช้ในการทำงาน • การเขียนโปรแกรม • ประสิทธิภาพของภาษา

More Related