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

Nilson Santos Figueiredo Jr. acid06 em gmail.com
Quarta Fevereiro 3 13:07:32 PST 2010


Boa. Na verdade o seu tem 63
Não sabia que $#$b era válido. Pra mim, nesses casos, sempre tinha que
usar $#{$b}.

-Nilson

2010/2/3 Fernando Oliveira <fernandocorrea at gmail.com>:
> 64:
>
> sub drop{($a,$b)=@_;$a=0if$a<0;ref$b?@$b[$a..$#$b]:substr$b,$a}
>
> Just another Perl Hacker,
> Fernando (SmokeMachine)
> http://perl-e.org
> Sent from Rio De Janeiro, RJ, Brazil
>
>
> Em 3 de fevereiro de 2010 18:33, Nilson Santos Figueiredo Jr.
> <acid06 at gmail.com> escreveu:
>>
>> Eu tenho certeza que vai dar pra alguém melhorar, ainda tá grande.
>> Mas se eu ficar mais tempo aqui tentando... vou ter meus dias inteiros
>> pra ficar brincando de Golf ao invés de ter um trabalho. ;-)
>>
>> -Nilson
>>
>> 2010/2/3 breno <breno at rio.pm.org>:
>> > Nilson, o final deveria dizer "mas é igual à versão do Nilson". Como
>> > eu fiz um monte de edições e contagens, acabou q foi enviado sem e eu
>> > nao percebi. O crédito e a menor tacada ainda são seus :-)
>> >
>> > []s
>> >
>> > -b
>> >
>> > 2010/2/3 Nilson Santos Figueiredo Jr. <acid06 at gmail.com>:
>> >> Breno,
>> >>
>> >> Essa que você mandou é exatamente igual a minha anterior, mas ao invés
>> >> de $a,$b eu uso $n,$w.
>> >> Tem 65 chars fora e 55 dentro. Eu estava contando tudo.
>> >>
>> >> Mas essa tem um problema que retorna undef em alguns casos. Aí o
>> >> Mantovani que tem que falar se é certo ou não.
>> >>
>> >> -Nilson
>> >>
>> >> 2010/2/3 breno <breno at rio.pm.org>:
>> >>> fernando, nao pode funcionar pra valores negativos, vai entender :-)
>> >>>
>> >>> acredito que assim funcione:
>> >>>
>> >>> sub drop{($a,$b)=@_;$a=0if$a<0;ref$b?@$b[$a..$#{$b}]:substr$b,$a}
>> >>>
>> >>> 55 chars (dentro da sub).
>> >>>
>> >>>
>> >>>
>> >>> 2010/2/3 Fernando Oliveira <fernandocorrea at gmail.com>:
>> >>>> 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 at 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 at 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 at 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 at 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 at mantovani-desktop:~/Perl/Funcional$ perl -MHaskell -E
>> >>>>> >>> 'say
>> >>>>> >>> $_ for drop(3,[1,2,3,4,5,6])'
>> >>>>> >>> 4
>> >>>>> >>> 5
>> >>>>> >>> 6
>> >>>>> >>>
>> >>>>> >>> mantovani at mantovani-desktop:~/Perl/Funcional$ perl -MHaskell -E
>> >>>>> >>> 'say
>> >>>>> >>> $_ for drop(1,"ofernandoagoragolf")'
>> >>>>> >>> fernandoagoragolf
>> >>>>> >>>
>> >>>>> >>> mantovani at mantovani-desktop:~/Perl/Funcional$ perl -MHaskell -E
>> >>>>> >>> 'say
>> >>>>> >>> $_ for drop(2,["mantovani","garu","fernando","blabos"])'
>> >>>>> >>> fernando
>> >>>>> >>> blabos
>> >>>>> >>>
>> >>>>> >>> mantovani at mantovani-desktop:~/Perl/Funcional$ perl -MHaskell -E
>> >>>>> >>> 'say
>> >>>>> >>> $_ for drop(-1,["mantovani","garu","fernando","blabos"])'
>> >>>>> >>> mantovani
>> >>>>> >>> garu
>> >>>>> >>> fernando
>> >>>>> >>> blabos
>> >>>>> >>>
>> >>>>> >>> mantovani at mantovani-desktop:~/Perl/Funcional$ perl -MHaskell -E
>> >>>>> >>> 'say
>> >>>>> >>> $_ for drop(-1,"mantovani")'
>> >>>>> >>> mantovani
>> >>>>> >>>
>> >>>>> >>> mantovani at 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 at pm.org
>> >>>>> >>> http://mail.pm.org/mailman/listinfo/rio-pm
>> >>>>> >>>
>> >>>>> >>
>> >>>>> >
>> >>>>> _______________________________________________
>> >>>>> Rio-pm mailing list
>> >>>>> Rio-pm at pm.org
>> >>>>> http://mail.pm.org/mailman/listinfo/rio-pm
>> >>>>
>> >>>>
>> >>>> _______________________________________________
>> >>>> Rio-pm mailing list
>> >>>> Rio-pm at pm.org
>> >>>> http://mail.pm.org/mailman/listinfo/rio-pm
>> >>>>
>> >>> _______________________________________________
>> >>> Rio-pm mailing list
>> >>> Rio-pm at pm.org
>> >>> http://mail.pm.org/mailman/listinfo/rio-pm
>> >>>
>> >> _______________________________________________
>> >> Rio-pm mailing list
>> >> Rio-pm at pm.org
>> >> http://mail.pm.org/mailman/listinfo/rio-pm
>> >>
>> > _______________________________________________
>> > Rio-pm mailing list
>> > Rio-pm at pm.org
>> > http://mail.pm.org/mailman/listinfo/rio-pm
>> >
>> _______________________________________________
>> Rio-pm mailing list
>> Rio-pm at pm.org
>> http://mail.pm.org/mailman/listinfo/rio-pm
>
>
> _______________________________________________
> Rio-pm mailing list
> Rio-pm at pm.org
> http://mail.pm.org/mailman/listinfo/rio-pm
>


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