Blogs

Decoupling and Encapsulation: how do they interplay

Submitted by metaperl on Sat, 08/20/2005 - 6:38am.

I recently found a fascinating job but was dismayed when they said they expected people to use HTML::Mason (a jsp/could-fusion/php-like embedded HTML generator for Perl) and MySQL. Instead of verbalizing my dismay, I decided to finally write the ultimate document on why inline HTML generation and MySQL are Bad Choices ... that's right

In doing so,I had to be tongue in cheek about one thing. I value decoupled program elements and that's why I like Haskell... it gives one maximum ability to define small pieces and maximum operators for flexible composition, allowing one to form complex computation strategies from small, well-tested, well-understood pieces.

Of course, the problem becomes: decoupling means that you have weaker encapsulation.

So we arrive at the battle of the buzzwords... which is better: decoupled program elements or encapsulated ones?

any feedback on this simple financial calculator?

Submitted by metaperl on Fri, 08/19/2005 - 4:59am.

-- bills paid on the 15th of the month
on 15 = [100.00,110.00,60.00,52.00,100.00,150.00,450.00,240.00,68.45]

-- bills paid on the 30th of the month
on 30 = [300.00,75.00,150.00,125.00,125.00,150.00,100.00]

-- total paid on 15th of month
s 15 = sum (on 15)

-- total paid on 30th of month
s 30 = sum (on 15)

-- total monthly bills
tot = (s 15) + (s 30)

your code is not generalized until its types are

Submitted by metaperl on Fri, 08/12/2005 - 2:00pm.

I was staring at chapter 10 in SJT's text and realized that each time he created a more general function, it was done by creating a function which worked on any type whereas the original function was "hardcoded" to process only the type of data.

well, there goes 8 hours up in smoke

Submitted by metaperl on Fri, 08/12/2005 - 4:13am.

I took a day off work to get an eye exam and then headed to Jamba Juice to get 4 32-oz containers of fresh-squeezed juice. I headed home, ready to continue my Haskell explorations.

For some reason, I decided to take just a few minutes to try to get my old laptop ip masquerading to my main machine, since I had never managed this before.

Well, boy was that a mistake. I tried guarddog and guidedog. I tried google and IRC. I tried man route. I tried shorewall. 8 hours later:

- no better at Haskell
- no ip masquerading
- severely frustrated

The lesson: pick your battles. Linux is an experimenters toolbox. It is very free and open and configurable and you will often have to do research to get anything done (unlike Windows). This is great at times but tempting at others.

I need to realize that I am a software developer who wants to transition from Perl to Haskell and stay focused on that. I will never be as good at networking as the sysadmins at work and I dont need to waste my time in such efforts.

had I bought a wireless bridge like linksys tech support said, or had I tried out the wireless card I bought, I would have had an easy and predictable path through unknown territory.

I havent even mentioned that a normal ethernet cable doesnt work. If it had not been for the phone tech who came out to fix my "phone problem" (ok, ok, sorry my laundry disconnected my phone and it was buried under 4 feet of laundry so I never noticed it), ... anyway. the phone tech saw I was using Linux and told me about his 4-processor Itanium and how Microsoft charges 2700 for its Windows version for this machine. He noticed the wire running between my machiens and told me I needed something called a crossover cable... yet another unknown thing...

So: pick your battles, stick to your guns and cherish your free time for what you need to get done... even if that means iconifying your IRC client, being a bit dumb about a bunch of things not related to your goal... and I didnt mention the lady who I told I wanted a vegetarian plate at the restauranet. I lift up the scrambled egg and there staring at me if roasted beef... she tried to scrape it out of the bown and give it back to me.... and yes, there were still beef chunks in there after her attempt to save some dollars... and of course I would've been urling my guts out for hours had this vegetarian body accidentally taken in that garbage.

Rule #2: dont play in the fire unless you dont mind getting burnt. Pick vegetarian restaurants if you are a vegetarian. If they serve meat at all, then stay the hell out... unless you like carcass grease mixed in with your veggies. Or better yet, cook for yourself so you arent wondering what the cook did with his hands, or what tasty ingredients they added that due nothing but constipate and poision the body (ie. table salt, distilled vinegar, white flour, etc etc).

see what your task is a function of - it may be a chunk instead of just an element.

Submitted by metaperl on Wed, 08/10/2005 - 8:36pm.

One reason this task was so difficult for me is that you sort of had to switch gears... up to now in the book every problem involved moving through the list or making decisions about the list one element at a time. In this particular problem, a decision had to be made on a chunk of the input list not just one element.

So, this is the first case where I had to get a chunk of the string and develop code that operated as a function of a chunk of data as opposed to one piece.

my top-level description technique failed me

Submitted by metaperl on Tue, 08/09/2005 - 7:18pm.

I was trying to work problem 7.26 in Simon Thompson's "Craft of Functional Programming":


given a STRING search for SEARCH and replace with REPLACE

Now, having fallen in love with the ability to give top-down specs for things in Haskell, I tried this for starters:


substr search replace input = pre ++ sub ++ post

meaning given an input string, we have the unaltered pre and post parts and a conditionally altered sub part.

But I could not take this top-down description further without searching the string numerous times. John Goerzen was nice enough to come up with this solution for me:


{-

I'd recurse over the string, dropping the first character on
each recursion
then I'd compare the first (length search) characters to (search)
If they're the same, I'd return (replace ++ xs)
otherwise return x:self xs

-}

subst search@(s:ss) replace input@(i:is)
| chunkSearch search input = replace ++ (drop (length search) input)
| otherwise = i : subst search replace is

-- presumes length a < length b
chunkSearch a b = a == take (length a) b

Well, I like this solution, but I'm a bit worried that my top-down approach fell through on me. It's interesting how he simply builds the pre part on the fly and then pastes the post part in with replace

Spelunking Darcs for the Good of MissingH???

Submitted by metaperl on Thu, 07/28/2005 - 7:39pm.

I have not grokked the darcs source code, but from using it, I am willing to be that it has some very good file and i/o libraries that would be generally useful.

Has anyone considered factoring out this functionality into MissingH for re-use elsewhere?

HELP NEEDED

Submitted by babyneri on Thu, 07/28/2005 - 12:24pm.
::

can anyone help with with these questions

1. Given two values say, A and B, display the sum of all odd numbers between A and B exclusive.
2. Given a number X, display the factorial of X
3. Given two characters display the larger of the two.
4. Given a list of integers display the smallest number in the list
5. Given a list of characters display True or False if the list contains capital letters
6. Given a list of integers display the sum of all numbers in the list
7. Given a list of integers display the sum of all even numbers

my teacher ran through the course without teaching it properly.....can anyone help me write it as simply as possible?

please email me if you can: risha15@yahoo.coom

Drupal is not Burger King!

Submitted by metaperl on Sun, 07/24/2005 - 11:06am.

The old motto of Burger King when I was growing up was: "Have it Your Way"

Drupal is a nice system, but note what I must do. I must visit this website, click on what I want to do and then fill out a bunch of forms to get my content up.

I have written two things recently. One on the Haskell pipeline paradigm:
http://scannedinavian.org/~metaperl/haskell/is-beautiful/pipeline-philosophy/

And another on the ease with which top-down development is in Haskell:
http://scannedinavian.org/~metaperl/haskell/is-beautiful/top-down-is-easy/

But I did not savor the prospect of dealing with Drupal's limited HTML... in other words, I wanted to HAVE IT MY WAY.

Why couldn't I just open up a directory under a tree in a shell, code up my text and/or HTML and then have some sort of skulking process notice that a new directory was there and add the content to the system?

Maybe only after a darcs push was done should this happen so that you can develop and only publish when you push.

But at anyrate I envision a system that is built up from a large collection of handwritten directories without visiting websites and filling out forms, blah blah.

Exercise 6.16 in Thompson is Complete!

Submitted by metaperl on Wed, 07/20/2005 - 6:31am.

Well, 6 weeks ago I was low in motivation and actually bought 2 C++ books and intended to learn C++ and enter ICFP contest using it.

However, I think I am too old to learn C++... I really just didn't get the hang of it. Actually, I think I rushed things.

But anyway, Haskell is much easier on my head. I have been struggling with the image superposition exercises (6.16) for a long long time. In the process I found a bug in the Haskell tracer, hat:

http://permalink.gmane.org/gmane.comp.lang.haskell.hat/176

And I learned how to use GHC's Debug.Trace.

I also was hit smack dab in the face with Haskell's non-determinism. I was using Debug.Trace.trace() and was confused about how output from trace and program output were interleaved:

http://article.gmane.org/gmane.comp.lang.haskell.cafe/7769/match=

And of course, I learned that lists in Haskell often end up reversed when you process them to generate another list... I kept staring at my final result until I realized that it was correct, only upside-down!

This was an excellent exercise and I'm glad that I did not skim over it. You've got to learn the language and how to debug the language as well and Thompson has once again shown why he has probably the best book introducing the Haskell language (at least better than Algorithms, haven't messed with my Hudak intro book).