[Rio-pm] [Golf] Função drop Haskell

Fernando Oliveira fernandocorrea em gmail.com
Quarta Fevereiro 3 12:02:37 PST 2010


54:

sub drop{($a,$b)=@_;ref$b?@$b[$a..$#{$b}]:substr$b,$a}

Just another Perl Hacker,
Fernando (SmokeMachine)
http://perl-e.org



Em 3 de fevereiro de 2010 17:54, Nilson Santos Figueiredo Jr. <
acid06 em gmail.com> escreveu:

> Pronto, 80 caracteres (sem strict).
> Estou satisfeito, agora é com vocês:
>
> sub
> drop{($n,$w)=@_;$n=0if$n<0;ref$w?@$w[$n..$#{$w}]:$n>length$w?'':substr$w,$n}
>
> -Nilson
>
> 2010/2/3 Nilson Santos Figueiredo Jr. <acid06 em gmail.com>:
> > Melhorando a minha solução anterior, 85 caracteres (83 tirando o my):
> >
> > sub drop{my($n,$w)=@_;$n=0
> > if$n<0;ref$w?@$w[$n..$#{$w}]:$n>length$w?'':substr($w,$n)}
> >
> > -Nilson
> >
> > 2010/2/3 Nilson Santos Figueiredo Jr. <acid06 em gmail.com>:
> >> Uma solução, warnings and strict compliant:
> >>
> >> sub drop {
> >>    my ($n, $w) = @_;
> >>    $n = 0 if $n < 0;
> >>    return ( $n > length $w ? '' : substr($w,$n) ) if !ref $w;
> >>    @$w[$n..$#{$w}];
> >> }
> >>
> >> Se colocar tudo em uma linha:
> >>
> >> sub drop{my($n,$w)=@_;$n=0
> >> if$n<0;return($n>length$w?'':substr($w,$n))if!ref$w;@$w[$n..$#{$w}];}
> >>
> >> 96 caracteres. Deve dar pra melhorar bastante ainda. Se tirar o "my"
> >> pra deixar de ser strict compliant, cai pra 94.
> >>
> >> -Nilson
> >>
> >> 2010/2/3 Daniel de Oliveira Mantovani <
> daniel.oliveira.mantovani em gmail.com>:
> >>> Olá pessoal, o desafio envolve a função drop do Haskell:
> >>>
> >>> <haskell>
> >>> ghci> myDrop 2 "foobar"
> >>> "obar"
> >>> ghci> myDrop 4 "foobar"
> >>> "ar"
> >>> ghci> myDrop 4 [1,2]
> >>> []
> >>> ghci> myDrop 0 [1,2]
> >>> [1,2]
> >>> ghci> myDrop 7 []
> >>> []
> >>> ghci> myDrop (-2) "foo"
> >>> "foo"
> >>> </haskell>
> >>>
> >>> Eu fiz em Perl, para ficar claro:
> >>>
> >>> <perl>
> >>> sub drop {
> >>>    my ( $n, $xs ) = @_;
> >>>    if ( ref $_[1] ne 'ARRAY' ) {
> >>>        $n <= 0 ? return $_[1] : return substr( $_[1], $n );
> >>>    }
> >>>    if ( $n <= 0 || !@_ ) {
> >>>        return @{$xs};
> >>>    }
> >>>    else {
> >>>        shift @{$xs};
> >>>        drop( ( $n - 1 ), $xs );
> >>>    }
> >>> }
> >>> </perl>
> >>>
> >>> Exemplos:
> >>> mantovani em mantovani-desktop:~/Perl/Funcional$ perl -MHaskell -E 'say
> >>> $_ for drop(3,[1,2,3,4,5,6])'
> >>> 4
> >>> 5
> >>> 6
> >>>
> >>> mantovani em mantovani-desktop:~/Perl/Funcional$ perl -MHaskell -E 'say
> >>> $_ for drop(1,"ofernandoagoragolf")'
> >>> fernandoagoragolf
> >>>
> >>> mantovani em mantovani-desktop:~/Perl/Funcional$ perl -MHaskell -E 'say
> >>> $_ for drop(2,["mantovani","garu","fernando","blabos"])'
> >>> fernando
> >>> blabos
> >>>
> >>> mantovani em mantovani-desktop:~/Perl/Funcional$ perl -MHaskell -E 'say
> >>> $_ for drop(-1,["mantovani","garu","fernando","blabos"])'
> >>> mantovani
> >>> garu
> >>> fernando
> >>> blabos
> >>>
> >>> mantovani em mantovani-desktop:~/Perl/Funcional$ perl -MHaskell -E 'say
> >>> $_ for drop(-1,"mantovani")'
> >>> mantovani
> >>>
> >>> mantovani em mantovani-desktop:~/Perl/Funcional$ perl -MHaskell -E 'say
> >>> $_ for drop(10,"mantovani")'
> >>>
> >>> --
> >>> "If you’ve never written anything thoughtful, then you’ve never had
> >>> any difficult, important, or interesting thoughts. That’s the secret:
> >>> people who don’t write, are people who don’t think."
> >>> _______________________________________________
> >>> Rio-pm mailing list
> >>> Rio-pm em pm.org
> >>> http://mail.pm.org/mailman/listinfo/rio-pm
> >>>
> >>
> >
> _______________________________________________
> Rio-pm mailing list
> Rio-pm em pm.org
> http://mail.pm.org/mailman/listinfo/rio-pm
>
-------------- Próxima Parte ----------
Um anexo em HTML foi limpo...
URL: <http://mail.pm.org/pipermail/rio-pm/attachments/20100203/a43988ed/attachment.html>


Mais detalhes sobre a lista de discussão Rio-pm