[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: conciseness is power

William James

3/23/2015 11:43:00 PM


Concision is power


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))))))))


Gauche Scheme:

(use srfi-42) ; do-ec

(let1 table (make-hash-table 'string=?)
(call-with-input-file "WORD_LST.txt"
(lambda (port)
(do-ec (: word port read-line)
(hash-table-push! table (sort word) word))))
(for-each
(lambda (words) (print (sort (cdr words))))
(take
(sort-by (hash-table->alist table) (.$ - length))
22)))

(apers apres asper pares parse pears prase presa rapes reaps spare spear)
(alerts alters artels estral laster ratels salter slater staler stelar talers)
(least setal slate stale steal stela taels tales teals tesla)
(palest palets pastel petals plates pleats septal staple tepals)
(estrin inerts insert inters niters nitres sinter triens trines)
(capers crapes escarp pacers parsec recaps scrape secpar spacer)
(anestri antsier nastier ratines retains retinas retsina stainer stearin)
(peris piers pries prise ripes speir spier spire)
(aspers parses passer prases repass spares sparse spears)
(ates east eats etas sate seat seta teas)
(earings erasing gainers reagins regains reginas searing seringa)
(arles earls lares laser lears rales reals seral)
(lapse leaps pales peals pleas salep sepal spale)
(carets cartes caster caters crates reacts recast traces)
(enters nester renest rentes resent tenser ternes treens)
(poster presto repots respot stoper topers tropes)
(canters carnets nectars recants scanter tanrecs trances)
(ester reest reset steer stere terse trees)
(artiest artiste attires iratest ratites striate tastier)
(angriest astringe ganister gantries granites ingrates rangiest)
(lavers ravels salver serval slaver velars versal)
(pinots pintos piston pitons points postin spinto)
3 Answers

Kaz Kylheku

3/24/2015 1:15:00 AM

0

On 2015-03-23, WJ <w_a_x_man@yahoo.com> wrote:
> Concision is power

W00T? None to be seen in this mindless gobbledygook.

> Gauche Scheme:
>
> (use srfi-42) ; do-ec
>
> (let1 table (make-hash-table 'string=?)
> (call-with-input-file "WORD_LST.txt"
> (lambda (port)
> (do-ec (: word port read-line)
> (hash-table-push! table (sort word) word))))
> (for-each
> (lambda (words) (print (sort (cdr words))))
> (take
> (sort-by (hash-table->alist table) (.$ - length))
> 22)))

TXR Lisp:

(let* ((lines (get-lines (open-file "/usr/share/dict/words")))
(groups (group-by [chain copy sort] lines :equal-based)))
[mapdo [iff cdr prinl] [sort (hash-values groups) > length]])

Output:

("carets" "caster" "caters" "crates" "reacts" "recast" "traces")
("pares" "parse" "pears" "rapes" "reaps" "spare" "spear")
("drapes" "padres" "parsed" "rasped" "spared" "spread")

[ snip ]

("part" "rapt" "tarp" "trap")

[ snip ]

("fifties" "iffiest")
("citric" "critic")
("incised" "indices")
("griffin" "riffing")
("dishing" "shindig")
("girding" "ridging")
("bribing" "ribbing")
("Chaitin" "Chianti")
("ignite" "tieing")
("dailies" "liaised")
("infidel" "infield")
("gilding" "gliding")
("kiwi" "wiki")
("cognition's" "incognito's")

Marco Antoniotti

3/24/2015 4:12:00 PM

0

On Tuesday, March 24, 2015 at 2:14:35 AM UTC+1, Kaz Kylheku wrote:
> On 2015-03-23, WJ <w_a_x_man@yahoo.com> wrote:
> > Concision is power
>
> W00T? None to be seen in this mindless gobbledygook.
>
> > Gauche Scheme:
> >
> > (use srfi-42) ; do-ec
> >
> > (let1 table (make-hash-table 'string=?)
> > (call-with-input-file "WORD_LST.txt"
> > (lambda (port)
> > (do-ec (: word port read-line)
> > (hash-table-push! table (sort word) word))))
> > (for-each
> > (lambda (words) (print (sort (cdr words))))
> > (take
> > (sort-by (hash-table->alist table) (.$ - length))
> > 22)))
>
> TXR Lisp:
>
> (let* ((lines (get-lines (open-file "/usr/share/dict/words")))
> (groups (group-by [chain copy sort] lines :equal-based)))
> [mapdo [iff cdr prinl] [sort (hash-values groups) > length]])
>
> Output:
>
> ("carets" "caster" "caters" "crates" "reacts" "recast" "traces")
> ("pares" "parse" "pears" "rapes" "reaps" "spare" "spear")
> ("drapes" "padres" "parsed" "rasped" "spared" "spread")
>
> [ snip ]
>
> ("part" "rapt" "tarp" "trap")
>
> [ snip ]
>
> ("fifties" "iffiest")
> ("citric" "critic")
> ("incised" "indices")
> ("griffin" "riffing")
> ("dishing" "shindig")
> ("girding" "ridging")
> ("bribing" "ribbing")
> ("Chaitin" "Chianti")
> ("ignite" "tieing")
> ("dailies" "liaised")
> ("infidel" "infield")
> ("gilding" "gliding")
> ("kiwi" "wiki")
> ("cognition's" "incognito's")

I just compiled the above message thread with a variation of http://compsoc.dur.ac.uk/w... . I found the results very interesting.

Cheers
--
MA

William James

1/14/2016 9:16:00 AM

0

WJ wrote:

>
> Concision is power
>
>
> 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))))))))

MatzLisp (Ruby):

table = Hash.new{|hash,key| hash[key] = []}
IO.foreach('words'){|word| word.strip!
table[word.each_char.sort.join] << word}
puts table.sort_by{|k,v| -v.size}.take(9).
map{|k,v| "#{v.size} #{v.sort.join ' '}"}

8 caret carte cater certa crate creat react trace
8 erste ester reset reste steer teres terse trees
7 least slate stael stale steal tales tesla
7 apres asper pares parse peras spare spear
6 adel dale deal lade lead leda
6 aril lair lari liar lira rail
6 reins resin rinse risen serin siren
6 aids dais disa idas said sida
6 amino animo naomi nomia omani omnia

--
"If a government uses the instruments of power in its hands for the purpose of
leading a people to ruin, then rebellion is not only the right but also the
duty of every individual citizen."