Changeset 8709 in project


Ignore:
Timestamp:
02/23/08 20:02:54 (12 years ago)
Author:
sjamaan
Message:

Move story about set! to variables

File:
1 edited

Legend:

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

    r8708 r8709  
    203203with a backslash, if you need it to be part of a variable.
    204204
    205 === Procedures
    206 
    207 Of course using simple expressions like this is not enough.  You'll
    208 need procedures too.  In Ruby, named procedures are actually methods
    209 on objects, but we can forget about that little detail for now:
    210 
    211 Ruby:
    212 
    213 <enscript highlight=ruby>
    214   def pythagoras(a, b)
    215     Math.sqrt(a**2 + b**2)
    216   end
    217 </enscript>
    218 
    219 Chicken:
    220 
    221 <enscript highlight=scheme>
    222   (define pythagoras
    223     (lambda (a b)
    224       (sqrt (* a a) (* b b))))
    225 </enscript>
    226 
    227 Now that's interesting!  Procedures are just regular variables in
    228 Scheme (a bit like functions in Javascript).  We assign a lambda to it.
    229 We can do that in Ruby too, but it's not pretty:
    230 
    231 Ruby:
    232   some_class.send(:define_method, :pythagoras) {|a, b| Math.sqrt(a**2 + b**2) }
    233 
    234 Just like in Ruby the {{def foo}} is shorter than the above, in Scheme
    235 we have a convenient shorthand for defining procedures too:
    236 
    237 <enscript highlight=scheme>
    238   (define (pythagoras a b)
    239     (sqrt (* a a) (* b b)))
    240 </enscript>
    241 
    242205To assign to a pre-existing variable we can also use {{set!}}:
    243206
     
    253216artifact of an imperative programming style and in clean code you
    254217want to avoid using it.
     218
     219=== Procedures
     220
     221Of course using simple expressions like this is not enough.  You'll
     222need procedures too.  In Ruby, named procedures are actually methods
     223on objects, but we can forget about that little detail for now:
     224
     225Ruby:
     226
     227<enscript highlight=ruby>
     228  def pythagoras(a, b)
     229    Math.sqrt(a**2 + b**2)
     230  end
     231</enscript>
     232
     233Chicken:
     234
     235<enscript highlight=scheme>
     236  (define pythagoras
     237    (lambda (a b)
     238      (sqrt (* a a) (* b b))))
     239</enscript>
     240
     241Now that's interesting!  Procedures are just regular variables in
     242Scheme (a bit like functions in Javascript).  We assign a lambda to it.
     243We can do that in Ruby too, but it's not pretty:
     244
     245Ruby:
     246  some_class.send(:define_method, :pythagoras) {|a, b| Math.sqrt(a**2 + b**2) }
     247
     248Just like in Ruby the {{def foo}} is shorter than the above, in Scheme
     249we have a convenient shorthand for defining procedures too:
     250
     251<enscript highlight=scheme>
     252  (define (pythagoras a b)
     253    (sqrt (* a a) (* b b)))
     254</enscript>
     255
    255256
    256257== Data types
     
    492493  ("this" "is" "very" "cool")
    493494
    494 === Regexes
     495=== Regular expressions
    495496
    496497Just like in Ruby, there's a Regex data type, but in Chicken there is
     
    499500Ruby:
    500501
    501 
     502  //
    502503
    503504Chicken:
Note: See TracChangeset for help on using the changeset viewer.