Changeset 8708 in project


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

Move strings section around a bit, remove duplicate stuff about symbols

File:
1 edited

Legend:

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

    r8706 r8708  
    259259different data types Chicken has to offer.  We will do this from a Ruby
    260260perspective.
    261 
    262 === Strings
    263 
    264 Strings are simple.  Just like in Ruby, we have strings enclosed by
    265 double quotes: {{"foo"}} works the same in Ruby as it does in Chicken.
    266 Chicken's double quoted strings work more like Ruby's single-quoted
    267 strings, though.  There is no string interpolation and other things;
    268 a string is just a string.
    269 
    270 Ruby:
    271 
    272   x = 10
    273   y = "x contains #{x}"
    274   z = "x contains " + x.to_s
    275 
    276 Scheme:
    277 
    278   (define x 10)
    279   (define y (sprintf "x contains ~A" x))
    280   (define z (conc "x contains " x))
    281   ; Conc automatically converts its arguments to strings. We also could do:
    282   (define z (string-append "x contains " (->string x)))
    283 
    284 Note that {{->string}} is simply the name of a procedure, including
    285 the arrow.
    286261
    287262=== Arrays
     
    433408Ruby:
    434409
    435   "foo".to_sym
    436 
    437 Scheme:
    438 
    439   (string->symbol "foo")
    440 
    441 To enter them literally, we can use this syntax:
    442 
    443 Ruby:
    444 
    445410  :foo
    446411  "blah".to_sym
     
    459424time and they take up very little memory space.
    460425
     426=== Strings
     427
     428Strings are simple.  Just like in Ruby, we have strings enclosed by
     429double quotes: {{"foo"}} works the same in Ruby as it does in Chicken.
     430Chicken's double quoted strings work more like Ruby's single-quoted
     431strings, though.  There is no string interpolation and other things;
     432a string is just a string.
     433
     434Ruby:
     435
     436  x = 10
     437  y = "x contains #{x}"
     438  z = "x contains " + x.to_s
     439
     440Scheme:
     441
     442  (define x 10)
     443  (define y (sprintf "x contains ~A" x))
     444  (define z (conc "x contains " x))
     445  ; Conc automatically converts its arguments to strings. We also could do:
     446  (define z (string-append "x contains " (->string x)))
     447
     448Note that {{->string}} is simply the name of a procedure, including
     449the arrow.
     450
     451It may be important to know that Scheme also has a ''character'' data
     452type, unlike Ruby:
     453
     454Ruby:
     455
     456  irb(main):001:0> "foo"[0]
     457  => 102
     458
     459  #;1> (string-ref "foo" 0)
     460  #\f
     461  #;2> (char->integer #\f)
     462  102
     463
     464You will probably not need this data type for your first few Scheme
     465programs so we won't go into it deeper here.
     466
     467==== String procedures
     468
     469Chicken comes shipped with
     470[[http://srfi.schemers.org/srfi-13/srfi-13.html|SRFI-13]], which is a
     471library of string procedures which is intended to be a lot like
     472SRFI-1, which we already looked at [[#List procedures|a few sections
     473ago]]:
     474
     475  #;1> (use srfi-13) ;; Not needed in Ruby
     476  ; loading library srfi-13 ...
     477  #;2> ;; "abc" + "def"
     478  (string-append "abc" "def")
     479  "abcdef"
     480  #;3> ;; "abcdef"[-3..-1]
     481  (string-take-right "abcdef" 2)
     482  "ef"
     483  #;4> ;; "abcdef".rjust(10)
     484  (string-pad "abcdef" 10)
     485  "    abcdef"
     486  #;5> ;; ["this", "is", "very", "cool"].join(" ")
     487  (string-join '("this" "is" "very" "cool"))
     488  "this is very cool"
     489  #;6> ;; "this is very cool".split(" ")
     490  ;; NOT from srfi-13 but chicken's extras unit:
     491  (string-split "this is very cool" " ")
     492  ("this" "is" "very" "cool")
     493
     494=== Regexes
     495
     496Just like in Ruby, there's a Regex data type, but in Chicken there is
     497no special syntax for it:
     498
     499Ruby:
     500
     501
     502
     503Chicken:
     504
     505=== Hashes
     506
     507The final datatype we use a lot in Ruby is the Hash.  In Chicken there
     508are two datatypes you could use instead of the Ruby Hash;
     509''association lists'' (or ''alists'' for short) or ''hash tables''.
     510
     511
     512
     513
    461514== Examples
    462515
    463 Let's look at a few larger programs to better appreciate how one would
    464 program in Chicken.
     516Now we have the tools to make programs, let's look at a few larger
     517programs to better appreciate how one would program in Chicken.
    465518
    466519  TODO
Note: See TracChangeset for help on using the changeset viewer.