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

Nilson Santos Figueiredo Jr. acid06 em gmail.com
Quarta Fevereiro 3 12:08:48 PST 2010


Como eu notei que o exemplo original emite warnings, então abaixo vai
minha tentativa que não liga para warnings (65 chars):

sub drop{($n,$w)=@_;$n=0if$n<0;ref$w?@$w[$n..$#{$w}]:substr$w,$n}

Essa causa os mesmos warnings do exemplo original do Mantovani. Na
verdade, ela tem um erro, em um ponto ela retorna um undef ao invés de
string vazio.

-Nilson

2010/2/3 Nilson Santos Figueiredo Jr. <acid06 at gmail.com>:
> Essa não funciona pra todos os casos de testes (se eu entendi direito).
> Estou usando isso para teste:
>
> print join ',', drop(-1, [1..6]);
> print "\n";
> print join ',', drop(0, [1..6]);
> print "\n";
> print join ',', drop(2, [1..6]);
> print "\n";
> print join ',', drop(10, [1..6]);
> print "\n";
> print join ',', drop(-1, "mantovani");
> print "\n";
> print join ',', drop(0, "mantovani");
> print "\n";
> print join ',', drop(2, "mantovani");
> print "\n";
> print join ',', drop(10, "mantovani");
> print "\n";
>
> O resultado esperado é:
>
> <result>
> 1,2,3,4,5,6
> 1,2,3,4,5,6
> 3,4,5,6
>
> mantovani
> mantovani
> ntovani
>
> </result>
>
> Mas eu posso ter entendido errado também.
>
> -Nilson
>
> 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
>>
>


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