You are looking at historical revision 8589 of this page. It may differ significantly from its current revision.

Input & output

Chicken C Python
Formatting and printing to stdout (format #t "I want ~A ~A~%" count desc) printf("I want %d %s\n", count, desc); ...
Printing to stderr (format (current-error-port) "Ooops~%") fprintf(stderr, "Ooops\n"); ...
Reading a line from stdin (read-line) char buffer[1024]; fgets(buffer, sizeof(buffer)/sizeof(buffer[0], stdin); ...
Opening a file for input (open-input-file path) fopen(path, "r"); ...
Opening a file for output (open-output-file path) fopen(path, "w"); ...
Count lines in a file (with-input-from-file path (lambda () (let loop ((count 0)) (if (eof-object? (read-line)) count (loop (+ count 1))))))) 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)))))) 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; ...
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();



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



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.


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


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

Obtaining an element from a hash


(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)







Printing the keys in the hash


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:

  (lambda (key value)
    (format #f "~A~%" key)))


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



for my $key ( keys %my_hash ) {
  print "$key\n";