[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: Symbolic Expression Counting CONTAINS SPOILER.

William James

5/12/2015 11:11:00 AM

Barry Margolin wrote:

> >why use perl to process lisp code?
> >presumably you are at least somewhat well versed in Lisp - in which case
> >it should be much easier to do the same in lisp, not perl.
>
> We've had this discussion before. Perl was designed to make file scanning
> and pattern matching easy. The programs we've been designing in this
> thread are precisely the kind of application that Perl is intended for.
> I'm well-enough versed in Lisp to know that the equivalent of:
>
> while (<>) { # Loop over input lines
> counter++ if /^\s*\(/; # if first non-white character is open-paren, count it
> }
>
> would be much more verbose without being significantly more expressive.

Gauche Scheme:

(use gauche.generator :only (file->line-generator))
(use srfi-42 :only (sum-ec))

(sum-ec
(:generator line (file->line-generator "tmp.txt"))
(if (#/^\s*\(/ line))
1)

===>
25

--
Arrogant and isolated, the elite treat the ordinary citizens of the empire as
slaves and potential enemies.
www.kolumbus.fi/aquilon/america-middle-class-and-the-end-of-growth.htm
3 Answers

William James

5/12/2015 3:30:00 PM

0

WJ wrote:

> (:generator line (file->line-generator "tmp.txt"))

Shorter, but somewhat slower:

(: line (file->line-generator "tmp.txt"))

William James

5/13/2015 4:48:00 AM

0

Pierre Mai wrote:

> > We've had this discussion before. Perl was designed to make file scanning
> > and pattern matching easy. The programs we've been designing in this
> > thread are precisely the kind of application that Perl is intended for.
> > I'm well-enough versed in Lisp to know that the equivalent of:
> >
> > while (<>) { # Loop over input lines
> > counter++ if /^\s*\(/; # if first non-white character is open-paren, count i
> t
> > }
> >
> > would be much more verbose without being significantly more expressive.
> -------------
>
> If it weren't for the regexp, which needs a comment of 10 words to
> explain what it does, and which is easy to get wrong (either comment,
> or regexp that is), I could believe that statement, but so I have to
> humbly disagree ;)
>
> Anyways, this is Common Lisp:
>
> (loop for line = (read-line *standard-input* nil nil)
> while line
> count (starts-with (left-trim-whitespace line) "("))
>
> This uses two trivial string functions which are probably part of
> every working CL user[1]. With an extensible LOOP facility, this could
> even be clarified further...
.....
> Footnotes:
> [1] Here are some very simple, inefficient sample implementations:
> (defun starts-with (string substring)
> "Detect whether the `string' starts with `substring'."
> (eql 0 (search substring string)))
>
> (defun left-trim-whitespace (string &optional (ws-bag '(#\Space #\Tab)))
> "Trims any whitespace characters (i.e. characters in `ws-bag') from
> the left side of `string'."
> (string-left-trim ws-bag string))

Gauche Scheme:

(use srfi-13 :only (string-trim string-prefix?))

(count
(lambda (line) (string-prefix? "(" (string-trim line)))
(generator->lseq read-line))

--
As more and more power gravitates in the capital, the more vital it becomes for
the corporate America to secure its interests by corrupting that seat of power.
www.kolumbus.fi/aquilon/america-middle-class-and-the-end-of-growth.htm

William James

1/15/2016 9:51:00 AM

0

WJ wrote:

> Barry Margolin wrote:
>
> > > why use perl to process lisp code?
> > > presumably you are at least somewhat well versed in Lisp - in which case
> > > it should be much easier to do the same in lisp, not perl.
> >
> > We've had this discussion before. Perl was designed to make file scanning
> > and pattern matching easy. The programs we've been designing in this
> > thread are precisely the kind of application that Perl is intended for.
> > I'm well-enough versed in Lisp to know that the equivalent of:
> >
> > while (<>) { # Loop over input lines
> > counter++ if /^\s*\(/; # if first non-white character is open-paren, count it
> > }
> >
> > would be much more verbose without being significantly more expressive.
>
> Gauche Scheme:
>
> (use gauche.generator :only (file->line-generator))
> (use srfi-42 :only (sum-ec))
>
> (sum-ec
> (:generator line (file->line-generator "tmp.txt"))
> (if (#/^\s*\(/ line))
> 1)
>
> ===>
> 25

MatzLisp (Ruby):

IO.foreach("tmp.txt").count{|line| line.lstrip[0] == "("}

--
Government is not reason, it is not eloquence, it is force; like fire, a
troublesome servant and a fearful master. Never for a moment should it be left
to irresponsible action. --- George Washington, speech of January 7, 1790
Use this [sword] for me, if I rule well; if not, against me. --- Trajan