[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: conciseness [concision] is power

William James

9/29/2015 6:50:00 PM

Pascal Bourguignon wrote:

> > As an exercise, I wrote code to find anagrams in a one-word-per-line
> > dictionary. See http://blogs.pragprog.com/cgi-bin/pra...
> > Practices/Kata/KataSix.rdoc. Within the equivalence classes, I sorted
> > the words in ascending order and sorted classes in descending order of
> > size. See the code below:
> > [...]
> > For comparison, I wrote Perl code to do the same task:
> >
> > #! /usr/bin/perl
> >
> > sub longest_first {
> > my $bylen = scalar(@$b) <=> scalar(@$a);
> >
> > if ($bylen) {
> > return $bylen;
> > }
> > else {
> > my $byalpha;
> > for (0 .. $#$a) {
> > $byalpha = $a->[$_] cmp $b->[$_];
> > return $byalpha if $byalpha;
> > }
> > 0;
> > }
> > }
> >
> > @ARGV = "words721.txt" unless @ARGV;
> >
> > my %group;
> > while (<>) {
> > s/\s+\z//;
> >
> > push @{ $group{ join "", sort split //, $_ } } => $_;
> > }
> >
> > my @anagrams = map [ sort @$_ ], grep @$_ >= 2, values %group;
> >
> > for (sort longest_first @anagrams) {
> > print join(" " => @$_), "\n";
> > }
>
>
>
>
> (defpackage :words (:use))
> (format
> t "~{~{~A~^ ~}~%~}"
> (let ((table (make-hash-table)))
> (mapcar
> (lambda (k) (sort (gethash k table) (function string-lessp)))
> (sort (delete-duplicates
> (with-open-file (words "/usr/share/dict/words")
> (loop :for w = (read-line words nil nil)
> :for k = (intern (sort (string-upcase w)
> (function char<=)) :words)
> :while w :do (push w (gethash k table nil)) :collect k)))
> (lambda (a b) (>= (length (string a)) (length (string b))))))))
>
>
> perl Region has 32 lines, 534 characters
> lisp Region has 13 lines, 613 characters

MatzLisp (Ruby):

IO.read("words").split.group_by{|w| w.split("").sort.join}.
sort_by{|key,group| -key.size}.each{|key,group| puts group.sort.join " "}

==== Contents of the "words" file ====
rat
tar
roam
more
flair
mare
tub
ream
but
frail
======================================

Output:

flair frail
mare ream
more
roam
rat tar
but tub

--
He has nothing but kind sentiments for those who would destroy his home and
family.... He is universally tolerant.... If he has any principles, he keeps
them well concealed.... He is, to the extent of his abilities, exactly like
the next citizen, who, he trusts, is trying to be exactly like him: a faceless,
characterless putty-man. --- Father Feeney; "Should Hate Be Outlawed?"