Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PLT-A2 Closure

PLT-A2 Closure

Avatar for kanaya

kanaya

July 14, 2025
Tweet

More Decks by kanaya

Other Decks in Education

Transcript

  1. pineapple.cc ΦϒδΣΫτࢦ޲ϓϩάϥϛϯάʢ001ʣݴޠ w 001ݴޠ w 4NBMMUBML w $ $ 0CKFDUJWF$

    +BWB 3VTU 0CKFDU1BTDBM 7JTVBM#BTJDʜ w 1ZUIPO 3VCZ 1FSM +BWB4DSJQUʜ w ΫϩʔδϟʢDMPTVSFʣΛཅʹαϙʔτ͢Δݴޠ w 4DIFNF $PNNPO-JTQ $MPKVSF 4DBMB )BTLFMMʜ w ओͳ001ݴޠ
  2. pineapple.cc   Function call vs. Receiver, Selector, Parameter w

    BEE    • 1 + 2 — Python, etc. • (+ 1 2) — Scheme, Common LISP, etc. • (+) 1 2 — Haskell • add(1, 2) — Python, C, etc. w BEE • 1 + 2 — Smalltalk, Ruby • one.add(2) — Ruby, Python, C++, Swift, etc. • [one add: 2] — Objective-C
  3. pineapple.cc template <typename T> class vector_2d { private: T x,

    y; public: vector_2d(T ax , T ay): x(ax), y(ay) { } vector_2d<T> plus(const vector_2d<T> &v) { return vector_2d<T>(v.x + x, v.y + y); } T get_x() const { return x; } T get_y() const { return y; } };
  4. pineapple.cc plus_1 x = x + 1 plus_2 x =

    x + 2 main = print ((plus_1 2) + (plus_2 3)) make_plus_n n = \x -> x + n plus_1 x = make_plus_n 1 plus_2 x = make_plus_n 2 main = print ((plus_1 2) + (plus_2 3))
  5. pineapple.cc auto make_plus_n(auto n) { return [n](auto x) { return

    x + n; }; } auto plus_1 = make_plus_n(1); auto plus_2 = make_plus_n(2); int a = plus_1(2); int b = plus_2(3); std::cout << a << “, ” << b << std::endl;
  6. pineapple.cc template <typename T> class make_plus_n { private: int n;

    public: make_plus_n(T an): n(an) { } T operator ()(T x) { return x + n; } }; auto plus_1 = new make_plus_n<int>(1); auto plus_2 = new make_plus_n<int>(2); int a = (*plus_1)(2); int b = (*plus_2)(3); std::cout << a << “, ” << b << std::endl;
  7. pineapple.cc (define (make-plus-n n) (lambda (x) (+ x n))) (define

    plus-1 (make-plus-n 1)) (define plus-2 (make-plus-n 2)) (define (main args) (print (+ (plus-1 2) (plus-2 3)) 0) make_plus_n = \x -> x + n plus_1 x = make_plus_n 1 plus_2 x = make_plus_n 2 main = print ((plus_1 2) + (plus_2 3))
  8. pineapple.cc (define (make-plus-n n) (lambda (x) (+ x n))) (define

    n 2) (define plus-1 (make-plus-n 1)) (define plus-2 (make-plus-n n)) (define a (plus-1 2)) (set! n 100) (define b (plus-2 3)) (define (main args) (print (+ (plus-1 2) (plus-2 3)) 0)
  9. pineapple.cc (define (new-counter) (let ((count 0)) (lambda () (set! count

    (+ count 1)) count))) (define c (new-counter)) (define (main args) (display (c)) (display (c)) (display (c)) 0)