Input & output

Chicken C Python Perl
Formatting and printing to stdout (format #t "I want ~A ~A~%" count desc) printf("I want %d %s\n", count, desc); print("I want {} {}\n".format(count, desc)) say "I want $count $desc";
Printing to stderr (format (current-error-port) "Ooops~%") fprintf(stderr, "Ooops\n"); ... say STDERR 'Ooops';
Reading a line from stdin (read-line) char buffer[1024]; fgets(buffer, sizeof(buffer), stdin); ... <STDIN>
Opening a file for input (open-input-file path) fopen(path, "r"); ... open my $fh, '<', $path;
Opening a file for output (open-output-file path) fopen(path, "w"); ... open my $fh, '>', $path;
Count lines in a file (with-input-from-file path (lambda () (let loop ((count 0)) (if (eof-object? (read-line)) count (loop (+ count 1))))))) FILE *in = fopen(path, "r"); int c, count = 0; if (!in) handle_error(); while ((c = fgetc(in)) != EOF) if (c == '\n') lines ++; fclose(in); return count; ... open my $fh, '<', $path; my $count = >$fh<; close $fh;
Counting lines in a file if you don't want to keep an entire line in memory at any time: (with-input-from-file path (lambda () (let loop ((count 0) (c (read-char))) (if (eof-object? c) count (loop (if (char=? c #\newline) (+ count 1) count) (read-char)))))) ... f=open("file.txt", "rt") count=0 while True: c= f.read(1) if c == "": break if c == "\n": count = count + 1 ...
Write a file with some numbers In practice you'd probably collapse the calls to format and equivalent functions in a single call. (with-output-to-file path (lambda () (format #t "~A~%" 1) (format #t "~A~%" 2) (format #t "~A~%" 3))) FILE *out = fopen(path, "w"); if (!out) handle_error(); fprintf(out, "%d\n", 1); fprintf(out, "%d\n", 2); fprintf(out, "%d\n", 3); if (!fclose(out)) handle_error(); ... open my $fh, '>', $path; say $fh $_ foreach (1 .. 3); close $fh;

Usage in a compiled program

Some of the procedures listed here, e.g. (read-line) and (format ...), are in the extras namespace, so if you are going to compile your program with csc, you'll need to add the following to your source file:

(use extras)

Lists

Vectors

Declaring a vector of numbers An anonymous vector is declared as: `#(1 2 ,x ,y 5) To declare it as a global variable: (define *my-vector* `#(1 2 ,x ,y 5)) Or, to declare it locally for some body of code: (let ((vector `#(1 2 ,x ,y 5))) body-of-code ...) int vector[] = { 1, 2, x, y, 5 };
Obtaining the size of a vector (vector-length my-vector) If the size of the vector is known at compile time: sizeof(my_vector)/sizeof(my_vector[0]) This often does not happen. If the vector is terminated with a special element (such as a NULL pointer or —as in the case of strings— a 0), you'll have to traverse it: int i; for (i = 0; my_vector[i] != SPECIAL_ELEMENT; i ++);
Printing a vector If you don't care that much about the format used: (format "~A~%" vector) If you need a newline after each element and can afford the vector to be converted to a list: (use format-modular) (format "~{~A~%~}" (vector->list vector)) Without converting the vector to list: (do ((i 0 (+ i 1))) ((= i (vector-length vector))) (format #t "~A~%" (vector-ref vector i))) int i; for (i = 0; i < n; i ++) printf("%d\n", vector[i]); ...

Hash tables

Creating a hash table

Chicken

(make-hash-table)

If you need to populate the hash table, you'll probably use alist->hash-table:

(alist->hash-table '((1 . "uno") (2 . "dos")))

Note that the list can be created at runtime.

C++ STL

map<int, string> my_hash;
my_hash[1] = "uno";
my_hash[2] = "dos";

Perl

my %hash = (
  1 => 'uno',
  2 => 'dos'
);

Obtaining an element from a hash

Chicken

(hash-table-ref/default my_hash 2 #f)

This will return #f (false) if the element is not defined. If you prefer to have an error thrown, you should use:

(hash-table-ref my_hash 2)

C++ STL

my_hash[2]

Python

my_hash[2]

Perl

$my_hash{2}

Printing the keys in the hash

Chicken

If you can afford to have a list with the keys created, you should do this:

(format #t "~{~A~%~}~%" (hash-table-keys my_hash))

Otherwise, do this:

(hash-table-walk
  my-hash
  (lambda (key value)
    (format #f "~A~%" key)))

C++ STL

for(map<int, string>::iterator iter = my_hash.begin();
    it != my_hash.end(); ++it )
  cout << it->first << endl;

Python

print my_hash.keys()

Perl

say foreach keys %my_hash;

Getting the number of items in a hash

Chicken

(hash-table-size my-hash)

Python

len(my-hash)