Drkcore

08 01 2009 perl lisp Tweet

カウンターのクロージャ

common lispだと

(let ((count 0))
  (list
  #'(lambda () (incf count))
  #'(lambda () (decf count))
  #'(lambda () (count)))

同じことをperlでもlispっぽくかける。

#!/usr/bin/env perl

use Perl6::Say;

sub make_counter {
  my $c = 0;
  return (
      sub {$c++},
      sub {$c--},
      sub {$c}
     );
}

my ($inc,$dec,$counter) = make_counter();

say $counter->(); # 0
$inc->();
$inc->();
$inc->();
say $counter->(); # 3
$dec->();
say $counter->(); # 2

ProductName 実践Common Lisp
Peter Seibel
オーム社 / ¥ 4,410 ()
通常24時間以内に発送

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021