Functional programming "style"

i discovered ruby some weeks ago and i have to say now that i’m through with
the book of the pragmatic programmers i quite like the language.

so far i’ve been coding in c/c++, java and COBOL (that was really painful
:slight_smile: )… i guess that’s why my code keeps on looking very procedural although
i’m writing ruby.

i’m very fascinated by the few examples of functional programming-style i’ve
seen so far but i don’t quite get the picture on how to “think this way”…
i mean i DO understand what the examples do and mean but i wouldn’t be able
to write them myself…

i know there are some other (more) functional languages than ruby but i
don’t have the energy nor the time to learn them. and i just want to get a
good coding style in ruby…

the book of the pragmatic programmers is a very good reference but i lack
some sort of
think-styleguide-on-functional-programming-for-procedural-programmers or
something likewise… or is there just some kind of “magic sentence” - a
"spell" ;-)) that can teach me how to…

anyway, i’d love to get some tips or some recommendations where to find such
a guide (hopefully there’s a free one, i don’t like buying books for i don’t
really have the money - but don’t hesitate to recommend a book - better than
nothing)…

thank you in advance,
patrick

I’d recommend checking out ‘The Little Schemer’ or ‘The Little Lisper’,
while they are Scheme/Lisp centric, you should be able to translate the
syntax into Ruby for the most part, or grab a free Scheme/Lisp
interpreter and go from there.

···

zesar (i_wont@tell.you) wrote:

i discovered ruby some weeks ago and i have to say now that i’m through with
the book of the pragmatic programmers i quite like the language.

so far i’ve been coding in c/c++, java and COBOL (that was really painful
:slight_smile: )… i guess that’s why my code keeps on looking very procedural although
i’m writing ruby.

i’m very fascinated by the few examples of functional programming-style i’ve
seen so far but i don’t quite get the picture on how to “think this way”…
i mean i DO understand what the examples do and mean but i wouldn’t be able
to write them myself…

i know there are some other (more) functional languages than ruby but i
don’t have the energy nor the time to learn them. and i just want to get a
good coding style in ruby…

the book of the pragmatic programmers is a very good reference but i lack
some sort of
think-styleguide-on-functional-programming-for-procedural-programmers or
something likewise… or is there just some kind of “magic sentence” - a
“spell” ;-)) that can teach me how to…

anyway, i’d love to get some tips or some recommendations where to find such
a guide (hopefully there’s a free one, i don’t like buying books for i don’t
really have the money - but don’t hesitate to recommend a book - better than
nothing)…


Eric Hodel - drbrain@segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

“zesar” i_wont@tell.you wrote in message
news:3df78342$0$38812$91cee783@newsreader01.highway.telekom.at…

i discovered ruby some weeks ago and i have to say now that i’m through
with
the book of the pragmatic programmers i quite like the language.

so far i’ve been coding in c/c++, java and COBOL (that was really painful
:slight_smile: )… i guess that’s why my code keeps on looking very procedural
although
i’m writing ruby.

i’m very fascinated by the few examples of functional programming-style
i’ve
seen so far but i don’t quite get the picture on how to “think this
way”…
i mean i DO understand what the examples do and mean but i wouldn’t be
able
to write them myself…

I don’t think you’ll find a Ruby reference on functional programming, but if
you can accept other languages I’ve got two online books - one for OCaml and
one for Scheme. Most principles should work well with Ruby, except for
pattern matching as discussed in the newsgroup recently.

The following book implements a range of minor applications using various
programming styles - object oriented, functional and procedural. It’s
available online - actually only online unless you read french. I’ve linked
to a chapter comparing functional and iterative style without preferring one
over the other. The book is generally interesting in its approach towards
developing applications (in harmony with its title), rather than trying
inventing problems to teach a specific style.

http://caml.inria.fr/oreilly-book/html/book-ora037.html#toc45

There is also the functional Bible, the Wizard book - which I have barely
skimmed - it supposedly very good and very hairy.

http://mitpress.mit.edu/sicp/sicp.html

It’s available online:

http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-4.html

Most other material I have seen have a combination of trivial useless
examples to give the basics, and then advances towards 8-Queen problems to
really illustrate the benefits of functional style - 8-Queen happens to be a
nice pattern to sample pixels in, but otherwise it is hardly a very
interesting example for real life applications.

Mikkel

“zesar” i_wont@tell.you wrote in message news:3df78342$0$38812$91cee783@newsreader01.highway.telekom.at

i discovered ruby some weeks ago and i have to say now that i’m through with
the book of the pragmatic programmers i quite like the language.

so far i’ve been coding in c/c++, java and COBOL (that was really painful
:slight_smile: )… i guess that’s why my code keeps on looking very procedural although
i’m writing ruby.

i’m very fascinated by the few examples of functional programming-style i’ve
seen so far but i don’t quite get the picture on how to “think this way”…
i mean i DO understand what the examples do and mean but i wouldn’t be able
to write them myself…

i know there are some other (more) functional languages than ruby but i
don’t have the energy nor the time to learn them. and i just want to get a
good coding style in ruby…

the book of the pragmatic programmers is a very good reference but i lack
some sort of
think-styleguide-on-functional-programming-for-procedural-programmers or
something likewise… or is there just some kind of “magic sentence” - a
“spell” ;-)) that can teach me how to…

anyway, i’d love to get some tips or some recommendations where to find such
a guide (hopefully there’s a free one, i don’t like buying books for i don’t
really have the money - but don’t hesitate to recommend a book - better than
nothing)…

thank you in advance,
patrick

Actually, Ruby is an Object-Oriented Language.
Examples of functional languages are LISP and HASKELL.
I am not sure if you want to grasp the Object-Oriented programming
style or
the Functional programming style. You can use Ruby to write in a
somewhat
functional style if you want to, but Ruby would not be the language to
use to learn functional programming.

You could program in Ruby using a somewhat functional style.

Interesting! Could you point me somewhere for details?

martin

···

MikkelFJ mikkelfj-anti-spam@bigfoot.com wrote:

8-Queen happens to be a nice pattern to sample pixels in, but

jcb@iteris.com (MetalOne) writes:

Actually, Ruby is an Object-Oriented Language.
Examples of functional languages are LISP and HASKELL.

This seems to be a common misperception. Imperative, functional and
object-oriented (OO) are merely axes on the language classification
system (among them OO is the most ambigious term).

All languages have some degree of existence on each axis. It is
incorrect that since Ruby is an OO, it is devoid of imperative and
functional aspects.

YS.

Thursday, December 12, 2002, 12:01:29 AM, Âû íàïèñàëè:

Most other material I have seen have a combination of trivial useless
examples to give the basics, and then advances towards 8-Queen problems to
really illustrate the benefits of functional style - 8-Queen happens to be a
nice pattern to sample pixels in, but otherwise it is hardly a very
interesting example for real life applications.

one of my love examples - is a “quick” sort one-liner:

qs() = ()
qs(x:xs) = qs( a<-xs | a<x ) || x || qs( a<-xs | a>=x )

···

Ñ óâàæåíèåì,
Áóëàò Çèãàíøèí mailto:bulatz@integ.ru
Âåäóùèé èíæåíåð-ïðîãðàììèñò
ÎÎÎ “Èíòåãðàë Àâòîìàòèêà”
ã.Íàáåðåæíûå ×åëíû. Òåë:(8552)51-94-42

Actually, Ruby is an Object-Oriented Language.
Examples of functional languages are LISP and HASKELL.
I am not sure if you want to grasp the Object-Oriented programming
style or
the Functional programming style. You can use Ruby to write in a
somewhat
functional style if you want to, but Ruby would not be the language to
use to learn functional programming.

You could program in Ruby using a somewhat functional style.

i was already aware of that, but i’ve seen some examples of this - maybe i
shouldn’t keep calling it “style” - approach to programming (the functional
approach) around here and the web and was just fascinated by it because it’s
rather different from what i knew.

Yohanes Santoso said in reply to your post:

All languages have some degree of existence on each axis.
i agree with that and moreover think that if a language has such intense
coexistence on two of this axis as ruby (oo, functional) it would be best to
know how to code in each way and use the best way suitable for solving on
every individual problem.

anyway, thx for your reply
patrick.

Yohanes Santoso ysantoso@jenny-gnome.dyndns.org writes:

This seems to be a common misperception. Imperative, functional and
object-oriented (OO) are merely axes on the language classification
system (among them OO is the most ambigious term).

Strictly speaking, imperative, functional and object-oriented are
types of programming style, not types of language. I’ve seen
object-oriented C (and OO assembler, horrors) and imperative LISP. As
they say, “you can write FORTRAN in any language”, it’s just
relatively difficult in Smalltalk.

···


If a 6600 used paper tape instead of core memory, it would use up tape
at about 30 miles/second.
– Grishman, Assembly Language Programming

“Martin DeMello” martindemello@yahoo.com wrote in message
news:FKOJ9.205315$ka.4793771@news1.calgary.shaw.ca…

8-Queen happens to be a nice pattern to sample pixels in, but

Interesting! Could you point me somewhere for details?

I think I recall having seen n-queen sampling in litterature, but it may
also be my own invention.

An image is divided into blocks of n by n pixels. n-queen sampling samples n
out n^2 pixels in each block.

The following is from my own experience only - the n-queen pattern has the
nice feature that the pattern resembles a random pattern which is generally
a good way to sample (good coverage and avoids apparent aliasing effects),
yet you know exactly what pixels have been sampled. You can rotate the
pattern such that you get 100% sample coverage after n iterations.

The reasons I worked on this was as follows (based on flawed recollection):

  • some years back at university we had a 9 man large group that should
    develop various software for controlling a robot arm remotely via network.
    The system had visual feedback and estimated wireframe model of robot.
    The controlling computer in the robot room was not very fast. It should
    manage robot control, video capture and network communication over limited
    bandwidth. Although the plan was to use standard video compression such as
    mpeg, but we realized we had to invent our own method to meet these
    criterias of fast low bandwidth compression and because the OS/9 operating
    system made it difficult to pick useful software off the internet which was
    not as vast as now. Later that task might have been solved by VNC, more CPU
    and more bandwidth.

We decided that a robot image is mostly static except where the robot arm
is. Therefore you need to identify motion and only ship those image blocks
containing changes. How do you detect changes fast? You sample selected
pixels in a pattern that gives good coverage. This allows you to scan n
times faster than otherwise, and still guarantee 100% change detection once
the image is complete sampled (or not quite, as the live image may have been
resampled meanwhile). We developed a simple point system for each block that
involved the amount of change and how long time had passed since last
change. Then for a given bandwidth we selected as many blocks as possible
and transmitted those, resetting their block score to zero. In order to
avoid sorting blocks for selection we created a crude priority list where
iterated over the block score table and scoped all blocks above ‘threshold’.
Then we halved the threshold and repeated until we had enough blocks to
transmit.
In this way we could very quickly detect change and simply transmit as many
blocks as possible while dynamically being able to tune the bandwidth
consumed. We didn’t try to compress transmitted blocks, but that could also
be done - we assumed the CPU would probably be better used by making block
sizes smaller decreasing n in the n-queen pattern.

One interesting aspect of the algorithm is the exploitation of how the brain
works: The brain tracks motion and completes motion even when information is
absent. Thus it would be sufficient to give the brain hints about the motion
in order to establish realtime feedback. The quality is obviously depending
on how much bandwidth could be provided.

I have some C-source for the image compression - I haven’t looked at for
quite a while - is quite small.

Mikkel

···

MikkelFJ mikkelfj-anti-spam@bigfoot.com wrote:

Hello Martin,

Thursday, December 12, 2002, 1:21:54 AM, you wrote:

8-Queen happens to be a nice pattern to sample pixels in, but

Interesting! Could you point me somewhere for details?

btw, in fido7.ru.compress conference several weeks ago was published
BWT/UNBWT and MTF/UNMTF algorithms in Haskell. BWT, for example, is
about 10 lines (but very, very low-speed :slight_smile:

it’s the algorithms used in bzip2, if someone want to know

···


Best regards,
Bulat mailto:bulatz@integ.ru

one of my love examples - is a “quick” sort one-liner:

qs() = ()
qs(x:xs) = qs( a<-xs | a<x ) || x || qs( a<-xs | a>=x )

Here is quick sort in Ruby
def qsort(arr)
return if arr.length <= 0
x, *xs = arr
qsort(xs.select{ |y| y <= x } ) + + qsort(xs.select{ |y| y > x } )
end

Very neat.

martin

···

MikkelFJ mikkelfj-anti-spam@bigfoot.com wrote:

The following is from my own experience only - the n-queen pattern has the
nice feature that the pattern resembles a random pattern which is generally
a good way to sample (good coverage and avoids apparent aliasing effects),
yet you know exactly what pixels have been sampled. You can rotate the
pattern such that you get 100% sample coverage after n iterations.