Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

letrec

Name: Anonymous 2011-12-23 12:06

Isn't
(letrec ([is-even? (lambda (n)
                    (or (zero? n)
                        (is-odd? (sub1 n))))]
         [is-odd? (lambda (n)
                   (and (not (zero? n))
                        (is-even? (sub1 n))))])
        (is-odd? 11))


equivalent to...

(let
    ([is-even? nil]
     [is-odd? nil])
    (set! is-even? (lambda (n)
                    (or (zero? n)
                        (is-odd? (sub1 n)))))
    (set! is-odd? (lambda (n)
                   (and (not (zero? n))
                        (is-even? (sub1 n)))))
    (is-odd? 11))

Name: Anonymous 2011-12-23 23:46

>>16

his for-each stream maker can be expressed pretty easily in lua using coroutines, and coroutines aren't that difficult to implement in scheme using continuations. Therefore, his thingy is poop.


#!/usr/bin/lua

-- ARGS:
-- for_each(operator_function) -> nil
--   operator_function will be called as operator_function(element)
--   for every element in some collection.
--
-- RETURNS:
-- a function of the form fn(), where successive calls to fn
-- will return the objects that for_each would normally call operator_function on.
function for_each_to_stream(for_each)
  return coroutine.wrap(function()
    for_each(coroutine.yield)
  end)
end

-- ARGS:
-- for_each(operator_function, collection)
--   operator_function will be called as operator_function(element),
--   for every element contained within collection.
--
-- RETURNS:
-- a function of the form:
-- fn(collection) -> f()
-- successive calls to f will return elements of collection.
function for_each_to_stream_maker(for_each)
  return function(collection)
    local for_each_function_for_this_collection = function(operator_function)
      return for_each(operator_function, collection)
    end
    return for_each_to_stream(for_each_function_for_this_collection)
  end
end


function for_each_in_range_1_10(operator_function)
  for i=1,10 do
    operator_function(i)
  end
end

stream_1_10 = for_each_to_stream(for_each_in_range_1_10)

for x in stream_1_10 do
  print(x)
end
-- output:
-- 1
-- 2
-- 3
-- 4
-- 5
-- 6
-- 7
-- 8
-- 9
-- 10

function for_each_divisor(operator_function, n)
  for i=1,n do
    if math.mod(n, i) == 0 then
      operator_function(i)
    end
  end
end

for_each_divisor(print, 10)
-- output:
-- 1
-- 2
-- 5
-- 10

-- make_divisor_stream(n) -> iterator for divisors of n
make_divisor_stream = for_each_to_stream_maker(for_each_divisor)

for i in make_divisor_stream(10) do
  print(i)
end
-- output:
-- 1
-- 2
-- 5
-- 10

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List