Changeset 8706 in project


Ignore:
Timestamp:
02/23/08 19:04:58 (12 years ago)
Author:
sjamaan
Message:

Give some examples with SRFI-1
Add car/cdr to cons examples

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/chicken-for-ruby-programmers

    r8701 r8706  
    279279  (define y (sprintf "x contains ~A" x))
    280280  (define z (conc "x contains " x))
    281   ; Or:
     281  ; Conc automatically converts its arguments to strings. We also could do:
    282282  (define z (string-append "x contains " (->string x)))
    283283
     
    340340used under.
    341341
     342==== List procedures
     343
     344Lists are, as mentioned before, linked lists.  This means they always
     345consist of two parts: a head and a tail.  We've seen the {{list}}
     346procedure which creates lists, but this works on lower primitives:
     347
     348  #;1> (list 1)
     349  (1)
     350  #;2> (cons 1 '())
     351  (1)
     352
     353The {{()}} is the empty list.  It is itself a list, but it is also a
     354single symbol.  It serves as the ''end of list marker''.  That's why
     355the list construction procedure, {{cons}}, can create longer lists too:
     356
     357  #;1> (list 1 2 3 4)
     358  (1 2 3 4)
     359  #;2> (cons 1 (cons 2 (cons 3 (cons 4 '()))))
     360  (1 2 3 4)
     361
     362To take the head/tail of these lists we have two procedures:
     363
     364  #;1> (car '(1 2 3 4))
     365  1
     366  #;2> (cdr '(1 2 3 4))
     367  (2 3 4)
     368  #;3> (cdr (cdr '(1 2 3 4)))
     369  (3 4)
     370  #;4> (car (cdr (cdr '(1 2 3 4))))
     371  3
     372  #;5> (caddr '(1 2 3 4)) ; combination of car cdr cdr
     373  3
     374  #;6> (car (car '(1 2 3 4)))
     375  Error: (car) bad argument type: 1
     376  #;7> (cdr (cdr '(1)))
     377  Error: (cdr) bad argument type: ()
     378
     379Actually, {{cons}} just sticks two things together, so we could also
     380stick together two numbers:
     381
     382  #;1> (cons 1 2)
     383  (1 . 2)
     384  #;2> (car (cons 1 2))
     385  1
     386  #;3> (cdr (cons 1 2))
     387  2
     388
     389Two things stuck together are called a ''pair''.  By sticking together
     390more things without an end of list marker, we can create an ''improper
     391list'':
     392
     393  #;1> (cons 1 (cons 2 (cons 3 4)))
     394  (1 2 3 . 4)
     395
     396You should not use lists like these unless you know what you're doing,
     397because ''all'' list library procedures expect ''proper lists'': lists
     398with end markers.  Chicken supports the full
     399[[http://srfi.schemers.org/srfi-1/srfi-1.html|SRFI-1]] out of the box.
     400Have a look at that document and compare it to the Ruby standard
     401Enumerator and Array methods.  Most of the procedures in srfi-1 will
     402look ''very'' familiar to you.  Here are some examples:
     403
     404  #;1> (use srfi-1)  ;; Not needed in Ruby
     405  ; loading library srfi-1 ...
     406  #;2> ;; [1, 2, 3] + [4, 5, 6] / [1, 2, 3].concat([4, 5, 6])
     407  (append '(1 2 3) '(4 5 6))
     408  (1 2 3 4 5 6)
     409  #;3> (map add1 '(1 2 3 4)) ;; [1, 2, 3, 4].map{|x| x + 1}
     410  (2 3 4 5)
     411  #;4> ;; No equivalent because map works on one object:
     412  (map + '(1 2 3 4) '(5 6 7 8))
     413  (6 8 10 12)
     414  #;5> ;; [1, 2, 3, 4].each{|x| puts x}
     415  (for-each (lambda (x) (printf "~A\n" x)) '(1 2 3 4))
     416  1
     417  2
     418  3
     419  4
     420  #;6> ;; [1, 2, 3, 4, 5, 6].select{|x| (x % 2).zero? }
     421  (filter even? '(1 2 3 4 5 6))
     422  (2 4 6)
     423  #;7> ;; [1, 2, 3, 4].inject(""){|str, x| str + x.to_s}
     424  (fold (lambda (x str) (conc str x)) "" '(1 2 3 4))
     425  "1234"
    342426
    343427=== Symbols
     
    360444
    361445  :foo
     446  "blah".to_sym
     447  :blah.to_s
    362448
    363449Scheme:
    364450
    365451  'foo
     452  (string->symbol "foo")
     453  (symbol->string 'foo)
    366454
    367455As we can see, a symbol is only a quoted variable name!  This is the
Note: See TracChangeset for help on using the changeset viewer.