Modula-2 Shopping Cart - Modula2 Typing CST Test
Loading…
Modula-2 Shopping Cart — Modula2 Code
Adds and removes items in a shopping cart with total cost.
MODULE ShoppingCart;
IMPORT InOut;
VAR cart: ARRAY 10 OF ARRAY 50 OF CHAR; prices: ARRAY 10 OF INTEGER; count: INTEGER := 0;
PROCEDURE addItem(item: ARRAY OF CHAR; price: INTEGER);
BEGIN
cart[count] := item; prices[count] := price; count := count + 1;
(* print cart and total *)
END addItem;
PROCEDURE removeItem(idx: INTEGER);
VAR i: INTEGER;
BEGIN
FOR i := idx TO count-2 DO cart[i] := cart[i+1]; prices[i] := prices[i+1] END;
count := count -1;
(* print cart and total *)
END removeItem;
BEGIN
addItem("Apple", 2);
addItem("Banana", 3);
removeItem(0);
END ShoppingCart.Modula2 Language Guide
Modula-2 is a statically typed, modular, procedural programming language designed for systems programming and teaching structured programming concepts, created as a successor to Pascal.
Primary Use Cases
- ▸Teaching structured and modular programming
- ▸Systems programming and embedded applications
- ▸Operating system and compiler development
- ▸Prototyping modular software architectures
- ▸Applications requiring strong type safety
Notable Features
- ▸Module system for encapsulation
- ▸Strong static typing
- ▸Procedural programming with structured control
- ▸Separate compilation for modules
- ▸Support for low-level systems programming
Origin & Creator
Developed by Niklaus Wirth in 1978 at ETH Zurich as a follow-up to Pascal, focusing on modularity and systems programming.
Industrial Note
Modula-2 found niche use in teaching programming concepts, embedded systems, and certain early OS development projects, though modern usage is mostly academic.
Quick Explain
- ▸Modula-2 emphasizes modularity, strong typing, and separate compilation of modules.
- ▸It introduces a module system to organize code, enabling encapsulation and namespace management.
- ▸Widely used in academia for teaching structured and systems programming, and in embedded or systems-level applications.
Core Features
- ▸Procedures, functions, and type-safe operations
- ▸Modules with exported and hidden interfaces
- ▸Record and array data structures
- ▸Set, pointer, and enumeration types
- ▸Control structures: IF, CASE, WHILE, FOR, REPEAT
Learning Path
- ▸Learn basic Pascal syntax
- ▸Understand modules and separate compilation
- ▸Practice writing procedures and functions
- ▸Explore records, arrays, and pointers
- ▸Build small modular projects
Practical Examples
- ▸Implementing a modular calculator
- ▸Creating a library of reusable procedures
- ▸Building simple file I/O applications
- ▸Developing a small embedded system module
- ▸Constructing compiler or interpreter components
Comparisons
- ▸Successor to Pascal with modular features
- ▸Stronger type safety and separate compilation
- ▸Less flexible than modern object-oriented languages
- ▸Excellent for structured and systems programming
- ▸Smaller ecosystem than C or Python
Strengths
- ▸Enforces disciplined programming with strong typing
- ▸Supports modular software design
- ▸Good for systems programming and low-level operations
- ▸Readable syntax similar to Pascal
- ▸Facilitates separate compilation and encapsulation
Limitations
- ▸Limited modern library ecosystem
- ▸Mostly academic or legacy use today
- ▸No native support for GUI programming
- ▸Not widely adopted in industry
- ▸Less flexible compared to modern object-oriented languages
When NOT to Use
- ▸Modern web or mobile development
- ▸GUI-intensive applications
- ▸High-level scripting or automation tasks
- ▸Projects requiring extensive third-party libraries
- ▸Enterprise software expecting active community support
Cheat Sheet
- ▸MODULE Name; … END Name. - define module
- ▸IMPORT ModuleName; - use another module
- ▸VAR x: INTEGER; - declare variable
- ▸PROCEDURE Foo(); - define procedure
- ▸BEGIN … END; - block or program body
FAQ
- ▸Is Modula-2 still used?
- ▸Mostly academic or legacy systems; active development is minimal.
- ▸Can Modula-2 handle low-level programming?
- ▸Yes, it supports pointers and system-level operations.
- ▸Does Modula-2 support modularity?
- ▸Yes, with a clear module interface and implementation separation.
- ▸Is Modula-2 object-oriented?
- ▸No, it is procedural and modular, not object-oriented.
- ▸Is Modula-2 cross-platform?
- ▸Compilers exist for Windows, Linux, and some legacy OSes.
30-Day Skill Plan
- ▸Week 1: Variables, types, and control structures
- ▸Week 2: Procedures, functions, and modules
- ▸Week 3: Records, arrays, and pointers
- ▸Week 4: Multi-module projects and linking
- ▸Week 5: Systems programming exercises
Final Summary
- ▸Modula-2 is a modular, strongly typed procedural language.
- ▸Designed for systems programming, teaching, and structured software design.
- ▸Emphasizes separate compilation and module encapsulation.
- ▸Mostly used in academic and legacy contexts today.
- ▸Introduced concepts foundational to later languages like Oberon.
Project Structure
- ▸Modules/ - source code files
- ▸Definitions/ - module interface files
- ▸Lib/ - standard or third-party libraries
- ▸Bin/ - compiled object files and executables
- ▸Docs/ - documentation and project notes
Monetization
- ▸Academic teaching materials
- ▸Embedded system solutions
- ▸Legacy system maintenance
- ▸Consulting on structured programming
- ▸Specialized industrial applications
Productivity Tips
- ▸Organize code into reusable modules
- ▸Use descriptive procedure and variable names
- ▸Separate interface and implementation cleanly
- ▸Keep module dependencies minimal
- ▸Document interfaces for clarity
Basic Concepts
- ▸Variables, constants, and types
- ▸Procedures and functions
- ▸Modules for encapsulation
- ▸Control structures (IF, CASE, loops)
- ▸Data structures: arrays, records, sets, pointers
Official Docs
- ▸Niklaus Wirth Modula-2 publications
- ▸ISO/IEC 10514 standard documents
- ▸Compiler manuals (XDS Modula-2, GNU Modula-2)