Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
newbie question
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  11 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Gian Luca Brunetti  
View profile  
 More options Aug 19 2011, 9:37 pm
Newsgroups: comp.lang.scheme
From: Gian Luca Brunetti <gianluca.brune...@gmail.com>
Date: Fri, 19 Aug 2011 18:37:35 -0700 (PDT)
Local: Fri, Aug 19 2011 9:37 pm
Subject: newbie question
Dear all,
a newbie question from a non-programmer.

I sometimes write programs in Perl for my research and I would greatly
like to learn to use Scheme (after having read about it), but I am
stuck at the very beginning with practical problem solving issues.

I would greatly appreciate a help.

I would like to be able to write in the Scheme the kind of script that
I most often find myself writing in Perl, but I lack an appropriate
example. My typical Perl program of this kind opens an ascii file
containing some tables (variable length fields, evidently not S-
expressions), reads it, stores it in memory as an array, one string
for each line; them splits each line at blank spaces (and/or commas)
and, if a certain equality condition is met, it assigns each so-
obtained substring to a sub-array.

Then it loops through the so-obtained nested array to print some of
the sub-arrays' elements to an output file.

I include in the following lines an example of rudimentary perl script
executing the described operations.

I would be very grateful if someone could send me a link to the source
of a simple Scheme program doing the sort of things I described.

Thank you very much in advance.

Best regards

Gian Luca Brunetti

Dipartimento BEST, Politecnico di Milano

_________________________________

sub write_a_new_file()
{
        my $infile  = "/some_address";
        my $outfile = "/some_other_address";
        open( INFILE, "$infile" );
        open( OUTFILE, ">$outfile" );
        my @lines = <INFILE>;
        close(INFILE);
        my $counter = 0;
        foreach my $line (@lines)
        {
                {
                        my @row = split( /\s+/, $line );
                        if ( $row[0] eq "pattern_to_match" )
                        {
                                push( @lines_to_write, [ $row[1], $row[2], $row[3] ] );
                        }
                }
                $counter = $counter++;
        }

        foreach $line_to_write (@lines_to_write)
        {
                print OUTFILE "${$line_to_write}[0], ${$line_to_write}[1], ${$vertex}
[2]\n";
        }


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Grant Rettke  
View profile  
 More options Aug 19 2011, 11:17 pm
Newsgroups: comp.lang.scheme
From: Grant Rettke <gret...@gmail.com>
Date: Fri, 19 Aug 2011 20:17:15 -0700 (PDT)
Local: Fri, Aug 19 2011 11:17 pm
Subject: Re: newbie question
On Aug 19, 8:37 pm, Gian Luca Brunetti <gianluca.brune...@gmail.com>
wrote:

> I would be very grateful if someone could send me a link to the source
> of a simple Scheme program doing the sort of things I described.

#lang racket

(define infile "C:\\tmp\\in.txt")

(define outfile "C:\\tmp\\out.txt")

(define lines (file->lines infile #:mode 'text #:line-mode 'linefeed))

(define lines-to-write (filter (ė (line) (regexp-match #rx"^foo"
line)) lines))

(with-output-to-file outfile (ė () (for-each (ė (line) (printf "~a~n"
line)) lines-to-write)) #:mode 'text #:exists 'append)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gian Luca Brunetti  
View profile  
 More options Aug 20 2011, 6:10 am
Newsgroups: comp.lang.scheme
From: Gian Luca Brunetti <gianluca.brune...@gmail.com>
Date: Sat, 20 Aug 2011 03:10:29 -0700 (PDT)
Local: Sat, Aug 20 2011 6:10 am
Subject: Re: newbie question

> #lang racket

> (define infile "C:\\tmp\\in.txt")

> (define outfile "C:\\tmp\\out.txt")

> (define lines (file->lines infile #:mode 'text #:line-mode 'linefeed))

> (define lines-to-write (filter (ė (line) (regexp-match #rx"^foo"
> line)) lines))

> (with-output-to-file outfile (ė () (for-each (ė (line) (printf "~a~n"
> line)) lines-to-write)) #:mode 'text #:exists 'append)

Thank you very much for the answer. It thoroughly satisfies my
practical needs, but it stirs up one more question by me, due to sheer
curiosity.

I see that the function fine->lines is implementation-dependent (#lang
racket) and takes something like keywords a la Common Lisp (like with-
output-to-file), which also is implementation-dependent.

I wonder if someone has to point me to a non-implementation dependent
solution, maybe r4rs or r5rs, even if much longer.

Thank you again, in advance.

Best regards

Gian Luca Brunetti


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eli Barzilay  
View profile  
 More options Aug 20 2011, 7:06 am
Newsgroups: comp.lang.scheme
From: Eli Barzilay <e...@barzilay.org>
Date: Sat, 20 Aug 2011 07:06:07 -0400
Local: Sat, Aug 20 2011 7:06 am
Subject: Re: newbie question
Gian Luca Brunetti <gianluca.brune...@gmail.com> writes:

> Thank you very much for the answer.

Here's a variant that is somewhat closer to your code (splits on
whitespace, outputs commas, and drops the first word):

  #lang racket
  (define infile "./some_address")
  (define outfile "./some_other_address")
  (define lines (map (λ (s) (regexp-split #px"\\s+" s))
                     (file->lines infile)))
  (define lines-to-write
    (for/list ([line (in-list lines)]
               #:when (equal? (car line) "pattern_to_match"))
      (string-join (cdr line) ", ")))
  (display-lines-to-file lines-to-write outfile)

> It thoroughly satisfies my practical needs, but it stirs up one more
> question by me, due to sheer curiosity.

> I see that the function fine->lines is implementation-dependent
> (#lang racket) and takes something like keywords a la Common Lisp
> (like with- output-to-file), which also is implementation-dependent.

The keywords were not really needed for a direct translation of your
code, but yes -- the whole thing is very racket-dependant.

> I wonder if someone has to point me to a non-implementation dependent
> solution, maybe r4rs or r5rs, even if much longer.

You can try sticking to more common functionality (like using
`read-line' repeatedly, since it's a bit more common), maybe dig
through some SRFIs etc.  But if you're coming from a practical point
of view, you'd better choose an implementation and use what it has to
offer.  (A complete code isn't hard to write for this, you just have
to dump some conveniences like regexps, and reading lines.  New
practical needs will get you even more stuck.)

--
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gian Luca Brunetti  
View profile  
 More options Aug 20 2011, 8:33 am
Newsgroups: comp.lang.scheme
From: Gian Luca Brunetti <gianluca.brune...@gmail.com>
Date: Sat, 20 Aug 2011 05:33:51 -0700 (PDT)
Local: Sat, Aug 20 2011 8:33 am
Subject: Re: newbie question
On Aug 20, 1:06 pm, Eli Barzilay <e...@barzilay.org> wrote:

>   #lang racket
>   (define infile "./some_address")
>   (define outfile "./some_other_address")
>   (define lines (map (λ (s) (regexp-split #px"\\s+" s))
>                      (file->lines infile)))
>   (define lines-to-write
>     (for/list ([line (in-list lines)]
>                #:when (equal? (car line) "pattern_to_match"))
>       (string-join (cdr line) ", ")))
>   (display-lines-to-file lines-to-write outfile)

Thank you very much for the answer. I find it very, very useful.

I think I understand the advantages of using the racket-dependant
functions "for/list" and "in-list" and I see the advantages of
sticking with an implementation.

I guess that my wish to be able to solve my basic practical problems
in Scheme using just a small set of primitives (plus regexes) stems
from an intellectual - even if maybe not entirely rational - need. Due
to which I would still find very useful finding some repositories or
examples featuring small, complete, and best if elemetary-style-
written programs performing the kind of described operations.

I would be very grateful if someone could point me to them, if
existing.

I think this would greatly enhance my understanding of the language.

Thank you again, in advance and in any case.

Best regards

Gian Luca Brunetti


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
toby  
View profile  
 More options Aug 20 2011, 1:56 pm
Newsgroups: comp.lang.scheme
From: toby <t...@telegraphics.com.au>
Date: Sat, 20 Aug 2011 10:56:10 -0700 (PDT)
Local: Sat, Aug 20 2011 1:56 pm
Subject: Re: newbie question
On Friday, 19 August 2011 21:37:35 UTC-4, Gian Luca Brunetti  wrote:

> Dear all,
> ...
> I would like to be able to write in the Scheme the kind of script that
> I most often find myself writing in Perl,

Have you looked at Scheme shell, scsh?

http://www.scsh.net/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gian Luca Brunetti  
View profile  
 More options Aug 20 2011, 4:35 pm
Newsgroups: comp.lang.scheme
From: Gian Luca Brunetti <gianluca.brune...@gmail.com>
Date: Sat, 20 Aug 2011 13:35:19 -0700 (PDT)
Local: Sat, Aug 20 2011 4:35 pm
Subject: Re: newbie question

> Have you looked at Scheme shell, scsh?

Thank you for the suggestion: scsh is really very intesting and the
site has very useful examples.

I was just trying not to bet on specific implementations before I have
learned how to use the very basics of the language; and I think that
to do that I would most of all benefit from reading very simple,
little, non-implementation-dependent practical scripts.

In a sense, I am trying to figure out what I have to learn in
programming, and I would like very much that it turns to be Scheme.

One unmentionable reason for this is that I think id that heavily
indented Scheme code looks insanely great. One more serious reason is
that I think that operations on list suit my brain.

At cognitive level, I am heavily invested in fields other than
programming (building technology) and for a number of reasons I feel
that if I don't manage to use Scheme as soon as possible, this may
never happen. I'd not mind to end up writing in a child-like Scheme
for a while or even forever, provided that I very soon put myself in
the position of being capable to write some working code.

Best regards

Gian Luca Brunetti


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Aaron W. Hsu  
View profile  
 More options Aug 21 2011, 7:20 pm
Newsgroups: comp.lang.scheme
From: "Aaron W. Hsu" <arcf...@sacrideo.us>
Date: Sun, 21 Aug 2011 19:20:01 -0400
Local: Sun, Aug 21 2011 7:20 pm
Subject: Re: newbie question
On Sat, 20 Aug 2011 06:10:29 -0400, Gian Luca Brunetti  

<gianluca.brune...@gmail.com> wrote:
> I wonder if someone has to point me to a non-implementation dependent
> solution, maybe r4rs or r5rs, even if much longer.

R6RS provides read-line, and that is very common among implementations.  
There is also IrRegex which is an R5RS regular expression engine, which  
should do the job just fine as well.

In R6RS, you might do something like this to get the lines from a file:

(define (file->lines path)
   (call-with-input-file path
     (lambda (if)
       (let loop ([lines '()])
         (let ([line (read-line if)])
           (if (eof-object? line)
               (reverse lines)
               (loop (cons line lines))))))))

        Aaron W. Hsu

--
Programming is just another word for the lost Art of Thinking.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gian Luca Brunetti  
View profile  
 More options Aug 21 2011, 11:04 pm
Newsgroups: comp.lang.scheme
From: Gian Luca Brunetti <gianluca.brune...@gmail.com>
Date: Sun, 21 Aug 2011 20:04:56 -0700 (PDT)
Local: Sun, Aug 21 2011 11:04 pm
Subject: Re: newbie question

> (define (file->lines path)
>    (call-with-input-file path
>      (lambda (if)
>        (let loop ([lines '()])
>          (let ([line (read-line if)])
>            (if (eof-object? line)
>                (reverse lines)
>                (loop (cons line lines))))))))

Thank very much for the script.

It is wonderful to look into the procedure "file->lines".

At first I didn't understand the first "let" construct, then I seached
through documentations and I saw that it is a named let.

I am starting to grasp that using this definition of "file-lines",
using "for-each" and "cond" instead of "for/list" and #:when, and
avoiding the use of the function "in-list", I could possibly manage to
modify the interesting solution kindly posted by Eli Barzillay in an
equivalent one implementation-independent, even if less efficient.

Thank you again.

Best regards

Gian Luca Brunetti


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Klemme  
View profile  
 More options Aug 22 2011, 1:28 pm
Newsgroups: comp.lang.scheme
From: Robert Klemme <shortcut...@googlemail.com>
Date: Mon, 22 Aug 2011 19:28:33 +0200
Local: Mon, Aug 22 2011 1:28 pm
Subject: Re: newbie question
On 08/20/2011 03:37 AM, Gian Luca Brunetti wrote:

I see one issue with your approach: if my Perl doesn't fail me you read
in the whole input file into memory, then determine which lines to print
and finally print them.  This can have dramatic effects if the input
file is large.  Memory wise an approach where you only act on one line
at a time is more efficient.

Not to spoil the fun (and sorry for posting Ruby code in a Scheme forum)
but here is how that single line approach would look like in Ruby:

File.open "/some_other_address", 'w' do |out|
   File.foreach "/some_address" do |line|
     row = line.split /\s+/
     out.puts(row[0..2].join(', ')) if row[0] == "pattern_to_match"
   end
end

For a regexp match you would do something like this

     out.puts(row[0..2].join(', ')) if /rx/ =~ row[0]

In case you need to correlate information from different lines to
determine what to print this approach doesn't work of course.

Kind regards

        robert


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gian Luca Brunetti  
View profile  
 More options Aug 23 2011, 7:21 pm
Newsgroups: comp.lang.scheme
From: Gian Luca Brunetti <gianluca.brune...@gmail.com>
Date: Tue, 23 Aug 2011 16:21:39 -0700 (PDT)
Local: Tues, Aug 23 2011 7:21 pm
Subject: Re: newbie question

> File.open "/some_other_address", 'w' do |out|
>    File.foreach "/some_address" do |line|
>      row = line.split /\s+/
>      out.puts(row[0..2].join(', ')) if row[0] == "pattern_to_match"
>    end
> end

Thank you for the observation.

The Ruby code is very short.

However, in this case, it is better for me to load the file content in
one whole data structure, to be able to modify it afterwards in
various ways.

Best regards

Gian Luca Brunetti


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »