Functional programming 6

A central concept in functional languages is that the result of a function is determined by its input, and only by its input. There are no side-effects! 

— Why Haskell matters

My question is, if a function makes changes only within its local environment, and returns the result, how can it interact with a database or a file system? By definition, wouldn’t that be accessing what is in effect a global variable or global state?

What is the most common pattern used to get around or address this?
   
— edited Dec 7 ’11 at 2:10, Matt Fenwick
   
— asked Dec 6 ’11 at 20:18, juwiley

The most common pattern for dealing with side-effects and impurity in functional languages is:

  •     be pragmatic, not a purist
  •     provide built-ins that allow impure code and side-effects
  •     use them as little as possible!

Examples:

  •     Lisp/Scheme: set!
  •     Clojure: refs, and using mutating methods on java objects
  •     Scala: creating variables with var
  •     ML: not sure of specifics, but Wikipedia says it allows some impurity

Haskell cheats a little bit — its solution is that for functions that access the file system, or the database, the state of the entire universe at that instant, including the state of the filesystem/db, will be passed in to the function.(1) Thus, if you can replicate the state of the entire universe at that instant, then you can get the same results twice from such a function. Of course, you can’t replicate the state of the entire universe at that instant, and so the functions return different values …

But Haskell’s solution, IMHO, is not the most common.

(1) Not sure of the specifics here. Thanks to CAMcCann for pointing out that this metaphor is overused and maybe not all that accurate.   

— edited Dec 7 ’11 at 2:11

— answered Dec 6 ’11 at 22:00, Matt Fenwick

— Stack Overflow

2012.08.12 Sunday ACHK

Earn respect

Prisons: How do you earn more respect in prison?

James Houston, inmate San Quentin

There was a time when I believed respect was about others seeing me as a tough guy. Over the years with maturity as well as help from programs like the TRUST and The Last Mile, my belief system has been challenged and as a direct result, my belief system has changed.

Now I see in prison, as well as in the free world, respect starts with self. Since my belief system has changed, the way I interact in my environment has changed, and the way my environment interacts with me has also changed. I no longer hide or run away from how I really feel. I don’t gossip or talk about people behind their back. I try as best I can to remain fair and impartial.

I can’t say that it gains respect with everyone. I can say it gains my respect with those who have respect for themselves.

All communications between inmates and external channels are facilitated by approved volunteers since inmates do not have access to the internet. This program with Quora is part of The Last Mile San Quentin. Twitter: @TLM
Post ‧ 9:57 on Thu Aug 02 2012

http://www.quora.com/Prisons/How-do-you-earn-more-respect-in-prison

2012.08.12 Sunday ACHK

個人客觀科學 3

這段改編自 2010 年 3 月 27 日的對話。

正如,你上次問我,如果你有一個缺點是,每逢走近麵包店,就會瘋狂地購買過多麵包的話,你應該怎麼辦,才能去除這個缺點。我的答覆是,不要走近麵包店。那樣,你就從來也不會遇到「瘋狂購買麵包」這隻怪獸。你就從來也毋須去處理牠。

你想一想的話,就會發現,這個根本是一個邏輯定律。如果你在走近麵包店之前,都說服不到自己,不要走近麵包店的話,你走近了麵包店時,就更加說服不到自己,不要購買過多的麵包。

有很多惡習的成因,都是基於這個邏輯結構,例如吸煙。吸煙成癮的主因是,吸煙者以為自己,有一個凌駕於自然定律的自由意志:「我吸一點煙不會有什麼事。而且我可以隨時不吸。」問題是,這個是一個邏輯問題。如果你在從來沒有吸煙之時,都說服不到自己,不要吸第一支煙的話,你在吸了第一支煙後,你又怎會說服到自己,不要吸第二支煙呢?

相反,如果知道自己和萬物蒼生一樣,同樣受制於自然定律的話,你就會了解到,即使自己誓神劈願,戒煙同樣會異常困難。那樣,你就從來也不肯吸第一口煙。

「戒煙」的最好方法是,從來不要吸煙。

地球人往往以為,自己有一個凌駕於自然定律的自由意志,其實沒有。人(或者宇宙中的任何東西)只有受制於自然定律的自由意志。或者說,自由意志建基於自然定律。

想要在這個宇宙間呼風喚雨,就要極度詳細地,掌握各種自然定律,從而善加利用。讀理科有助瞭解「物理定律」;讀文科則有助瞭解「人情定律」。兩門定律缺一不可。

— Me@2012.08.12

Knowledge is power. — Sir Francis Bacon

The power is in the principles. — Stephen R. Covey

權力來自於對自然定律的了解和運用。 — Me@2012-08-12 9:56:20 AM

2012.08.12 Sunday (c) All rights reserved by ACHK