CSE240: Programming Languages

Master Multiple Programming Paradigms

Imperative Programming (C)

  • Memory management & pointers
  • Arrays and string manipulation
  • Functions and parameter passing
  • Structs and unions
  • File I/O operations
  • Dynamic memory allocation
// C pointer manipulation int *ptr = malloc(sizeof(int) * 10); for(int i = 0; i < 10; i++) { ptr[i] = i * 2; } free(ptr);

Object-Oriented (C++)

  • Classes and objects
  • Inheritance and polymorphism
  • Encapsulation and abstraction
  • Virtual functions
  • Operator overloading
  • Templates and STL
// C++ class with inheritance class Animal { protected: string name; public: virtual void speak() = 0; }; class Dog : public Animal { public: void speak() override { cout << "Woof!" << endl; } };

Functional Programming (Scheme)

  • Lambda expressions
  • Higher-order functions
  • Recursion patterns
  • List processing
  • Closures and scope
  • Immutable data structures
;; Scheme recursive list processing (define (map-square lst) (if (null? lst) '() (cons (* (car lst) (car lst)) (map-square (cdr lst))))) ;; Using lambda ((lambda (x) (* x x)) 5) ;; returns 25

Logic Programming (Prolog)

  • Facts and rules
  • Pattern matching
  • Backtracking and unification
  • Cut operator (!)
  • List manipulation
  • Database queries
% Prolog family relationships parent(tom, bob). parent(bob, ann). parent(bob, pat). grandparent(X, Z) :- parent(X, Y), parent(Y, Z). % Query: ?- grandparent(tom, ann).

Course Timeline

Week 1-2
C Fundamentals
Pointers & Memory
Week 3
Advanced C
C++ Basics
Week 4
OOP in C++
Midterm
Week 5-6
Scheme Functional
Programming
Week 7
Prolog Logic
Programming
Week 8
Final Exam
Integration

Quick Reference Guide

C Memory Management

malloc() - allocate memory free() - deallocate memory calloc() - zero-initialized allocation realloc() - resize allocated memory // Always check for NULL! int *ptr = malloc(sizeof(int)); if (ptr == NULL) { // handle error }

C++ Key Concepts

// Virtual functions for polymorphism virtual void method() = 0; // pure virtual // Constructor initialization list MyClass(int x) : member(x) {} // RAII pattern unique_ptr<int> ptr = make_unique<int>(42);

Scheme Essentials

;; Basic list operations (car '(1 2 3)) ;; returns 1 (cdr '(1 2 3)) ;; returns (2 3) (cons 1 '(2 3)) ;; returns (1 2 3) ;; Conditional (if (null? lst) 'empty 'not-empty)

Prolog Patterns

% Basic query patterns member(X, [X|_]). member(X, [_|T]) :- member(X, T). % List append append([], L, L). append([H|T1], L2, [H|T3]) :- append(T1, L2, T3). % Cut to prevent backtracking max(X, Y, X) :- X >= Y, !. max(X, Y, Y).

Study Tips