unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* I/O, modules
@ 2012-11-13  5:57 thorsopia
  2012-11-13  9:31 ` Ludovic Courtès
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: thorsopia @ 2012-11-13  5:57 UTC (permalink / raw)
  To: guile-user

Hello,

Could you show me some trivial programs related to I/O (e.g. read from
file, convert to uppercase, write to another file)?

This page [0] doesn't list a function that can be used to read the whole
file at once. Is there a "read-string" function? What about "read-port"?

Should I import some module to get the above functions? Which one?

BTW, I have a problem with modules. How to check what can be imported?

For example:

I created two files in the same dir:

test-scm.scm:

(define-module (test-scm)
  #:export (test-func))

(define (test-func x)
  (+ x 1))

import-test.scm:

(define-module (import-test)
  #:use-module (test-scm))

(display (test-func 2))

Why does "guile import-test.scm" raise "ERROR: no code for module
(test-scm)"?

[0] https://gnu.org/software/guile/manual/guile.html#Reading





^ permalink raw reply	[flat|nested] 8+ messages in thread
* Re: I/O, modules
@ 2012-11-13 11:45 tantalum
  2012-11-13 23:03 ` Ian Price
  0 siblings, 1 reply; 8+ messages in thread
From: tantalum @ 2012-11-13 11:45 UTC (permalink / raw)
  To: guile-user

Hi

The (ice-9 streams) module could be interesting, too.
http://www.gnu.org/software/guile/manual/html_node/Streams.html

Example:
-------------
(import
  (ice-9 streams)
  (ice-9 rdelim))

(define (port->line-stream port handle-delim)
  (port->stream port (lambda (port) (read-line port handle-delim))))

(define (file-upcase source-path target-path)
  (call-with-input-file source-path
    (lambda (source-file)
      (call-with-output-file target-path
        (lambda (target-file)
          (stream-for-each
            (lambda (line) (display (string-upcase line) target-file))
            (port->line-stream source-file (quote concat))))))))


;test
(system "echo test >/tmp/file-upcase-test")
(file-upcase "/tmp/file-upcase-test" "/tmp/file-upcase-test-out")
--------------
Where
  - Handle-delim specifies what should happen with the newline character. Default is to remove it
  - And display is Schemes standard string write procedure

I don't know of any existing generic file to string procedure.
Something similar to what I have used is:
---------------
(import
  (ice-9 streams)
  (ice-9 rdelim))

(define (port->line-stream port handle-delim)
  (port->stream port (lambda (port) (read-line port handle-delim))))

(define (file->string file)
  (apply string-append
    (stream->list (port->line-stream file (quote concat)))))

(define (file-from-path->string path)
  (call-with-input-file path file->string))

(display (file-from-path->string "/home/jkalbhenn/temp/temp.scm"))
-------------

Julian



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2012-11-13 23:03 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-11-13  5:57 I/O, modules thorsopia
2012-11-13  9:31 ` Ludovic Courtès
2012-11-13  9:53 ` Thien-Thi Nguyen
2012-11-13 14:31 ` Mark H Weaver
2012-11-13 14:50   ` Mike Gran
2012-11-13 22:29   ` Ian Price
  -- strict thread matches above, loose matches on Subject: below --
2012-11-13 11:45 tantalum
2012-11-13 23:03 ` Ian Price

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).