source: project/wiki/chicken-for-ruby-programmers @ 8801

Last change on this file since 8801 was 8801, checked in by sjamaan, 12 years ago

Normalize nomenclature: function -> procedure for Scheme examples, function -> method for Ruby examples

File size: 28.0 KB
Line 
1[[toc:]]
2
3== Introduction
4
5If you don't know much about chicken yet, please take a moment to read
6the introductory part of [[The User's Manual]].  You're back?  Good!
7
8=== Paradigm independence
9
10The most important design feature of Ruby is that it is purely
11object-oriented; everything is an object.  Scheme is ''not'' an
12object-oriented language.  In fact, it does not commit to any
13particular programming paradigm -- it offers ''complete and total
14freedom to the programmer''.  If you decide (a part of) a program is
15best implemented in an object-oriented fashion, you can choose to use
16one of the many object systems.  Have a quick glance at
17[[Eggs Unlimited 3#object-oriented-programming]] to get an impression of the
18diversity of styles of object oriented programming you can use with
19Chicken.  By the way, the list on that page shows all the available
20''eggs'' for Chicken.  We'll explain all about these [[#Eggs|later]].
21
22Besides object-oriented programming, you can also program in a
23procedural fashion (like you would in Pascal, for example) or in a
24functional style (a bit like Haskell or ML) and you can even
25experiment with message-passing like in Erlang, logic programming
26like you would with Prolog, or stack languages like Forth and Factor.
27
28=== Origins
29
30Ruby's origins are firmly rooted in Lisp.  It takes many things and
31ideas from Lisp (symbols, lambdas, eval, metaprogramming, DSLs etc).
32What it doesn't take from Lisp it takes from Smalltalk, which was
33itself inspired by Lisp's clean syntax.  All this means that once
34you're over the initial hump of grokking the syntax, you'll find
35yourself in pretty familiar territory.
36
37Originally, Ruby started out as an implementation-defined language.
38That is, there was only one implementation (Matz's) and whatever that
39implementation did '''was''' the language spec.  Nowadays, new
40implementations of Ruby are being developed, but as of this writing
41there is still no official specification on Ruby's syntax and
42semantics, by my knowledge.
43
44Scheme, on the other hand, is a specification-defined language.  There
45is one official language specification which says what Scheme is and
46how it works.  Chicken is simply an implementation of that
47specification.  There is one thing that is important to know right
48now: The Scheme specification is ''extremely'' minimal in design.  It
49tries to define as few language constructs as possible, but these few
50should be so powerful that you will not need any more to make
51programs.  This results in an extremely small spec and a very elegant
52and clean language with very few rules to remember, which makes it
53easy to learn.
54
55However, in the real world you will need much more than just
56programming constructs, you need things to interact with the operating
57system, networking libraries, etc.  That's where the difference
58between Scheme and other languages comes in: Every implementation of
59Scheme defines the things it thinks are necessary to build real
60programs with.  Unfortunately, this means most Scheme programs are
61inherently not portable from one implementation to another, but it also means that Scheme implementations
62are free to experiment how they want and explore new language
63territory.  This gives each Scheme implementation its uniqueness.
64
65Fortunately, most Scheme implementations including Chicken are fairly portable to modern hardware architectures and operating systems.  You or may not be able to port a Chicken program to another Scheme, but you can port it from Windows to Mac OS X or Linux, or from the 32-bit Intel 386 to the 64-bit SPARC architecture, because Chicken runs in all those environments.  Usually "porting" just means "running it on that system", because just like Ruby Chicken has procedures that are platform-independent, with multiple implementations where the platform itself differs.
66
67Chicken's power is in how it extends the Scheme standard.  It has a
68very comfortable interface to C that does not require you to touch a
69single line of C code in order to create bindings to existing C
70libraries, but it also gives you the freedom to embed Chicken in C
71code or C in Chicken code as you want.  It offers a TCP/IP networking
72layer, it has great POSIX interoperability so you can interact with
73the OS. And most importantly: It can compile Scheme code to very
74efficient C code which can itself be compiled to machine code, giving
75you the best of both worlds: a dynamic language which allows you to
76program in the flexible way you are used to with Ruby, but this can be
77compiled for maximum efficiency.
78
79== Syntax
80
81=== The basics
82
83The one thing that is most strikingly different between Ruby and
84Scheme is of course the syntax.  Ruby has an extremely baroque syntax
85that allows you many freedoms in how you would like to write down
86things. Scheme, on the other hand, has only one way in which to write
87a given expression.  Let's start by looking at an example.  First we
88start by firing up an {{irb}} session and typing a little program:
89
90  irb(main):001:0> # My first program
91  irb(main):002:0* [1, 2, 3, 4].map{|x| x + 1}
92  => [2, 3, 4, 5]
93  irb(main):003:0>
94
95Now, we fire up a {{csi}} (chicken scheme interpreter) session:
96
97  #;1> ; My first program
98  (map add1 (list 1 2 3 4))
99  (2 3 4 5)
100  #;2>
101 
102In Scheme, lists are delimited with parentheses with its elements
103separated by spaces. As we can see, everything in Scheme is a list,
104even the expressions that you use to tell it what to do!  An
105expression in Scheme is always a list with the operator on its first
106position and the operands following it.  Procedures that accept no
107arguments are simply a list with only the procedure in it, for example
108{{(newline)}}, which simply displays a newline character.
109
110This simple rule also means that ''every parenthesis has a meaning''.
111You can't add more parentheses or leave off parentheses like you can
112in most Ruby expressions.  Adding extra parentheses simply applies
113the resulting expression as if it were a procedure:
114
115  #;2> ((newline))
116
117  Error: call of non-procedure: #<unspecified>
118
119          Call history:
120
121          <syntax>                ((newline))
122          <syntax>                (newline)
123          <eval>          ((newline))
124          <eval>          (newline)       <--
125
126If {{(newline)}} returned a procedure, it would be called.  But as it
127happens, {{newline}} simply returns an unspecified value which is not
128a procedure and thus can't be applied.  We can also use the result of
129calling a procedure in the call to another procedure:
130
131  #;3> (add1 2)
132  3
133  #;4> (- 10 (add1 2) 1)
134  6
135
136We see that arithmetic is a little different from Ruby, as a result of
137the simple rules Scheme has for its syntax.  This may be a little
138awkward, especially with complex calculations:
139
140  #;5> (* (+ 1 (- 6 2) 2) (- 10 5))
141  35
142
143In Ruby (and other languages with algebraic syntax) this would have
144been
145
146  irb(main):002:0> (1 + (6 - 2) + 2) * (10 - 5)
147  => 35
148  irb(main):003:0> # Alternatively:
149  irb(main):004:0* (1 + 6 - 2 + 2) * (10 - 5)
150  irb(main):005:0> # or even:
151  irb(main):006:0* (((1) + ((((6 - 2)))) + 2) * (((10) - ((5)))))
152  => 35
153
154Both types of syntax have their advantages and disadvantages: The
155Ruby-like syntax is more natural, but you have to think about operator
156precedence rules.  Chicken does not need operator precedence rules
157because the precedence can be determined from the way it's nested, but
158it's less natural for most people (though you get used to it very
159quickly).
160
161Actually, right now you know almost all there is to know
162about Scheme's syntax!  Chicken has a couple of extensions to the
163basic Scheme syntax, but we won't go into detail here.  Later you'll
164see a couple of handy shorcuts, but this is basically it.
165
166=== Variables
167
168Variables are names for things.  Chicken has vary lax rules for
169naming variables.  Actually, ''any string'' is a valid identifier
170as long as you quote it correctly.
171
172Ruby:
173
174  #;1> x = 10
175  => 10
176  #;2> x
177  => 10
178  #;3> x-y-z = 10
179  NameError: undefined local variable or method `x' for main:Object
180        from (irb):2
181 
182Scheme:
183
184  #;1> (define x 10)
185  #;2> x
186  10
187  #;3> (define x-y-z 10)
188  #;4> x-y-z
189  10
190  #;5> (define %x% 1)
191  #;6> %x%
192  1
193  #;7> (define |a b c| 5)
194  #;8> |a b c|
195  5
196
197As you can see, because of Scheme's operator rules, symbols that would
198normally be separate tokens designating operators have no special
199meaning so we can use it in the middle of a name.  The convention in
200Scheme is to use the minus sign as word separator (in Ruby, you would
201use an underscore for separating words).  The final example shows how
202any string is allowed as a variable name: if the string contains
203syntax that would mean something else to Scheme you can enclose the
204variable name in pipe symbols.  The pipe symbol itself can be escaped
205with a backslash, if you need it to be part of a variable.
206
207To assign to a pre-existing variable we can also use {{set!}}:
208
209  #;1> (define x 10)
210  #;2> x
211  10
212  #;3> (set! x 20)
213  #;4> x
214  20
215
216Top-level variables can also be overwritten by simply redefining them,
217but in some cases you need {{set!}}.  However, set! is a typical
218artifact of an imperative programming style and in clean code you
219want to avoid using it.
220
221Scheme also allows the ''binding'' of local variables. Bound variables
222behave like the defined variables above, however, they are only valid
223within a local scope. The top-level variables we've seen are the equivalent
224of a global in Ruby.
225
226The most common binding constructs are {{let}} and {{let*}}.
227{{let}} allows for any number of bindings, none of which are related.
228
229<enscript highlight=scheme>
230  (let ((a 5)
231        (b 10))
232    (+ a b))
233</enscript>
234
235{{let*}} is like let, except that the bindings are evaluated in order,
236so subsequent bindings can reference earlier bindings.
237
238<enscript highlight=scheme>
239  (let* ((a 5)
240         (b (* 2 a)))
241    (+ a b))
242</enscript>
243
244There are other binding forms, such as {{letrec}} and the so-called
245''named let''. More information about these forms can be found in
246the [[http://schemers.org/Documents/Standards/R5RS/|Scheme specification]].
247
248=== Procedures
249
250Of course using simple expressions like this is not enough.  You'll
251need procedures too.  In Ruby, named procedures are actually methods
252on objects, but we can forget about that little detail for now:
253
254Ruby:
255
256<enscript highlight=ruby>
257  def pythagoras(a, b)
258    Math.sqrt(a**2 + b**2)
259  end
260</enscript>
261
262Chicken:
263
264<enscript highlight=scheme>
265  (define pythagoras
266    (lambda (a b)
267      (sqrt (* a a) (* b b))))
268</enscript>
269
270Now that's interesting!  Procedures are just regular variables in
271Scheme (a bit like functions in Javascript).  We assign a lambda to it.
272We can do that in Ruby too, but it's not pretty:
273
274Ruby:
275  some_class.send(:define_method, :pythagoras) {|a, b| Math.sqrt(a**2 + b**2) }
276
277Just like in Ruby the {{def foo}} is shorter than the above, in Scheme
278we have a convenient shorthand for defining procedures too:
279
280<enscript highlight=scheme>
281  (define (pythagoras a b)
282    (sqrt (* a a) (* b b)))
283</enscript>
284
285In Scheme, recursion is a very important tool.  In fact, it is so
286important that the Scheme standard ''demands'' tail call optimization
287(TCO), which basically means that you can have infinite recursion as
288long as the recursive procedure does not need to do anything after it
289returns.  That sounds a bit strange, so here's an example:
290
291Ruby:
292
293  irb(main):010:0> def add_up_to(num)
294  irb(main):011:1>   if num.zero?
295  irb(main):012:2>     0
296  irb(main):013:2>   else
297  irb(main):014:2*     add_up_to(num - 1) + num
298  irb(main):015:2>   end
299  irb(main):016:1> end
300  => nil
301  irb(main):017:0> add_up_to(4)
302  => 10
303  irb(main):018:0> add_up_to(9999)
304  SystemStackError: stack level too deep
305
306Chicken:
307
308  #;2> (define (add-up-to x)
309         (if (zero? x)
310             0
311             (+ (add-up-to (sub1 x)) x)))
312  #;3> (add-up-to 4)
313  10
314  #;4> (add-up-to 9999)
315  49995000
316
317In most other Schemes, however, this will break just like in Ruby,
318because when the {{(+ (add-up-to (sub1 x)) x)}} expression is
319evaluated, the recursive call to {{add-up-to}} creates a new stack
320frame so that when it returns the x can be added to the result.
321
322[Note that this code will 'break' in Chicken too, but only for much
323larger numbers. Although Chicken doesn't have an arbitrary stack
324depth, if you try (add-up-to) on a large enough number, you'll use up
325all your system memory before getting an answer. Read on for a better
326way to write it.]
327
328All Schemes know that when there is nothing that needs to be done
329after a procedure returns, there is no point in returning to the
330procedure that called it at all: instead it can just return directly
331to the procedure that called the current procedure. So the call can
332be optimized to become a ''goto'', replacing the current stack frame.
333
334Ruby still can't handle it:
335
336  irb(main):027:0> def add_up_to(x)
337  irb(main):028:1>   def inner(x, y)
338  irb(main):029:2>     if x.zero?
339  irb(main):030:3>       y
340  irb(main):031:3>     else
341  irb(main):032:3*       inner(x, x+y)
342  irb(main):033:3>     end
343  irb(main):034:2>   end
344  irb(main):035:1>   inner(x, 0)
345  irb(main):036:1> end
346  => nil
347  irb(main):037:0> add_up_to(9999)
348  SystemStackError: stack level too deep
349
350Scheme (this works in all Schemes):
351
352  #;2> (define (add-up-to x)
353         (define (inner x y)
354           (if (zero? x)
355               y
356               (inner (sub1 x) (+ y x))))
357         (inner x 0))
358  #;3> (add-up-to 4)
359  10
360  #;4> (add-up-to 9999)
361  49995000
362
363As you'll notice, this version is a lot faster in Chicken too because
364it does not have to travel back through all those empty "stack
365frames".  In the first example, Chicken's memory usage increases upon
366every recursion: for large numbers, it will break because it can't
367allocate any more. But in the second example, memory usage will stay
368constant and simply loop forever.
369
370=== Blocks
371
372Ruby programmers will be familiar with ''blocks.'' Classic example in
373Ruby is the {{map}} method used to iterate over a collection, executing
374a ''block'' of code for each item in the collection.
375
376Ruby:
377  >> [1, 2, 3, 4, 5].map { |x| x * x }
378  => [1, 4, 9, 16, 25]
379
380Scheme also contains blocks, though we call them anonymous procedures
381usually. Procedures are created using the {{(lambda args body...)}} body
382form. This syntax is a little more verbose than Ruby's, but the trade off
383is that more than one procedure can be passed as an argument, whereas Ruby
384generally only allows one.
385
386Scheme:
387  #;1> (map (lambda (x) (* x x)) '(1 2 3 4 5))
388  (1 4 9 16 25)
389
390A more complicated example involves opening and closing files. Say we
391wanted to create a utility like {{wc -l}} that counts the number of
392lines in a file. In Ruby, it might look something like:
393
394<enscript highlight=ruby>
395  count = 0
396  File.open("myfile", 'r') { |fh|
397      count += 1 while fh.gets
398  }
399  puts count
400</enscript>
401
402Similarly, Scheme uses anonymous procedures to create the same behavior:
403
404<enscript highlight=scheme>
405  (with-input-from-file "filename"
406    (lambda () (port-fold (lambda (line lines-so-far) (+ 1 lines-so-farc)) 0 read-line)))
407</enscript>
408
409This Scheme code also showcases some typical functional style, using
410a ''fold'' operation instead of incrementing the value of a variable.
411
412
413== Data types
414
415Now we have a basic grasp of Scheme's syntax, we can have a look at the
416different data types Chicken has to offer.  We will do this from a Ruby
417perspective.
418
419=== Arrays
420
421In Ruby we use arrays for storing lists of things.  The obvious Scheme
422equivalent type is the list, you'd think.  This is sort of true:
423
424Ruby:
425
426  x = [1, 2, 3, 4]
427  x.map{|y| x + 10 }
428  x.each{|y| puts y }
429
430Scheme:
431
432  (define x '(1 2 3))
433  (map (lambda (x) (+ x 10)) x)
434  (for-each print x)
435
436Note that Scheme does not have the block scoping bug.  Another thing
437that we should note is the first line.  We create a list by
438''quoting'' it.  This allows us to enter the list in such a way that
439Chicken knows the list is just that; a list, and not a procedure
440application of the procedure called {{1}} on the arguments {{2}} and
441{{3}}.  The apostrophe takes care of that.
442
443However, we must always remember that the Scheme list is more like a
444linked list.  This means that it is very flexible in how we can add
445things to it and alter it, but it also means that traversing it takes
446more time as more items are added to it.  Accessing an element is an
447O(n) operation, where n is the position of the element.
448
449If we want O(1) operations on our lists, we can use a ''vector'':
450
451  #;1> (define x (vector 1 2 3 4))
452  #;2> (vector-ref x 2)
453  3
454  #;3> (define y (list 1 2 3 4))
455  #;4> (list-ref y 2)
456  3
457
458Adding new elements to a vector requires resizing or even copying the
459vector, just like it would in Ruby.  So whenever you're contemplating
460using a list type, think about the properties you want the list type
461to have.  This may sound odd, but in fact this gives you much more
462flexibility than Ruby, where you have the choice of using an Array,
463or... using an Array.  However, as Knuth famously said: "Premature
464optimization is the root of all evil", and you should probably take
465the list solution until it's proven that you ''need'' vectors.  Also,
466because Lisp was built on lists, it is very good at manipulating them,
467so they're most likely the most convenient datatype.
468
469Chicken also offers you several other types of array-like types, each
470with their own unique time and space properties.  Which you'll use
471depends on the task at hand and the situations your system will be
472used under.
473
474==== List procedures
475
476Lists are, as mentioned before, linked lists.  This means they always
477consist of two parts: a head and a tail.  We've seen the {{list}}
478procedure which creates lists, but this works on lower primitives:
479
480  #;1> (list 1)
481  (1)
482  #;2> (cons 1 '())
483  (1)
484
485The {{()}} is the empty list.  It is itself a list, but it is also a
486single symbol.  It serves as the ''end of list marker''.  That's why
487the list construction procedure, {{cons}}, can create longer lists too:
488
489  #;1> (list 1 2 3 4)
490  (1 2 3 4)
491  #;2> (cons 1 (cons 2 (cons 3 (cons 4 '()))))
492  (1 2 3 4)
493
494To take the head/tail of these lists we have two procedures:
495
496  #;1> (car '(1 2 3 4))
497  1
498  #;2> (cdr '(1 2 3 4))
499  (2 3 4)
500  #;3> (cdr (cdr '(1 2 3 4)))
501  (3 4)
502  #;4> (car (cdr (cdr '(1 2 3 4))))
503  3
504  #;5> (caddr '(1 2 3 4)) ; combination of car cdr cdr
505  3
506  #;6> (car (car '(1 2 3 4)))
507  Error: (car) bad argument type: 1
508  #;7> (cdr (cdr '(1)))
509  Error: (cdr) bad argument type: ()
510
511Actually, {{cons}} just sticks two things together, so we could also
512stick together two numbers:
513
514  #;1> (cons 1 2)
515  (1 . 2)
516  #;2> (car (cons 1 2))
517  1
518  #;3> (cdr (cons 1 2))
519  2
520
521Two things stuck together are called a ''pair''.  By sticking together
522more things without an end of list marker, we can create an ''improper
523list'':
524
525  #;1> (cons 1 (cons 2 (cons 3 4)))
526  (1 2 3 . 4)
527
528You should not use lists like these unless you know what you're doing,
529because ''all'' list library procedures expect ''proper lists'': lists
530with end markers.  Chicken supports the full
531[[http://srfi.schemers.org/srfi-1/srfi-1.html|SRFI-1]] out of the box.
532Have a look at that document and compare it to the Ruby standard
533Enumerator and Array methods.  Most of the procedures in srfi-1 will
534look ''very'' familiar to you.  Here are some examples:
535
536  #;1> (use srfi-1)  ;; Not needed in Ruby
537  ; loading library srfi-1 ...
538  #;2> ;; [1, 2, 3] + [4, 5, 6] / [1, 2, 3].concat([4, 5, 6])
539  (append '(1 2 3) '(4 5 6))
540  (1 2 3 4 5 6)
541  #;3> (map add1 '(1 2 3 4)) ;; [1, 2, 3, 4].map{|x| x + 1}
542  (2 3 4 5)
543  #;4> ;; No equivalent because map works on one object:
544  (map + '(1 2 3 4) '(5 6 7 8))
545  (6 8 10 12)
546  #;5> ;; [1, 2, 3, 4].each{|x| puts x}
547  (for-each (lambda (x) (printf "~A\n" x)) '(1 2 3 4))
548  1
549  2
550  3
551  4
552  #;6> ;; [1, 2, 3, 4, 5, 6].select{|x| (x % 2).zero? }
553  (filter even? '(1 2 3 4 5 6))
554  (2 4 6)
555  #;7> ;; [1, 2, 3, 4].inject(""){|str, x| str + x.to_s}
556  (fold (lambda (x str) (conc str x)) "" '(1 2 3 4))
557  "1234"
558
559=== Symbols
560
561Luckily, you are a Ruby programmer, so we will not have to go through
562the whole "explaining what symbols exactly are" again :)
563Actually, Ruby borrowed symbols from Lisp.
564
565Ruby:
566
567  :foo
568  "blah".to_sym
569  :blah.to_s
570
571Scheme:
572
573  'foo
574  (string->symbol "foo")
575  (symbol->string 'foo)
576
577As we can see, a symbol is only a quoted variable name!  This is the
578origin of symbols and also the reason you can {{send}} symbols
579representing method names to objects in Ruby.  Symbols have all the
580same semantics as Ruby's symbols: they can be compared in constant
581time and they take up very little memory space.
582
583=== Strings
584
585Strings are simple.  Just like in Ruby, we have strings enclosed by
586double quotes: {{"foo"}} works the same in Ruby as it does in Chicken.
587Chicken's double quoted strings work more like Ruby's single-quoted
588strings, though.  There is no string interpolation and other things;
589a string is just a string.
590
591Ruby:
592
593  x = 10
594  y = "x contains #{x}"
595  z = "x contains " + x.to_s
596
597Scheme:
598
599  (define x 10)
600  (define y (sprintf "x contains ~A" x))
601  (define z (conc "x contains " x))
602  ; Conc automatically converts its arguments to strings. We also could do:
603  (define z (string-append "x contains " (->string x)))
604
605Note that {{->string}} is simply the name of a procedure, including
606the arrow.
607
608It may be important to know that Scheme also has a ''character'' data
609type, unlike Ruby:
610
611Ruby:
612
613  irb(main):001:0> "foo"[0]
614  => 102
615
616  #;1> (string-ref "foo" 0)
617  #\f
618  #;2> (char->integer #\f)
619  102
620
621You will probably not need this data type for your first few Scheme
622programs so we won't go into it deeper here.
623
624==== String procedures
625
626Chicken comes shipped with
627[[http://srfi.schemers.org/srfi-13/srfi-13.html|SRFI-13]], which is a
628library of string procedures which is intended to be a lot like
629SRFI-1, which we already looked at [[#List procedures|a few sections
630ago]]:
631
632  #;1> (use srfi-13) ;; Not needed in Ruby
633  ; loading library srfi-13 ...
634  #;2> ;; "abc" + "def"
635  (string-append "abc" "def")
636  "abcdef"
637  #;3> ;; "abcdef"[-3..-1]
638  (string-take-right "abcdef" 2)
639  "ef"
640  #;4> ;; "abcdef".rjust(10)
641  (string-pad "abcdef" 10)
642  "    abcdef"
643  #;5> ;; ["this", "is", "very", "cool"].join(" ")
644  (string-join '("this" "is" "very" "cool"))
645  "this is very cool"
646  #;6> ;; "this is very cool".split(" ")
647  ;; NOT from srfi-13 but chicken's extras unit:
648  (string-split "this is very cool" " ")
649  ("this" "is" "very" "cool")
650
651=== Regular expressions
652
653Just like in Ruby, there's a Regex data type, but in Chicken there is
654no special syntax for it:
655
656Ruby:
657
658  irb(main):001:0> /(.)(.)(\d+)(\d)/.match("THX1138.").to_a
659  => ["HX1138", "H", "X", "113", "8"]
660
661Chicken:
662
663  #;1> (use regex)
664  ; loading library regex ...
665  #;2> (string-search "(.)(.)(\\d+)(\\d)" "THX1138.")
666  ("HX1138" "H" "X" "113" "8")
667
668The {{string-search}} procedure automatically transforms
669the first string to a regexp.  You can also do that yourself:
670
671  #;3> (string-search (regexp "(.)(.)(\\d+)(\\d)") "THX1138.")
672  ("HX1138" "H" "X" "113" "8")
673
674The advantage of doing this is that when you need to match several
675strings you can use the same regexp so it doesn't have to precompile
676the regexp every time you call {{string-search}}.
677
678=== Hashes
679
680The final datatype we use a lot in Ruby is the Hash.  In Chicken there
681are two datatypes you could use instead of the Ruby Hash;
682''association lists'' (or ''alists'' for short) or ''hash tables''.
683
684==== Association Lists
685
686Association lists are the simpler Hash like structure in chicken.
687Effectively, alists are standard lists of ''pairs,'' where the
688first item in the pair is the key and the second item is the value.
689Consequently, alists have a nice literal form:
690
691  '((foo  1) (bar 42) (baz 101))
692
693To lookup a value in the alist, use {{assoc}}. For example to check
694if the pair {{(bar 42)}} is in the alist:
695
696  #;1> (assoc 'bar '((foo  1) (bar 42) (baz 101)))
697  (bar 42)
698
699If the pair is not in the list, you would get the boolean false ({{#f}}).
700If you need more stringent checks, you can also use {{assq}} or {{assv}},
701learning more about these procedures is an exercise for the reader.
702
703Alists may simplistic, and inquisitive readers may notice that lookup is
704{{O(n)}} time. However, they are convenient and adding new items is a constant
705time operation. You may find they work in may places that you might use a
706small Hash in Ruby.
707
708==== Hash tables
709
710For more complex hashing operations, Chicken supplies true hash tables.
711
712  #;11> (define h (make-hash-table))
713  #;12> h
714  #<hash-table>
715  #;13> (hash-table-set! h 'foo 12)
716  12
717  #;14> (hash-table-set! h 'bar 101)
718  101
719  #;15> (hash-table-ref h 'bar)
720  101
721  #;16> (hash-table-delete! h 'bar)
722  #t
723  #;17> (hash-table-ref h 'bar)
724  Error: (hash-table-ref) hash-table does not contain key
725  bar
726
727Hash tables are more powerful overall, but do not offer convenient literal
728notation. If you need to convert from a hash table an alist you can use
729{{hash-table->alist}}. The {{alist->hash-table}} procedure converts in the
730opposite direction.
731
732== Examples
733
734Now we have the tools to make programs, let's look at a few larger
735programs to better appreciate how one would program in Chicken.
736
737  TODO
738
739== Chicken and the Real World
740
741Programming is about more than having a pretty language, so let's look
742at what Chicken has to offer for real construction work.
743
744=== Eggs
745
746Eggs are to chicken what ''gems'' are to Ruby: installable extensions
747like libraries and programs.  The list of [[eggs]] is where you should
748look first when you are going to implement something big. You can
749install an egg almost like you install a gem, as follows:
750
751  $ chicken-setup runcmd
752
753This downloads and installs the egg with the name "runcmd".  This
754egg has no dependencies, but if it did it would have downloaded and
755installed them as well.
756
757
758== Meta programming
759
760A hot topic in the Ruby community is meta programming and DSLs
761(Domain specific languages).  These ideas originated from Lisp, which
762means you can just keep on trucking in Chicken!
763
764=== Data is code and code is data
765
766The most fundamental concept of Lisp is that code is data.  As we've
767seen, procedure calls look like lists.  We've also seen that we can
768quote lists to "block" Scheme from interpreting a list as a procedure
769call.  We can also turn this around on its head and force a list to
770be evaluated as code:
771
772  #;1> (define calculate '(+ 1 2 3 4))
773  #;2> calculate
774  (+ 1 2 3 4)
775  #;3> (eval calculate)
776  10
777  #;4>
778
779"Big deal", you might say, "Ruby also has eval".  But the difference
780is what matters: In Ruby you have to construct strings to be evaluated,
781which means you need to be able to parse strings if you want to change
782a Ruby-program-stored-as-string.  In Scheme we can simply hack the
783list.  ''The program is stored in a parsed state'', so to speak.
784
785If we would want to change the operator from + to *, we can simply
786do that:
787
788  #;1> (eval (cons '* (cdr calculate)))
789  24
790
791This is much more robust than any regexp hacking or ad-hoc parsing on
792strings you want to evaluate.
793
794=== Macros
795
796One of the coolest concepts, but also the one most easily abused is
797''macros''.  Because Scheme stores code as data, you can change the
798code on-the-fly as described above.  You can do that at run-time on
799random data through eval, but you can also do it at compile-time on
800your program, which is usually the best time to do it.
801
802Scheme macros ''rewrite'' your code during compile time. They can
803range from simple to complex, with some macros defining entire
804"sublanguages" embedded in Scheme.
805
806Some people call Rails' {{acts_as_foo}} methods "macros". This
807description is not wrong, as these methods do ''rewrite'' your
808classes in a similar way to Scheme macros, but they are not quite
809as powerful.
810
811Here is a simple example of a task that is easy in Scheme, but
812much, much harder using Ruby's eval. Say you were debugging a
813program and found yourself printing out variables at certain
814points in the execution, along with the name of the variable
815so you could tell what you were looking at.
816
817<enscript highlight=scheme>
818  (print "myvar: " myvar)
819</enscript>
820
821You decide that repeatedly typing the variable name twice (once to
822indicate which variable, once to get the value) is a waste of time.
823Using a macro, you can quickly and easily abstract away the common
824syntax into one place.
825
826<enscript highlight=scheme>
827  (define-macro (ez-debug var)
828    (let ((name (symbol->string var)))
829      `(print ,name ": " ,var)))
830   
831  (define myvar '(this is a list))
832
833  (ez-debug myvar)
834</enscript>
835
836This simply wouldn't be possible with a regular procedure. By the time
837a procedure is called, syntactic information like variables names has
838been optimized away.
839
840
Note: See TracBrowser for help on using the repository browser.