Changeset 8734 in project


Ignore:
Timestamp:
02/24/08 03:41:36 (12 years ago)
Author:
graham
Message:

suggested revision for tail-call discussion

Location:
wiki
Files:
2 edited

Legend:

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

    r8731 r8734  
    316316because when the {{(+ (add-up-to (sub1 x)) x)}} expression is
    317317evaluated, the recursive call to {{add-up-to}} creates a new stack
    318 frame so that when it returns the x can be added to the result.
     318frame so that when it returns the x can be added to the result.
     319
     320[Note that this code will 'break' in Chicken too, but only for much
     321larger numbers. Although Chicken doesn't have an arbitrary stack
     322depth, if you try (add-up-to) on a large enough number, you'll use up
     323all your system memory before getting an answer. Read on for a better
     324way to write it.]
     325
    319326All Schemes know that when there is nothing that needs to be done
    320327after a procedure returns, there is no point in returning to the
    321 procedure that called it at all, instead it can just return directly
    322 to the procedure that called the current procedure, so the call can
    323 be optimized to become a ''goto'', replacing the current stack frame:
     328procedure that called it at all: instead it can just return directly
     329to the procedure that called the current procedure. So the call can
     330be optimized to become a ''goto'', replacing the current stack frame.
    324331
    325332Ruby still can't handle it:
     
    354361As you'll notice, this version is a lot faster in Chicken too because
    355362it does not have to travel back through all those empty "stack
    356 frames".  You can also notice the difference when you remove the stop
    357 condition and you look at your OS's process list.  Then Chicken's
    358 memory usage just keeps going up until it breaks because it can't
    359 allocate any more.  In the second example it will stay constant and
    360 simply loop forever.
     363frames".  In the first example, Chicken's memory usage increases upon
     364every recursion: for large numbers, it will break because it can't
     365allocate any more. But in the second example, memory usage will stay
     366constant and simply loop forever.
    361367
    362368=== Blocks
  • wiki/orders

    r753 r8734  
    44
    55There's no documentation about it right now.  Sorry.  :-(
     6
     7== Dependencies
     8
     9{{srfi-13}}
     10
     11== Procedures
     12
     13[procedure] (cmp-key cmp key)
     14
     15Generates an ordering procedure that applies the 'key' proc to both
     16sort candidates, and compares the resulting keys using the 'cmp'
     17procedure.
     18
     19E.g. to sort a list of integers based on their value modulo-10:
     20(sort my-numbers (cmp < (cut modulo <> 10)))
     21
     22[procedure] (key-string< key)
     23
     24Generates an ordering procedure that sorts strings using string<
     25(defined in SRFI-13).
     26
Note: See TracChangeset for help on using the changeset viewer.