From: ········@gmail
Subject: Rewrap "> " lines from file ?
Date: 
Message-ID: <1219024612.124151@vasbyt.isdsl.net>
I want to promote the idea that 'we should use a higher level langauge,
like lisp or scheme'.   But I don't yet have enough fluency to give a good
example.   A suitable and not too big a task, to serve as a comparison
against the currently used languages would be:

rearrange a small text file, so that:
* all lines of length > 78 are wrapped to len < 75, with breaks at 
    word-boundries;
* the "> " strings which start each line are replaced to the beginning
  of the new shorter lines;

I guess something like this might do it:

* delete all line-breaks and "> ";
* Initialise counter/s; 
* While Not eof do
   write("> ");
   While lineNotFull do
       write(NextWord);
       Inc(lineLen by wordLen)
   EndWhile lineNotFull;
EndWhile Not eof.

I've got elisp ver xxx to start with -  and an undocumented Scheme,
which runs a graphics application, which I've tested on simple functions,
but I don't know the syntax to:  ' read/write in/out text from/to a file'.

Please provide some source for the folowing skeleton, which could get 
me started:

1. Init files if needed;
2. For lines = 1st till last or eol do
   ReadLine from InFile & count chars;
   To OutFile write "line n =", lines
3. To OutFile write "Done".

== TIA.

From: Paul Donnelly
Subject: Re: Rewrap "> " lines from file ?
Date: 
Message-ID: <87myjbkoeo.fsf@plap.localdomain>
········@gmail writes:

> I want to promote the idea that 'we should use a higher level langauge,
> like lisp or scheme'.   But I don't yet have enough fluency to give a good
> example.   A suitable and not too big a task, to serve as a comparison
> against the currently used languages would be:
>
> rearrange a small text file, so that:
> * all lines of length > 78 are wrapped to len < 75, with breaks at 
>     word-boundries;
> * the "> " strings which start each line are replaced to the beginning
>   of the new shorter lines;
>
> I guess something like this might do it:
>
> * delete all line-breaks and "> ";
> * Initialise counter/s; 
> * While Not eof do
>    write("> ");
>    While lineNotFull do
>        write(NextWord);
>        Inc(lineLen by wordLen)
>    EndWhile lineNotFull;
> EndWhile Not eof.
>
> I've got elisp ver xxx to start with -  and an undocumented Scheme,
> which runs a graphics application, which I've tested on simple functions,
> but I don't know the syntax to:  ' read/write in/out text from/to a file'.
>
> Please provide some source for the folowing skeleton, which could get 
> me started:
>
> 1. Init files if needed;
> 2. For lines = 1st till last or eol do
>    ReadLine from InFile & count chars;
>    To OutFile write "line n =", lines
> 3. To OutFile write "Done".
>
> == TIA.

If you've got elisp, you've probably got the rest of Emacs too, right?
Why not take a look at the source for FILL-REGION-AS-PARAGRAPH? It
includes a lot of code for handling things you don't plan to handle,
but what you'll see is that the business end of the function is mostly
made up of editor commands. In a high-level language, you can create
abstractions that let you work at the level you wish to, in this case,
text editing commands. If you can easily make the abstractions you
want, then the language you are using is high-level enough. Which is
my roundabout way of saying that I don't think Lisp has any particular
advantage in this task, except for intangibles like comfort and
familiarity for and to Lispers. I mean, look at your
pseudocode. Despite being slightly wrong, what would be easier about
writing it in Lisp than in some other language?

If you want to promote this idea, it would be better to routinely
dazzle your peers by writing better programs more quickly in
Lisp. That's much more persuasive than picking some arbitrary
benchmark program because, for most tasks, it's the little things that
count, like being able to put an IF form anywhere a value is expected.
For some tasks, there might be a trick you can do in Lisp that makes
it worlds easier, but, even so, that trick will probably be a small
part of the program and not a big deal to a person who isn't looking
to be convinced to learn a new language. These are little things that
are hard to see from a single code comparison, but whose cumulative
impact over several programs is impossible to ignore.
From: ········@gmail
Subject: Re: Rewrap "> " lines from file ?
Date: 
Message-ID: <1219062223.206144@vasbyt.isdsl.net>
In article <··············@plap.localdomain>, Paul Donnelly <·············@sbcglobal.net> wrote: 

> ········@gmail writes:
> 
> > I want to promote the idea that 'we should use a higher level langauge,
> > like lisp or scheme'.   But I don't yet have enough fluency to give a good
> > example.   A suitable and not too big a task, to serve as a comparison
> > against the currently used languages would be:
> >
> > rearrange a small text file, so that:
> > * all lines of length > 78 are wrapped to len < 75, with breaks at 
> >     word-boundries;
> > * the "> " strings which start each line are replaced to the beginning
> >   of the new shorter lines;
> >
> > I guess something like this might do it:
> >
> > * delete all line-breaks and "> ";
> > * Initialise counter/s; 
> > * While Not eof do
> >    write("> ");
> >    While lineNotFull do
> >        write(NextWord);
> >        Inc(lineLen by wordLen)
> >    EndWhile lineNotFull;
> > EndWhile Not eof.
> >
> > I've got elisp ver xxx to start with -  and an undocumented Scheme,
> > which runs a graphics application, which I've tested on simple functions,
> > but I don't know the syntax to:  ' read/write in/out text from/to a file'.
> >
> > Please provide some source for the folowing skeleton, which could get 
> > me started:
> >
> > 1. Init files if needed;
> > 2. For lines = 1st till last or eol do
> >    ReadLine from InFile & count chars;
> >    To OutFile write "line n =", lines
> > 3. To OutFile write "Done".
> >
> > == TIA.
> 
> If you've got elisp, you've probably got the rest of Emacs too, right?

No ! I was able to 'chroot <otherPartitionInstallation> info emacs'
 and info elisp. And the [other partition/installation] info elisp 
apparently uses emacs documentation ?

I don't want to re-boot to the other partition now.
Doing is not as easy as talking.
Can't someone just write my 3 step algorithm in lisp/scheme ?!

> Why not take a look at the source for FILL-REGION-AS-PARAGRAPH? It
> includes a lot of code for handling things you don't plan to handle,
> but what you'll see is that the business end of the function is mostly
> made up of editor commands. In a high-level language, you can create
> abstractions that let you work at the level you wish to, in this case,
> text editing commands. If you can easily make the abstractions you
> want, then the language you are using is high-level enough. Which is
> my roundabout way of saying that I don't think Lisp has any particular
> advantage in this task, except for intangibles like comfort and
> familiarity for and to Lispers. I mean, look at your pseudocode. 
> Despite being slightly wrong, what would be easier about
> writing it in Lisp than in some other language?
> 
> If you want to promote this idea, it would be better to routinely
> dazzle your peers by writing better programs more quickly in
> Lisp. That's much more persuasive than picking some arbitrary
> benchmark program because, for most tasks, it's the little things that
> count, like being able to put an IF form anywhere a value is expected.
> For some tasks, there might be a trick you can do in Lisp that makes
> it worlds easier, but, even so, that trick will probably be a small
> part of the program and not a big deal to a person who isn't looking
> to be convinced to learn a new language. These are little things that
> are hard to see from a single code comparison, but whose cumulative
> impact over several programs is impossible to ignore.
> 
From: Thomas A. Russ
Subject: Re: Rewrap "> " lines from file ?
Date: 
Message-ID: <ymi63pyoy9o.fsf@blackcat.isi.edu>
········@gmail writes:

> I want to promote the idea that 'we should use a higher level langauge,
> like lisp or scheme'.   But I don't yet have enough fluency to give a good
> example.   A suitable and not too big a task, to serve as a comparison
> against the currently used languages would be:
> 
> rearrange a small text file, so that:
> * all lines of length > 78 are wrapped to len < 75, with breaks at 
>     word-boundries;
> * the "> " strings which start each line are replaced to the beginning
>   of the new shorter lines;

I note below that you mention elisp.  I would suggest also considering
Common Lisp, as that is much easier to use for stand-alone processing.
Is there even any elisp processor that doesn't require Emacs to be
running?

Also, you may wish to look at some additional libraries such as cl-ppcre
(regular expressions) and maybe the ubiquitous SPLIT-SEQUENCE code
discussions.

Also, it would seem that you will want to have some method of detecting
paragraph breaks.  This can be tricky, because there might not be any
really good definition for this.  A blank line works well, but short
lines may be a bit trickier.  Especially if something has previously
re-formatted the lines in a brain-dead fashion so you end up with

    > This is a long line that was just a bit too long so it
    > was
    > reformatted with only a single word on the follow-up line
    > but
    > that is enough to render paragraph detection difficult.

Anyway, once you have paragraphs, you can use a fancy Common Lisp format
expression to output your final form.  It assumes that there is a list
of string "tokens" to be output.

  (defun format-paragraph (stream token-list)
     (format stream "~%>~{~<~%>~1,75:; ~A~>~}~%" token-list))

A fancier form of this function would allow you to specify the prefix
and line length, but that would mean adding a step to generate the
format control string.  I omitted that to make the structure clearer.

  (defun format-paragraph (stream token-list
                            &key (line-length 75) (prefix ">"))
    (let ((line-format (format nil "~~%~A~~{~~<~~%~A~~1,~A:; ~~A~~>~~}~~%"
                               prefix prefix line-length)))
       (format stream line-format token-list)))


If you really want to show the benefits of using a higher-level
language, then what you should do is write a rule-based reformatter, and
use lisp to implement the engine that runs the rules to create the new
format.  That would make it easier to update, as you would just need to
change the formatting rules.  It would also allow for some
experimentation with the problem I noted above.  It is also an example
of the common Lisp paradigm of creating a domain-specific language and
an intepreter/compiler for that language.


-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Rob Warnock
Subject: Re: Rewrap "> " lines from file ?
Date: 
Message-ID: <M_KdnYrqUvciWDHVnZ2dnUVZ_iydnZ2d@speakeasy.net>
<········@gmail> wrote:
+---------------
| I want to promote the idea that 'we should use a higher level langauge,
| like lisp or scheme'.   But I don't yet have enough fluency to give a good
| example.   A suitable and not too big a task, to serve as a comparison
| against the currently used languages would be:
| 
| rearrange a small text file, so that:
| * all lines of length > 78 are wrapped to len < 75, with breaks at 
|     word-boundries;
| * the "> " strings which start each line are replaced to the beginning
|   of the new shorter lines;
+---------------

As much as I like Lisp, this tiny task simply cries out
for a "one-liner" shell script!

     sed -s 's/^> //' ······@"} | fmt -75 |  sed -s 's/^/> /'


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607
From: Bakul Shah
Subject: Re: Rewrap "> " lines from file ?
Date: 
Message-ID: <48AD8D47.2090706@bitblocks.com>
Rob Warnock wrote:
> <········@gmail> wrote:
> +---------------
> | I want to promote the idea that 'we should use a higher level langauge,
> | like lisp or scheme'.   But I don't yet have enough fluency to give a good
> | example.   A suitable and not too big a task, to serve as a comparison
> | against the currently used languages would be:
> | 
> | rearrange a small text file, so that:
> | * all lines of length > 78 are wrapped to len < 75, with breaks at 
> |     word-boundries;
> | * the "> " strings which start each line are replaced to the beginning
> |   of the new shorter lines;
> +---------------
> 
> As much as I like Lisp, this tiny task simply cries out
> for a "one-liner" shell script!
> 
>      sed -s 's/^> //' ······@"} | fmt -75 |  sed -s 's/^/> /'
> 

Your one-liner doesn't meet the spec. The way I read it, all long lines
are folded to be < 75 chars long but "> " is prepended only for folded
lines that start with a "> ".  The OP doesn't specify it but I think
he'd want the "> " prepended lines to be < 75 chars long as well.
From: Icarus Sparry
Subject: Re: Rewrap "> " lines from file ?
Date: 
Message-ID: <48ad9657$0$17228$742ec2ed@news.sonic.net>
On Wed, 20 Aug 2008 20:32:47 -0500, Rob Warnock wrote:

> <········@gmail> wrote:
> +---------------
> | I want to promote the idea that 'we should use a higher level
> langauge, | like lisp or scheme'.   But I don't yet have enough fluency
> to give a good | example.   A suitable and not too big a task, to serve
> as a comparison | against the currently used languages would be: |
> | rearrange a small text file, so that: | * all lines of length > 78 are
> wrapped to len < 75, with breaks at |     word-boundries;
> | * the "> " strings which start each line are replaced to the beginning
> |   of the new shorter lines;
> +---------------
> 
> As much as I like Lisp, this tiny task simply cries out for a
> "one-liner" shell script!
> 
>      sed -s 's/^> //' ······@"} | fmt -75 |  sed -s 's/^/> /'
> 
> 
> -Rob
> 
> -----
> Rob Warnock			<····@rpw3.org>
> 627 26th Avenue			<URL:http://rpw3.org/> San Mateo, 
CA 94403	
> (650)572-2607

There is also a very nice program "par" http://www.nicemice.net/par that 
does everything requested and much more. From the manual page examples


       Before:

          /*   We the people of the United States, */
          /* in order to form a more perfect union, */
          /* establish justice, */
          /* insure domestic tranquility, */
          /* provide for the common defense, */
          /* promote the general welfare, */
          /* and secure the blessing of liberty */
          /* to ourselves and our posterity, */
          /* do ordain and establish the Constitution */
          /* of the United States of America. */

       After "par 59":

          /*   We the people of the United States, in      */
          /* order to form a more perfect union, establish */
          /* justice, insure domestic tranquility, provide */
          /* for the common defense, promote the general   */
          /* welfare, and secure the blessing of liberty   */
          /* to ourselves and our posterity, do ordain     */
          /* and establish the Constitution of the United  */
          /* States of America.                            */

Most web searches for this program end up at
http://www.cs.berkeley.edu/~amc/Par but this no longer seems to be valid.