Higher-Order Procedures Tutorial (long)

Higher-Order Procedures Tutorial (long)

Post by Nate Murra » Fri, 29 Dec 2006 10:35:48


ey Guys,
I've been going through the video lectures "Structure and
Interpretation of Computer Programs by Hal Abelson and Gerald Jay
Sussman. " (
http://swiss.csail.mit.edu/classes/6.001/abelson-sussman-lectures/ ).
The section on Higher-Order Procedures was a huge eye-opener for me and
I wanted to condense down what I learned in Lisp to guys who program in
Ruby. Now I know that for most of the experienced on this list this
will be old-news, but I hope to provide a valuable tutorial of Abelson
and Sussman's material to some guys who are just learning about this
stuff in Ruby.

Posted below is the straight text and code examples of what I have so
far. ( I've also posted the pdf of slides here:
http://tech.natemurray.com/2006/12/higher-order-procedures-in-ruby.html
if you're interested. ) Now, I am not just trying to drive up traffic
to my site. My purpose in posting this here is two-fold:

1) To submit it for peer review. I'd like to know if you guys have any
suggestions or improvements on the code examples and/or copy. For
example a part that seems particularly ugly to me is the

cube = self.method(:cube).to_proc
cube.call(3)

part. Any suggestions on how to make this a little more transparent or
simplified?

2) To provide a valuable introductory tutorial to the power of
higher-order procedures and how to implement them in Ruby.

The text below can be copied and pasted into a file. It should run with
no problems.

DISCLAIMER: As mentioned above and below the copy is taken mainly from
"Structure and Interpretation of Computer Programs by Hal Abelson and
Gerald Jay Sussman. " (
http://swiss.csail.mit.edu/classes/6.001/abelson-sussman-lectures/ ). A
few paraphrases and examples were added and the code was converted to
Ruby.

enjoy,
Nate Murray
http://www.natemurray.com

----------------------------
#!/usr/bin/ruby
# == Higher-Order Procedures (in Ruby)
# based on Structure and Interpretation of Computer Programs (1985 MIT
Press)
# by Hal Abelson and Gerald Jay Sussman.
# * http://swiss.csail.mit.edu/classes/6.001/abelson-sussman-lectures/
# Nathan Murray < XXXX@XXXXX.COM > v1.0 12/13/06
# http://www.natemurray.com
#
# == Legal
# The copy in this presentation is taken directly from Structure and
# Interpretation of Computer Programs by Hal Abelson and Gerald Jay
Sussman
# (MIT Press, 1984; ISBN 0-262-01077-1). Specifically section 1.3
Formulating
# Abstractions with Higher-Order Procedures. There are a few
paraphrases and
# additional examples added.
#
# The main difference is that the code has been converted from Lisp to
Ruby.
#
# The full text of this book and accompanying video lectures can be
found at:
# http://swiss.csail.mit.edu/classes/6.001/abelson-sussman-lectures/
# http://mitpress.mit.edu/sicp/
#
# The video lectures are copyright by Hal Abelson and Gerald Jay
Sussman. The
# video lectures, and in turn this document, are licensed under a
Creative
# Commons License.
# http://creativecommons.org/licenses/by-sa/2.0/

# == Slide
# Mathematical procedures are, in effect, abstractions that describe
compound operations on
# numbers independent of the particular numbers. For example, when we

def cube(a)
a * a * a
end

# define cube we are not talking about the cube of a particular number,
but rather about a
# method for obtaining the cube of any number.

# == Slide
# Of course we could get along
# without ever defining this proced
 
 
 

Higher-Order Procedures Tutorial (long)

Post by William Ja » Fri, 29 Dec 2006 11:39:47


def cube n
n * n * n
end

def inc(n)
n + 1
end

def sum(term, a, the_next, b)
return 0 if a > b
send(term,a) + sum(term, send(the_next,a), the_next, b)
end

p sum( :cube, 1, :inc, 3 )

 
 
 

Higher-Order Procedures Tutorial (long)

Post by ara.t.howa » Fri, 29 Dec 2006 11:50:45


method('cube')[ 3 ]


btw.

harp:~ > cat a.rb
def sum_integers(a, b)
return 0 if a > b
a + sum_integers((a + 1), b)
end

p sum_integers(10, 10)

harp:~ > ruby a.rb
10

and similar errors.

you could post this on http://www.yqcomputer.com/

regards.

-a
--
if you find yourself slandering anybody, first imagine that your mouth is
filled with excrement. it will break you of the habit quickly enough. - the
dalai lama
 
 
 

Higher-Order Procedures Tutorial (long)

Post by ara.t.howa » Fri, 29 Dec 2006 11:52:07


def cube(n) n ** 3 end

-a
--
if you find yourself slandering anybody, first imagine that your mouth is
filled with excrement. it will break you of the habit quickly enough. - the
dalai lama
 
 
 

Higher-Order Procedures Tutorial (long)

Post by Giles Bowk » Fri, 29 Dec 2006 14:11:15

Hey, I don't have time to look at this right away, BUT, you should
check out James Grey's blog, Google "Higher-Order Ruby." He did a
series on using higher-order programming in Ruby, based on
translations from Mark-Jason Dominus' "Higher-Order Perl," and it's
very good.





--
Giles Bowkett
http://www.yqcomputer.com/
http://www.yqcomputer.com/
http://www.yqcomputer.com/
 
 
 

Higher-Order Procedures Tutorial (long)

Post by James Edwa » Sat, 30 Dec 2006 00:37:07


http://www.yqcomputer.com/

I really am working on the last two as well and will get them out
eventually...

James Edward Gray II
 
 
 

Higher-Order Procedures Tutorial (long)

Post by pat eyle » Sat, 30 Dec 2006 00:40:57


I hope so, I'm anxious to read them. ;^)



--
thanks,
-pate
-------------------------
http://www.yqcomputer.com/
 
 
 

Higher-Order Procedures Tutorial (long)

Post by Sam Smoo » Sat, 30 Dec 2006 07:15:56

The filter_evens() example is not very ruby-ish. (As I interpret it
anyways.) I might write it like this:

class Fixnum
def even?
self % 2 == 0
end
def odd?
not self.even?
end
end

Then instead of a filter_evens() method, I would just:

list.select { |n| n.even? }

Or even shorter:

list.select &:even?

...if you have 'facets/symbol/to_proc' or 'active_support' loaded.
 
 
 

Higher-Order Procedures Tutorial (long)

Post by Giles Bowk » Mon, 01 Jan 2007 12:04:20


..


You also did (a * a * a) for a to the 3rd power, when you could have
just done (a ** 3).

However, I took a look at the PDF, and I have to say, there are some
very good bits in this. It looks like a lot of your examples come
directly from the Ableson-Sussman videos? I really liked the
#filter_ths() example. There are probably more Rubyish ways to do
these things, but it's a very clear set of examples. I liked it a lot.

--
Giles Bowkett
http://www.yqcomputer.com/
http://www.yqcomputer.com/
http://www.yqcomputer.com/