Changeset 29354 in project


Ignore:
Timestamp:
07/14/13 16:03:06 (6 years ago)
Author:
svnwiki
Message:

Anonymous wiki edit for IP [85.164.10.19]: Split the second line-counting example into its own line, added Python example

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/language-comparison

    r29353 r29354  
    6363        count
    6464        (loop (+ count 1)))))))</enscript>
    65 
    66 If you don't want to keep an entire line in memory at any time:
    67 
    68 <enscript highlight="scheme">(with-input-from-file path
    69   (lambda ()
    70     (let loop ((count 0) (c (read-char)))
    71       (if (eof-object? c)
    72         count
    73         (loop (if (char=? c #\newline) (+ count 1) count) (read-char))))))</enscript>
    7465</td>
    7566<td><enscript highlight="c">FILE *in = fopen(path, "r");
     
    8879close $fh;
    8980</enscript</td>
     81</tr>
     82
     83<tr>
     84<td>
     85Counting lines in a file if you don't want to keep an entire line in memory at any time:
     86<td>
     87<enscript highlight="scheme">(with-input-from-file path
     88  (lambda ()
     89    (let loop ((count 0) (c (read-char)))
     90      (if (eof-object? c)
     91        count
     92        (loop (if (char=? c #\newline) (+ count 1) count) (read-char))))))</enscript>
     93</td>
     94<td>...</td>
     95<td>
     96<enscript highlight="python">f=open("file.txt", "rt")
     97count=0
     98while True:
     99    c= f.read(1)
     100    if c == "":
     101        break
     102    if c == "\n":
     103        count = count + 1</enscript>
     104</td>
     105<td>...</td>
    90106</tr>
    91107
Note: See TracChangeset for help on using the changeset viewer.