POV-Ray : Newsgroups : povray.off-topic : how to scheme a cat up? : Re: how to scheme a cat up? Server Time
5 Sep 2024 15:27:46 EDT (-0400)
  Re: how to scheme a cat up?  
From: Daniel Bastos
Date: 19 Aug 2009 11:00:49
Message: <4a8c13a1$1@news.povray.org>
In article <web.4a8b9c8c606eeda4cb3dd3d0@news.povray.org>,
nemesis wrote:

> Daniel Bastos <dbastos+0### [at] toledocom> wrote:
>> #!/usr/bin/env mzscheme
>> (module cat scheme/base
>>
>> (define (interact f)
>>   (let ([ln (read-line)])
>>      (cond
>>       [(not (eq? ln eof))
>>          (and
>>     (printf "~a\n" (f ln))
>>     (interact f))])))
>>
>> (define (id x) x)
>>
>> (define (cat files)
>>   (for-each
>>    (lambda (x)
>>      (with-input-from-file x
>>        (lambda ()
>>   (interact id))))
>>    files))
>>
>> ;main
>> (cat (vector->list (current-command-line-arguments)))
>> )
>>
>> With with-input-from-file, we get the stdin connected to the file
>> opened, so no changes in interact were required.
>
> That's the PLT way.  here's another:

By the way, I'm studying Scheme through PLT due to a clue from
authority, which I gathered. I absolutely know nothing about
implementations, but I saw this guy who seems to do his homework using
PLT, so instead of MIT, I picked PLT. Clearly at this point it doesn't
matter, but I already think about portability. Can you give me a word
on portability? On UNIX, I use POSIX as a reference, even though I
don't consider it a standard in a profound way; it works as a document
to help me understand what is like to be common out there. I'd like
that in Scheme.

>
> #!r6rs
> (import (rnrs base)(rnrs io simple)(rnrs programs))
>
> ; simple and straightforward standard R6RS scheme solution
> (define (cat files)
>   (for-each
>     (lambda (file)
>       (call-with-input-file file
>         (lambda (port)
>           ; no simple line-oriented IO in standard R6RS
>           ; so you either write one or loop char-by-char
>           (let go ((c (read-char port)))
>             (if (not (eof-object? c))
>               (begin
>                 (write-char c)
>                 (go (read-char port))))))))
>     files))
>
> (cat (cdr (command-line)))

I've kinda been able to read this; which is cool. But I don't really
get the purpose of ``go'' here.

>> Lisp likes might be hackestables.
>
> I just love Lisp, Scheme in particular.  Minimalist like C, only much higher
> level.  Incidentally, the only two languages I truly enjoy, for different
> purposes sure.

Same here.

> yes, I'm aware the C solution for this particular example would be way
> shorter... :P

I see it as the same thing; there isn't anything very high level about
cat that could really be taken advantage of a very high level language.

Here's the essence of cat from AT&T UNIX version 7.

while (--argc > 0) {
  if (fflg || (*++argv)[0]=='-' && (*argv)[1]=='\0')
  	fi = stdin;
  else {
  	if ((fi = fopen(*argv, "r")) == NULL) {
  		fprintf(stderr, "cat: can't open %s\n", *argv);
  		continue;
  	}
  }
  fstat(fileno(fi), &statb);
  if (statb.st_dev==dev && statb.st_ino==ino) {
  	fprintf(stderr, "cat: input %s is output\n",
  	   fflg?"-": *argv);
  	fclose(fi);
  	continue;
  }
  while ((c = getc(fi)) != EOF)
  	putchar(c);
  if (fi!=stdin)
		fclose(fi);
}

Hey, looking at this very old cat, I can see that Warp is going to say
that our cats aren't cats after all; we don't read the stdin. And
he'll be right.


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.