Changeset 8803 in project


Ignore:
Timestamp:
02/24/08 17:23:36 (12 years ago)
Author:
sjamaan
Message:

Explain about void and truthiness

File:
1 edited

Legend:

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

    r8802 r8803  
    730730opposite direction.
    731731
     732
     733=== Booleans
     734
     735Scheme has a boolean type where {{#f}} is false and {{#t}} is true.
     736Its handling of truthiness is a lot like Ruby's; anything that is not
     737{{#f}} is treated as being ''true'':
     738
     739  #;1> (if #f
     740           (print "WTF, it's true")
     741           (print "It's not true"))
     742  It's not true
     743  #;2> (if #f
     744           (print "Yes, it's really true")
     745           (print "No, it's not true"))
     746  Yes, it's really true
     747  #;3> (if "Some random other value than #f"
     748           (print "Yes, this is also true")
     749           (print "No, it's not true"))
     750  Yes, this is also true
     751
     752Ruby's {{nil}} does not have a direct equivalent in Scheme.  In the
     753situations where a ''not present'' value is supposed to be returned,
     754usually {{#f}} is used:
     755
     756  #;1> (use srfi-1)
     757  ; loading library srfi-1 ...
     758  #;2> (find even? '(3 1 4 1 5 9))
     759  4
     760  #;3> (find even? '(1 3 7 9))
     761  #f
     762
     763In cases where a procedure really has no sensible thing to return, we
     764use the special ''void'' value, returned by the {{void}} procedure:
     765
     766  #;1> (define (say-hello)
     767         (print "Hello")
     768         (void))
     769  #;2> (say-hello)
     770  Hello
     771  #;3>
     772
     773As we see, the interpreter understands that there is no proper value
     774to return so it displays the prompt immediately without showing the
     775result value.  The example is a little contrived, because in real code
     776we wouldn't explicitly call {{(void)}} because {{print}} already
     777returns the {{void}} value.
     778
    732779== Examples
    733780
Note: See TracChangeset for help on using the changeset viewer.