#!/usr/bin/env ruby # ========================================================================= # NeverEnding.rb # J. Menu - http://cui.unige.ch/DI/cours/CompInterpretes # # $Id$ # ========================================================================= # The following functions are in fact methods in Ruby # A function that cannot be evaluated, whatever it's argument def NeverEnding (n) 1 + NeverEnding(n + 1) end # A function that illustrates the virtues of closures def funct (m, n) if m.call < 100 m.call + 12 else n.call end end puts "\n==> An interesting programming example...\n\n" # Call by name will compute "funct" fine! puts "--> Calling 'funct' with closures, thus mimicking call by name" printf( "%d\n\n", funct( lambda { || 3 + 5 }, lambda { || NeverEnding(7) } ) ) # Call by value will just defeat it! puts "--> Calling 'funct' without closures, i.e. with mere call by value" printf("%d\n", funct(3 + 5, NeverEnding(7)))