Programming paradigms
แนะนำใหม่
13:52
แนวทางการเขียนโปรแกรม (Programming Paradigms)
นี่คืองานหนึ่ง: รายการนักเรียนทุกคนที่ได้เกรด A นักเขียนโปรแกรมคนหนึ่งเขียนลูป — เริ่มต้นที่บันทึกแรก ตรวจสอบเกรด เพิ่มชื่อลงในรายการ เลื่อนไป…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| loop/luːp/ | A structure that repeats a sequence of instructions. |
| array/əˈreɪ/ | An indexed collection of elements arranged in one or more dimensions. |
| filter/ˈfɪltə/ | A function that selects items from a collection using a given condition. |
| state/steɪt/ | The saved information needed to continue a program from its current point. |
| rule/ruːl/ | A logic-program statement deriving a conclusion when its conditions hold. |
| SQL/ˌes kjuː ˈel/ | Structured Query Language, used to define, query and manipulate data in relational databases. |
| index/ˈɪndeks/ | A position marker or lookup structure used to locate data within a collection or database. |
| cores/kɔːz/ | Independent processing units within a processor that can execute instructions. |
| paradigm/ˈpærədaɪm/ | รูปแบบการเขียนโปรแกรม |
| low-level/ləʊ ˈlevl/ | ระดับต่ำ |
| imperative/ɪmˈperətɪv/ | แบบคำสั่ง |
| declarative/dɪˈklærətɪv/ | แบบประกาศ |
| hardware/ˈhɑːdweə/ | The physical parts of a computer system that can be touched. |
| machine code/məˈʃiːn kəʊd/ | Instructions encoded in binary form that a processor can execute directly. |
| assembly language/əˈsemblɪ ˈlæŋɡwɪdʒ/ | A low-level programming language using symbolic names for machine instructions and their operands. |
| direct/daɪˈrekt/ | An addressing method where the instruction contains the memory address of the data. |
| registers/ˈredʒɪstəz/ | A small, fast storage location within a processor used to hold data, addresses or instructions temporarily. |
| memory addresses/ˈmeməri əˈdresɪz/ | Identifiers of locations in an addressable memory space, used to access stored instructions or data. |
| instructions/ɪnˈstrʌkʃnz/ | Commands that tell a computer what actions to perform. |
| memory/ˈmeməri/ | The part of a computer that holds data and programs currently in use. |
| processor/ˈprəʊsesə/ | The component that executes instructions and performs the operations of a computer or similar system. |
| device drivers/dɪˈvaɪs ˈdraɪvəz/ | Programs that allow an operating system to control hardware devices. |
| firmware/ˈfɜːmweə/ | Software stored in a device to control its hardware and basic operation. |
| embedded controllers | Computers built into larger devices to control their operation. |
| real-time system | A system that must respond to events within a required time. |
| immediate/ɪˈmiːdɪət/ | An addressing method where the data value is part of the instruction. |
| indirect/ɪndaɪˈrekt/ | An addressing method where the given address holds the address of the data. |
| indexed/ˈɪndekst/ | An addressing method that adds an index value to an address to find the data. |
| relative addressing/ˈrelətɪv əˈdresɪŋ/ | An addressing mode that adds an offset to the program counter. |
| operand/ˈɒpərænd/ | A value, register or memory location that an instruction operates on. |
| procedural/prəˈsiːdʒərəl/ | Describes programming that uses a sequence of commands to change program state. |
| sequence/ˈsiːkwəns/ | A set of things arranged in a particular order. |
| commands | Instructions that tell a computer or program to perform actions. |
| assignments | Statements that give values to variables in a program. |
| conditionals | Statements that choose which actions to perform based on a condition. |
| procedures | Named sets of instructions that perform tasks when called. |
| variables/ˈveərɪəblz/ | A quantity or named value that can change or take different values. |
| pseudocode/ˈsuːdəʊkəʊd/ | A language-independent description of an algorithm using structured, programming-like statements. |
| runtime | The software environment that manages and carries out a program while it runs. |
| Functional/ˈfʌŋkʃənl/ | Describes programming that builds results by combining functions. |
| pure functions/pjʊə ˈfʌŋkʃnz/ | ฟังก์ชันบริสุทธิ์ |
| side effects/saɪd ɪˈfekts/ | Observable effects beyond a routine's returned value, such as modifying an argument, changing shared state or writing output. |
| Logic/ˈlɒdʒɪk/ | Describes programming that uses facts and rules to answer questions. |
| facts/fækts/ | Statements treated as true within a logic program. |
| engine | Software that uses facts and rules to work out answers to queries. |
| inference/ˈɪnfərəns/ | The use of a trained model to make a prediction from new input. |
| query/ˈkwɪərɪ/ | A request to retrieve or manipulate information, especially data in a database. |
| rows | Horizontal lines in a table, each containing one record. |
| database/ˈdeɪtəbeɪs/ | An organised collection of data that can be accessed and managed. |
| memory locations | Places in memory where data is stored, each identified by an address. |
| data transformation | The process of changing data from one form into another. |
| parallel/ˈpærəlel/ | Describing operations that take place concurrently rather than one after another. |
| cached | Stored for a time so that it can be used again quickly. |
| global variable/ˈɡləʊbl ˈveərɪəbl/ | A variable declared outside a routine's local scope and accessible wherever the language's program or module scope rules permit. |
| bugs/bʌɡz/ | Mistakes in software that can make it work incorrectly. |
โปรแกรมเดียวกัน เขียนด้วยสี่วิธี
- ขอข้อมูลลูกค้าทั้งหมดในสหราชอาณาจักร นักเขียนโปรแกรม C จะเขียนลูปวนผ่านอาร์เรย์ นักเขียน Haskell จะเขียน Filter นักเขียน Prolog จะกำหนดกฎแล้วตั้งคำถาม ผู้ใช้ SQL จะเขียน
SELECT * FROM Customer WHERE Country = 'UK' - เฉพาะข้อแรกเท่านั้นที่บอก วิธีการ ทำ ส่วนอีกสามข้อบอก สิ่งที่ต้องการ และปล่อยให้เครื่องจัดการ โดยเครื่องมีอิสระที่จะจัดลำดับงานใหม่ ใช้ดัชนี หรือรันบน eight cores
- ไม่มีสิ่งใดดีกว่ากันโดยทั่วไป สิ่งที่แตกต่างกันคือคำตัดสินใจที่ภาษาดึงไปจากคุณ และนี่แหละคือสิ่งที่ Paradigm คือ
- บทเรียนนี้เกี่ยวกับ Paradigms ที่หลักสูตรระบุ: Low-level, Imperative และ Declarative

การเขียนโปรแกรม Low-level
- Low-level programming ทำงาน ใกล้กับฮาร์ดแวร์ ในรูปแบบ Machine code หรือ Assembly language โดยมีเข้าถึง Register, ที่อยู่หน่วยความจำ และคำสั่งแต่ละตัวโดยตรง
- ข้อดี: ได้ ควบคุม สูงสุดและ ความเร็ว สูงสุด รวมถึงโค้ดที่มีขนาดเล็กที่สุด ซึ่งสำคัญเมื่อมีหน่วยความจำเพียงกิโลไบต์
- ข้อเสีย: เป็น เฉพาะสถาปัตยกรรม จึงต้องเขียนใหม่สำหรับโปรเซสเซอร์อื่น และเขียนได้ยากและดูแลรักษายาก
- ใช้ในพื้นที่ที่ต้องสั่งฮาร์ดแวร์อย่างแม่นยำ: Device drivers, Firmware, Embedded controllers และ Innermost loop ของ Real-time system
- ฝึกการเข้าถึงแบบทันที, โดยตรง, ทางอ้อม, แบบดัชนี และแบบสัมพัทธ์ใน บทเรียนภาษาแอสเซมบลี. การติดตามตัวอย่างแสดงว่าทำไม operands เดียวกันจึงให้ผลลัพธ์ต่างกัน

Which are true of low-level programming? Select ทั้งหมด that apply.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Control is bought with effort: assembly is slow to write and hard to maintain, which is why it is confined to drivers and firmware.
การเขียนโปรแกรม Imperative
- Imperative programming หรือที่เรียกว่า Procedural คือ ลำดับของคำสั่งที่เปลี่ยนสถานะของโปรแกรม: Assignment, Conditionals, Loops และการเรียก Procedure
- นักเขียนโปรแกรมระบุ วิธีการ คำนวณผลลัพธ์ทีละขั้นตอน ตัวแปรที่เก็บสถานะเป็นจุดสำคัญ
- เป็นสไตล์ของ Python, C, Java และ Pseudocode และเป็นสิ่งที่ทุกคนเกือบทุกคนเรียนรู้เป็นอันดับแรก

คำสั่งหนึ่งตามอีกคำสั่งหนึ่ง เปลี่ยนสิ่งที่ถูกจัดเก็บ
Imperative (procedural) programming is based on:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Imperative code gives step-by-step commands (assignments, loops, calls) that change state.
การเขียนโปรแกรม Declarative
- Declarative programming บอก สิ่งที่ต้องการคำนวณ ไม่ใช่ วิธีการ Runtime จะหาขั้นตอนเอง
- Functional programming สร้าง Pure functions ซึ่งไม่มี Side effects: อินพุตเท่าเดิมให้เอาต์พุตเท่าเดิมเสมอ และไม่มีอะไรภายนอกฟังก์ชันเปลี่ยนแปลง Haskell และ Lisp เป็นตัวอย่าง
- Logic programming บอกข้อเท็จจริงและกฎ และ Engine ตอบคำถามด้วยการอนุมาน Prolog เป็นตัวอย่าง
- SQL เป็นภาษา Declarative ที่ทุกคนเคยเจอ: Query บอกว่าต้องการ Row ไหน และ Database ตัดสินใจวิธีค้นหา
Programming concept lab
Connect examples to the programming idea they show.
Declarative programming means you specify:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Declarative code (functional, logic, SQL) states the goal; the runtime decides the steps.
A pure function (functional programming):
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Purity means no side effects and a deterministic result, which makes functional code easy to reason about.
Declarative paradigms (functional, logic, SQL) state WHAT to compute and let the runtime decide how, whereas imperative code spells out every step.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
A SQL query says which rows you want, not how to scan the tables — the opposite of step-by-step imperative code.
A pure function always gives the same output for the same input and has no ____.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
That is what lets it be tested in isolation, run in parallel safely and have its result cached.
ตัวอย่างเชิงปฏิบัติ: ระบุ Paradygm
FOR i ← 1 TO n : total ← total + A[i] : NEXT i— imperative: ลำดับคำสั่งที่เปลี่ยนสถานะที่เก็บไว้ในtotalSELECT Name FROM Customer WHERE Country = 'UK'— declarative: บอกว่าต้องการ Row ไหนและไม่บอกว่าค้นหาอย่างไรLDD 200 : ADD 201 : STO 202— low-level: คำสั่ง Assembly ที่เข้าถึงตำแหน่งหน่วยความจำโดยตรงparent(X, Y) :- father(X, Y).— declarative โดยเฉพาะ logic programming: กฎที่ Engine อนุมานคำตอบออกมา- ชื่อนาม Paradygm และ คุณสมบัติของโค้ดที่แสดงออกถึงมัน
Match each fragment to its paradigm.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Commands changing state, a statement of what is wanted, instructions addressing memory, and a rule for inference.
เปรียบเทียบกัน
| Paradygm | บอก | จุดแข็ง | การใช้ทั่วไป |
|---|---|---|---|
| low-level | คำสั่งที่แน่นอน | ควบคุม, ความเร็ว, ขนาด | Drivers, Firmware |
| imperative | วิธีการ, ทีละขั้น | ตรงไปตรงมาและเป็นที่ยอมรับ | การเขียนโปรแกรมทั่วไป |
| declarative | สิ่งที่ต้องการ | กระชับ, Runtime ทำการ Optimize | Queries, Rules, Data transformation |
- ภาษาสมัยใหม่ ผสม ทั้งสอง Python เป็น imperative แต่มีคุณสมบัติ functional; SQL อยู่ภายในโปรแกรมที่เขียนแบบ imperative Paradygmคือสไตล์ ไม่ใช่กำแพงกั้น
Match each paradigm to its core idea.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Imperative says how step by step; OO models objects; functional uses pure functions; declarative states the goal.
What is the essential difference between imperative and declarative programming?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Because the declarative version does not fix the how, the runtime may reorder the work, use an index or parallelise it.
ตัวอย่างเชิงปฏิบัติ: ทำไม Side effects ถึงสำคัญ
- Pure function ไม่มี side effects อธิบายประโยชน์หนึ่ง.
- เนื่องจากอินพุตเดียวกันให้เอาต์พุตเสมอและไม่มีการเปลี่ยนแปลงภายนอก ฟังก์ชันบริสุทธิ์สามารถ ทดสอบแยกต่างหาก: ไม่ต้องตั้งค่า ไม่มีสถานะซ่อนเร้นที่ต้องเตรียม
- ยังสามารถรันใน Parallel ได้อย่างปลอดภัย เพราะสอง Call ไม่รบกวนกัน และผลลัพธ์สามารถ Cache ได้
- ความแตกต่าง: Routine แบบ imperative ที่อัปเดต Global variable จะทำงานต่างกันขึ้นอยู่กับสิ่งที่รันมาก่อนหน้า นี่คือสาเหตุที่ทำให้ Bug แบบนี้หาได้ยาก

คะแนนที่หลุดหายไป
- Imperative บอก how; Declarative บอก what ความแตกต่างนี้คือคำตอบของคำถามส่วนใหญ่ในที่นี้
- ข้อเสียของระดับต่ำคือมัน ขึ้นกับสถาปัตยกรรม และรักษายาก ไม่ใช่เพราะมัน "เก่า"
- Pure function ไม่มี side effects และให้เอาต์พุตเท่าเดิมสำหรับอินพุตเท่าเดิม ต้องมีทั้งสองส่วน
- SQL เป็น declarative เป็นตัวอย่างที่สอบมักใช้มากที่สุด ดังนั้นต้องจดจำได้
คุณเข้าใจแล้ว
- a paradigm คือสไตล์การจัดโครงสร้างโค้ด และภาษาสมัยใหม่ผสมหลายอย่าง
- low-level: Machine code หรือ Assembly เข้าถึงฮาร์ดแวร์โดยตรง ควบคุมและความเร็วสูงสุด แต่เฉพาะสถาปัตยกรรมและดูแลรักษายาก
- imperative: ลำดับคำสั่งที่เปลี่ยนสถานะ ระบุ how; สไตล์ของ Python, C และ Pseudocode
- declarative: บอก what ที่ต้องการและปล่อย how ให้ Runtime ทำ เช่น functional programming กับ pure functions, logic programming กับข้อเท็จจริงและกฎ และ SQL
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้