At Thu, 29 Sep 2005 18:38:31 +1000, Oscar Plameras wrote:
> Is their equivalent codes for ff in perl 6 ?
Sure, perl6 (just as in perl5) has coderefs. In fact, these can be
references to anonymous functions or dynamically created closures,
which certainly can't be done in C.
> [EMAIL PROTECTED] oscarp]# cat f.c
> int main(void)
> {
> int (*get_f())();
> static int arr[] = { -1, 0, 1, 0, 2, +9, +3200, -3500 };
> int *ptr, *pastend;
> int (*fptr)();
> pastend = arr + sizeof(arr)/sizeof(arr[0]);
> for (ptr = arr; ptr < pastend; ptr++)
> if (fptr = get_f(ptr))
> fptr(ptr);
> return 0;
> }
>
> int (*get_f(ptr))()
> int *ptr;
> {
> int is_neg(), is_pos();
> static int (*cmds[])() = {
> (int (*)())0,
> is_neg,
> is_pos
> };
> int index = 0;
> if (*ptr < 0)
> index = 1;
> if (*ptr > 0)
> index = 2;
> return(cmds[index]);
> }
> is_neg (iptr)
> int *iptr;
> {
> printf("%d is negative\n", *iptr);
> }
> is_pos (iptr)
> int *iptr;
> {
> printf("%d is positive\n", *iptr);
> }
Deliberately reproducing the structure of the C program:
#!/usr/bin/pugs
use v6;
sub get_f (Int $i) {
our @cmd is private //=
(undef, { say "$^a is negative" }, { say "$^a is positive" });
my $index = 0;
if ( $i < 0 ) { $index = 1 }
elsif ( $i > 0 ) { $index = 2 }
return @cmd[$index];
}
my int @arr = (-1, 0, 1, 0, 2, 9, 3200, -3500);
for @arr {
my $fptr = get_f($_);
$fptr($_) if $fptr;
}
But you are choosing awkward examples around trivial numerical
operations. Basic numeric operations and branching are things C can
do quite easily (provided the numbers fit within C's types). Try
common coding needs that C can't handle easily, like string
manipulation or memory management during error recovery. Oscar, I
suggest you actually use a high level language for a while and then
try going back to C. Doing anything in C (or even java for that
matter) is just so much tedious typing.
--
- Gus
--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html