��ǰ�ʤ���Blocks���Ȥ���Τ�OS X�Τ��Ǥ��뤬��

Perl���󥸥˥��Τ����Objective-C Blocks���� - unknownplace.org
OS X 10.6 �ʹߤ� xcode �Ǥ� Objective-C �� Blocks �Ȥ������󥿥å������ɲä���Ƥ��롣

gcc�Ǥ���С�Nested Functions �Ȥ�����Τ��Ȥ��롣

�㤨�С��ʲ���codepad�Ǥ�ư����

http://codepad.org/V3uDduYT
#include <stdio.h>

int main(int argc, char** argv) {
    int fact(n){
	return n <= 1 ? 1 : n * fact(n-1);
    };
    printf("%d\n", fact(10));
    return 0;
}

���� nested function �ϡ�����404 Blog Not Found:C�Ƕ����ˤ��餤�����Ǥ�Ҳ𤷤����Ȥ����롣

blocks�Ȥΰ㤤�ϤȤ����С��ؿ���̵̾�ˤϤǤ��ʤ����ȡ�������

Blocks Programming Topics: Getting Started with Blocks
qsort_b(myCharacters, 3, sizeof(char *), ^(const void *l, const void *r) {
    char *left = *(char **)l;
    char *right = *(char **)r;
    return strncmp(left, right, 1);
});

�Ȥ������ȤϽ���ʤ��Τ���������Ȥ�

int compare(const void *l, const void *r) {
    char *left = *(char **)l;
    char *right = *(char **)r;
    return strncmp(left, right, 1);
};
qsort(myCharacters, 3, sizeof(char *), compare);

�Ȥ��뤳�Ȥ�ư���褦�ˤʤ롣

��ǰ�ʤ���blocks�Ǥ�nested function�Ǥ⡢��������closure�Ͻ񤱤ʤ����Ȥ������ѿ���enclose���뤳�Ȥ��Ǥ��ʤ������Ȥ��С��ʲ���segfault������Ƥ��ޤ���

http://codepad.org/JXAWS6ok
#include <stdio.h>

typedef int (*fint)(void);

fint make_incr(int n){
    int f(){ return n++; };
    return f;
}

int main(int argc, char** argv) {
    void f() { printf("Hello Nested Function!\n"); };
    f();
    fint fi = make_incr(0);
    fint gi = make_incr(0);
    printf("%d\n", fi());
    printf("%d\n", fi());
    printf("%d\n", fi());
    printf("%d\n", gi());
    return 0;
}

�ʤ�Perl�Ǥ�

http://codepad.org/jDG5y64E
#!/usr/bin/perl
sub make_incr{
    my $n = shift;
    sub { $n++ };
}

{
    my $fi = make_incr(0);
    my $gi = make_incr(0);
    printf("%d\n", $fi->());
    printf("%d\n", $fi->());
    printf("%d\n", $fi->());
    printf("%d\n", $gi->());
}

���Τ褦��ư���Τˡ�(nested fucntion��ĥ���줿)C�Ǥ�ư���ʤ��Τ����ɼԤν���Ȥ��Ƥ�������

Dan the (dys)?functional Man

�ɵ�:

�ʲ��Τ褦�ˤ���ȡ�blocks�Ǥ��ѿ���enclose���뤳�Ȥ������褦�ˤʤ롣faulist++

#include <stdio.h>
#include <Block.h>

typedef int (^bint)(void);

bint make_incr(int n){
    __block int _n = n;
    return Block_copy(^{ return _n++;});
}

int main(int argc, char** argv) {
    bint fi = make_incr(0);
    bint gi = make_incr(0);
    printf("%d\n", fi());
    printf("%d\n", fi());
    printf("%d\n", fi());
    printf("%d\n", gi());
    return 0;
}