1 / 26

Functional Programming

Functional Programming. Tasanawan Soonklang. Programming paradigms. Imperative Object-oriented Functional Logic. Procedural programming. Non-procedural programming or Declarative programming. Procedural programming. วิธีการสั่งงานได้รับอิทธิพลโดยตรงจากสถาปัตยกรรมของคอมพิวเตอร์

Download Presentation

Functional Programming

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 Programming Tasanawan Soonklang

  2. Programming paradigms • Imperative • Object-oriented • Functional • Logic Procedural programming Non-procedural programming or Declarative programming

  3. Proceduralprogramming • วิธีการสั่งงานได้รับอิทธิพลโดยตรงจากสถาปัตยกรรมของคอมพิวเตอร์ • Von Neumann machine ประกอบดว้ย • หน่วยประมวลผลกลาง ควบคุมและประมวลผลคำสั่ง • หน่วยความจำ เก็บชุดคำสั่งของโปรแกรมและข้อมูลที่จะใช้ • ใช้แนวคิดในการเปลี่ยนสถานะ (ค่าในหน่วยความจำ) ของเครื่องไปตามลำดับ จนได้ผลลัพธ์ที่ต้องการ • มักใช้คำสั่งกำหนดค่า (assignment statement) เป็นหลัก ในการควบคุมการทำงาน • มีคำสั่งประเภทอื่นๆ ประกอบในการกำหนดลำดับการทำงาน เช่น คำสั่งวนซ้ำ คำสั่งแบบมีเงื่อนไข

  4. Program Modulo • Read M1 • Read M2 • M1 = M1 - M2 • If M1 >= 0 then goto (3) • M1 = M1 + M2 • Write M1 • halt

  5. Non-proceduralprogramming • ออกแบบเพื่อลดอิทธิพลของลักษณะการทำงานของเครื่อง • การเขียนโปรแกรมเป็นอิสระจากฮาร์ดแวร์ • ภาษาเชิงหน้าที่ • ใช้ฟังก์ชั่นที่คล้ายกับฟังก์ชั่นทางคณิตศาสตร์ • ระบุการเชื่อมโยงค่าจากข้อมูลเข้าไปยังผลลัพท์ • ไม่ต้องระบุขั้นตอนการเปลี่ยนแปลงสถานะภายในเครื่อง • ภาษาเชิงตรรกะ • ใช้ความสัมพันธ์เป็นส่วนประกอบหลักในการเขียนโปรแกรม • เป็นการพิสูจน์ความจริงของความสัมพันธ์ทั้งหลายที่ระบุไว้ • ไม่ต้องระบุลำดับการทำงานให้กับโปรแกรม

  6. Title Functional Programming • Loremipsumdolorsitamet, consectetueradipiscingelit. Vivamus et magna. Fuscesedsemsed magna suscipitegestas. • Loremipsumdolorsitamet, consectetueradipiscingelit. Vivamus et magna. Fuscesedsemsed magna suscipitegestas. • Loremipsumdolorsitamet, consectetueradipiscingelit. Vivamus et magna. Fuscesedsemsed magna suscipitegestas. • ส่วนประกอบที่สำคัญ • โครงสร้างข้อมูลพื้นฐาน คือ ลิสต์ (list) • ฟังก์ชั่นพื้นฐาน • โครงสร้างที่ใช้สร้างฟังก์ชั่นจากฟังก์ชั่นที่มีอยู่ • โปรแกรมประกอบด้วยฟังก์ชั่น นิยามจากนิพจน์ (expression) • ค่าเดี่ยว • นิพจน์ย่อย • นิพจน์แบบมีเงื่อนไข

  7. Imperative vs. Functional fun fact(0) = 1 | fact (n) = n * fact(n-1); • ไม่มีคำสั่ง (statement) • ประกอบด้วยนิพจน์ • สั่งให้ทำซ้ำโดยใช้ recursive function • เรียกใช้ฟังก์ชั่นให้ทำงาน แทนการระบุในรูปคำสั่ง เช่น fact(3) int fact(int n) { inti = 1; for (int j = n; j>1; --j) i = i * j; return I; }

  8. การประมวลผล กลไกพื้นฐาน 2 ประการ • การเชื่อมโยงระหว่างชื่อกับค่า (binding) • การเรียกใช้ฟังก์ชั่น (application) fact(3) • เชื่อมโยง 3 กับชื่อ n ในโปรแกรม • เรียกใช้ฟังก์ชั่นที่ n มีค่าเป็น 3

  9. ฟังก์ชั่น (function) • การเชื่อมโยงข้อมูลจากเซตของโดเมน ไปยัง ข้อมูลในเซตของเรนจ์ (โคโดเมน) X1 X2 X3 X4 f(X1) f(X2) = f(X3) f(X4)

  10. การเชื่อมโยงข้อมูล (mapping) • Total function การเชื่อมโยงข้อมูลทุกตัวในเซตของโดเมน • Partial function การเชื่อมโยงข้อมูลบางตัวในเซตของโดเมน • ข้อมูลหนึ่งตัวจากเซตของโดเมนจะเชื่อมโยงไปยังข้อมูลเพียงตัวเดียวในเซตของเรนจ์ • การนิยามฟังก์ชั่นไม่กำกวม

  11. การอธิบายการเชื่อมโยงการอธิบายการเชื่อมโยง รูปแบบการอธิบายการเชื่อมโยง • การแจกแจงทุกการเชื่อมโยง • แผนภาพ • กฎหรือสมการ ... ... sign(-2) = minus sign(-1) = minus sign(0) = zero sign(1) = plus sign(1) = plus … … . . -2 -1 0 1 2 . . minus zero plus minus if x <0 sign(x) = zero if x = 0 plus if x > 0

  12. การเชื่อมโยงข้อมูล (mapping) • Total function การเชื่อมโยงข้อมูลทุกตัวในเซตของโดเมน • Partial function การเชื่อมโยงข้อมูลบางตัวในเซตของโดเมน • ข้อมูลหนึ่งตัวจากเซตของโดเมนจะเชื่อมโยงไปยังข้อมูลเพียงตัวเดียวในเซตของเรนจ์ • การนิยามฟังก์ชั่นไม่กำกวม

  13. แคลคูลัสแลมป์ดา • แคลคูลัสอย่างง่าย • ใช้เป็นโมเดลอธิบายการทำงานของฟังก์ชั่น • เป็นพื้นฐานในการออกแบบภาษาเชิงหน้าที่ • ใช้เครื่องหมาย l (lambda)แทนฟังก์ชั่น • ตัวอย่างการนิยามฟังก์ชั่น lx.x*x • การเรียกใช้ ((lx.x*x )2)

  14. ตัวอย่าง • กำหนด 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

  15. องค์ประกอบของภาษา (Components) • ใช้การเรียกฟังก์ชันมาทำงาน แทนการระบุในรูปคำสั่ง • นิยามขึ้นจากนิพจน์ต่างๆ • เป็นลักษณะการทำงานตามหน้าที่ (Functional) • มีส่วนประกอบดังนี้ • โครงสร้างข้อมูลพื้นฐาน คือ list ใช้เก็บข้อมูล • ฟังก์ชันพื้นฐาน เพื่อทำงานกับ list • โครงสร้างที่ช่วยสร้างฟังก์ชันใหม่จากฟังก์ชันที่มีอยู่

  16. Title ภาษา LISP • Loremipsumdolorsitamet, consectetueradipiscingelit. Vivamus et magna. Fuscesedsemsed magna suscipitegestas. • Loremipsumdolorsitamet, consectetueradipiscingelit. Vivamus et magna. Fuscesedsemsed magna suscipitegestas. • Loremipsumdolorsitamet, consectetueradipiscingelit. Vivamus et magna. Fuscesedsemsed magna suscipitegestas. • LISP - Listand Symbol Processing Language • ภาษาระดับสูงเก่าแก่อันดับสองรองจากภาษาฟอร์แทรน • สร้างขึ้นเพื่อนประมวลผลกับข้อความสัญลักษณ์ต่างๆ • เหมาะกับงานด้านปัญญาประดิษฐ์ • เป็นภาษาเชิงหน้าที่อย่างแท้จริง • การทำงานผ่านฟังก์ชั่น • ไม่มีการใช้ตัวแปร global หรือคำสั่งกำหนดค่าให้ตัวแปร • ค่าของตัวแปรเกิดจากการส่งผ่านค่าระหว่างฟังก์ชั่น

  17. LISP (List Processor) • นิพจน์ Lambda • (lx.ly. x+y) • โปรแกรมในภาษา LISP (LAMBDA(X Y) (PLUS X Y)) • การเรียกใช้ฟังก์ชั่น ((LAMBDA(X Y) (PLUS X Y)) 2 3) • การตั้งชื่อฟังก์ชั่น • DEFINE(ADD(LAMBDA(X Y) (PLUS X Y))) • (ADD 2 3)

  18. LISP (List Processor) • Primitive 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) • Objects ATOM, LIST

  19. Title ภาษา Scheme • Loremipsumdolorsitamet, consectetueradipiscingelit. Vivamus et magna. Fuscesedsemsed magna suscipitegestas. • Loremipsumdolorsitamet, consectetueradipiscingelit. Vivamus et magna. Fuscesedsemsed magna suscipitegestas. • Loremipsumdolorsitamet, consectetueradipiscingelit. Vivamus et magna. Fuscesedsemsed magna suscipitegestas. • มีรากฐานจากภาษา LISP • โครงสร้างภาษาขนาดเล็ก • syntax และ semantic ง่ายไม่ซับซ้อน • เขียนอยู่ในรูปแบบโครงสร้างของ list หรือนิพจน์ต่างๆ • การทำงานซ้ำใช้วิธีการของ recursive function • ตัวแปรภาษาเป็นลักษณะของ interpreter • เป็นภาษาที่นิยมใช้ในการเรียนการสอน

  20. Scheme • Syntax: โปรแกรมประกอบด้วยนิพจน์ เขียนอยู่ในวงเล็บในรูป prefix (ในรูปโครงสร้างของ list) • (+ 2 3) ; evaluates to 5 • (- 4 2 1) ; evaluates to 1 • (* 5 (+ 4 3)) • (max 2 3 17) • Semantic : สัญลักษณ์ตัวแรกในนิพจน์ คือ operator/function ตัวที่ตามมาเป็น operand/argument

  21. Scheme • กำหนดค่าหรือตัวแปร • (define f 120) • f ; evaluates to 120 • (+ f 5) • กำหนดเป็นรูปแบบของข้อมูล • (2 3 4) ; error: 2 is not a function • (quote (2 3 4)) • ‘(2 3 4)

  22. ประเภทของนิพจน์ • นิพจน์คณิตศาสตร์ ให้ค่าเป็นตัวเลข > (+ f 5) • นิพจน์ตรรกะ ให้ค่าเป็นจริงและเท็จ > (< 1 5) > (not (> 5 10) • นิพจน์แบบมีเงื่อนไข ให้เลือกทำงานตามเงื่อนไขที่ระบุ > (if (> 5 4) 40 50) > (cond ((> 3 2) “greater”) ((<3 2) “less”))) • นิพจน์แลมป์ดา ใช้นิยามฟังก์ชัน > (lambda (x) (+ x 1)) > ((lambda (x) (+ x 1)) 3)

  23. ฟังก์ชันประมวลผลข้อมูลฟังก์ชันประมวลผลข้อมูล • (null? x) ตรวจสอบว่า x เป็น list ว่างหรือไม่ • (cons a x) เพิ่ม a เข้าไปที่ต้น list • (append x y) นำ list y ไปต่อท้าย list x • (car x) ส่งค่าสมาชิกตำแหน่งแรกใน list x • (cdr x) ส่งค่าสมาชิกใน list x ทุกตัวยกเว้นตัวแรก • (length x) ส่งค่าจำนวนสมาชิกใน list x • (reverse x) ส่งค่าเป็นสมาชิกใน list x ในลำดับย้อนกลับ • (equal? x y) ตรวจสอบว่า list x เหมือนกับ list y หรือไม่

  24. ฟังก์ชันประมวลผลข้อมูลฟังก์ชันประมวลผลข้อมูล > (define mylist (list 1 2 3 4)) ; mylist > mylist ; (1 2 3 4) > (length mylist) ; 4 > (reverse mylist) ; (4 3 2 1) > (cons a mylist) ; (a 1 2 3 4) > (append ‘(a b) mylist) ; (a b 1 2 3 4) > (null? mylist) ; #f > (car mylist) ; 1 > (cdr mylist) ; (2 3 4) > (car (cdr mylist)) > (cdr (cdr mylist)) > (car (cdr (cdr mylist)))

  25. ฟังก์ชันเวียนบังเกิด 1 if n=0 f(n)  n*f(n-1) if n>0 (define (factorial n) (if (< n 1) 1 (* n (factorial (- n 1) ) ) ) ) (factorial 3) = (* 3 (factorial 2)) = (* 3 (* 2 (factorial 1))) = (* 3 (* 2 (* 1 (factorial 0)))) = (* 3 (* 2 (* 1 1))) = (* 3 (* 2 1)) = (* 3 2) = 6

  26. Imperative VS Functional • แนวคิดพื้นฐานของการสร้างโปรแกรม • ฟังก์ชันทางคณิตศาสตร์ เชื่อมโยงค่าจากข้อมูลไปยังผลลัพธ์ • กำหนดค่าข้อมูลในรูปตัวแปร ทำการเปลี่ยนแปลงค่าโดยคำสั่งต่างๆ ไปสู่ผลลัพธ์ • ความเป็นอิสระจากฮาร์ดแวร์ • ไม่เกี่ยวข้องกับโครงสร้างภายในหน่วยความจำหรือการเปลี่ยนสถานะของหน่วยความจำ • การเขียนโปรแกรม • syntax และ semantic ที่เรียบง่ายประกอบด้วยนิพจน์ต่างๆ

More Related