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

Wheeler–DeWitt equation, 6

John Wheeler is also a co-father of the Wheeler-DeWitt equation, the vanishing Hamiltonian constraint applied to the “wave function of the Universe” in quantum gravity that is expected to replace the equations of motion as soon as it is properly formulated. The Hartle-Hawking wave function remains the most famous solution of the WDW equation.

— John Wheeler: 1911-2008

— Lubos Motl

2012.08.11 Saturday ACHK

Meaningful 7

upgrade path

有出路

— Me@2012-08-06 8:28:47 AM

Somewhere in the course of doing Viaweb, someone gave me a very useful piece of advice: users always want an upgrade path, even though as a rule they’ll never take it. Rtml was our upgrade path. If you wanted to, you could get absolute control over everything on your pages.

— Lisp in Web-Based Applications

— Paul Graham

2012.08.11 Saturday (c) All rights reserved by ACHK

Before Sunrise

Before Sunrise is a 1995 romantic drama film directed by Richard Linklater and written by Linklater and Kim Krizan.

The plot is minimalist, since aside from walking and talking, not much happens. The two characters’ ideas and perspectives on life and love are detailed.

The film received high critical praise at the time of its release and has received a rare perfect 100% rating of positive reviews on Rotten Tomatoes and a 77 metascore on Metacritic.

— Wikipedia on Before Sunrise

.

情留半天 Before Sunrise (1995)

日落巴黎 Before Sunset (2004)

情約半生 Before Midnight (2013)

.

2012.08.10 Friday ACHK

個人客觀科學 2.3

真相客觀程式 3.3

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

(安:絕大部分人也不會研究「自己科學」。他們也不會以「自己」為「科學」研究的對象。換句話說,平常人對自己不夠客觀。他們會覺得自己有「自由意志」,例如「我要早上六時起床,就自然可以六時起到床。」

實情則不是那麼簡單。能否在六時正成功起床,除了意願外,還取決於你在之前一晚何時開始睡、身體狀態的好壞 和 當時起床後需不需要上班 等等。)

無錯。大部分人以為,物件和動物需要遵守自然定律,而人,尤其是自己,則可以任意違反自然定律,而毋須承受任何報應惡果。他們忘記了,自己也是人,人也是動物,動物也是物件,同樣受物理定律、化學定律 和 生物定律 的主宰。

例如,想六時起床,就必須設定好「六時起床」的所有先決條件:要保持身體健康,即是不要在平日累積太多的疲勞、之前一晚要早一點睡 和 調校好鬧鐘 等等。又例如,要別人信任你,你就要在長年累月地保持誠信。

所以,所有人都應該受科學的訓練。科學教育的最重點,不在於培養未來的科學家,而在於潛移默化,使大部分未來人,也知道這個世界有「自然定律」,從而習慣事事「以自然定律為中心」。

— Me@2012.08.10

2012.08.10 Friday (c) All rights reserved by ACHK

Tricki

Sir William Timothy Gowers, FRS (born 20 November 1963, Wiltshire) is a British mathematician. In 1998 he received the Fields Medal for his research connecting the fields of functional analysis and combinatorics.

Tricki

Tricki.org is a Wikipedia-style project collecting methods of mathematical problem solving conceived in 2008 and launched by Gowers, Olof Sisask and Alex Frolkin in March 2009. Terence Tao and Ben Green are among those to have contributed articles.

The Cost of Knowledge

In January 2012 Gowers made a post on his personal blog which initiated a project called The Cost of Knowledge. The blog post called for a boycott of Elsevier. The Cost of Knowledge is a petition for change in which researchers commit to discontinue support for Elsevier journals. As a result, Gowers has been credited with starting the Academic Spring.

— Wikipedia on Timothy Gowers

2012.08.09 Thursday ACHK

理智與感情

.

The good life is one inspired by love and guided by knowledge.

– What I Believe (1925), Bertrand Russell

.

發乎情 止乎禮 –

.

恰如其份的感性,是理性的極致。

— Me@2011.11.02

.

.

2012.08.09 Thursday (c) All rights reserved by ACHK

巴士原理

背誦製成品 8

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

除非考試課程範圍有要求,否則,有很多公式或者現成的方法,你可以立刻使用,而毋須百分百追求它們的來源出處。千萬不要有一個心態:我要百分百明白一樣東西,才肯使用它。有著這個心態的話,莫講話讀書考試,就連日常生活,你也會過不到,例如:

甲:你不是趕時間的嗎?為什麼不肯上那輛巴士(公共汽車)?

乙:因為我想來想不通,那輛巴士運作原理。為什麼它可以高速行走的呢?

甲:……

這個雖然是一個荒誕笑話。但是,有很多年青人,正正是抱著這個荒誕的心態去讀書。我年青時都是那樣,節外生枝後再節外生枝,導致浪費了極多的讀書時間。

留意,我並不是叫你在讀書時,每每不求甚解。我的意思是,「百分百明白」並不是合理的期望,因為你沒有無限的時間。學習任何一樣東西時,明白到一定的程度後,你就要立刻練習使用,而不應節外生枝。

至於要明白到什麼程度,則要視乎上文下理。例如,如果你是巴士乘客,你只要明白如何付款、在那個站上車 和 在那個站下車。即使你是巴士司機,你亦毋須明白太多巴士的機械原理。你只要明白如何安全地駕駛那輛巴士就可以。如果你是修理巴士的師傅,你就真的要明白,為何巴士可以行走。

— Me@2012.08.09

2012.08.09 Thursday (c) All rights reserved by ACHK

Ring circuit

The ring circuit came about because Britain had to embark on a massive rebuilding programme following World War II. There was an acute shortage of copper, and it was necessary to devise a scheme that used less copper than would normally be the case.

— Wikipedia on Ring circuit

2012.08.08 Wednesday ACHK

No reading

Many people don’t read nor listen because they don’t want other people minds “invade” theirs.

In short, they are dumb.

— Me@2012.08.07

ARIADNE
Why are they looking at me?

COBB
Because you’re changing things. My subconscious feels that someone else is creating the world. The more you change things, the quicker the projections converge on you.

ARIADNE
Converge?

COBB
They feel the foreign nature of the dreamer, and attack-like white blood cells fighting an infection.

ARIADNE
They’re going to attack us?

COBB
Just you, actually.

— Inception

2012.08.08 Wednesday (c) All rights reserved by ACHK

個人客觀科學 1.2

真相客觀程式 3.2

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

(安:那為什麼「科學」可以百分百「以事實為中心」,而其他類型的學科卻不跟隨?)

那只是「字眼定義」,或者「科目命名」的問題。凡是肯百分百「以事實為中心」的學科,都已經被我們稱之為「科學」,歸類成「科學」了。

— Me@2012.08.07

2012.08.07 Tuesday (c) All rights reserved by ACHK

Wheeler–DeWitt equation, 5

In the new paper, this concept is taken very seriously. “The wave function” is interpreted as nothing else than the Hawking-Hartle wave function of the Universe. You know, the Hawking-Hartle wave function is something like a wave functional of quantum gravity that solves the Wheeler-deWitt equation (a sophisticated definition of the quantum equation

    H.psi = 0

that is appropriate in general relativity but whose exact meaning requires a working quantum theory gravity, i.e. it requires string theory). The Hawking-Hartle wave function is a functional of the fields of quantum gravity on S^3, if you allow me to deal with the “most realistic” example, and this functional may be calculated as the path integral of quantum gravity defined in the ball B^4 inside this S^3, with the right boundary conditions at the sphere S^3. The Hawking-Hartle state is then the functional of these boundary conditions.

— Lubos Motl

2012.08.06 Monday ACHK

Eleven dimensions of color

Diclaimer: I have full-color vision, but with color blindness there are (roughly) two dimensions of color, whereas most people have three (and a black and white photograph has one). What this means is that with 1D color, you can sort all of the colors you see into a line — dark to light. With 2D color, you can sort all of the colors you see on a flat plane. With 3D color, you require stacking colors.

Now, there’s plenty of animals out there that have more than 3 dimensional color (they have more than 3 types of cone cells). So two colors that look the same for a normal person will look completely different for an animal. Some octopi have eleven dimensions of color! To them almost every human would be severely colorblind.

It’s thought that some people might be tetrachromats. They have 4 types of cone cells because of a genetic mutation. There’s still some questions on how this extra information is processed by the brain, but there’s a chance that for these people almost everyone else seems colorblind. They are able to distinguish two colors that everyone else cannot. This also means that television won’t reproduce colors correctly for them, and it won’t look natural.

— Xcelerate 1 day ago

— Hacker News

2012.08.04 Saturday ACHK

個人客觀科學

真相客觀程式 3

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

(安:一般人以為,只有在學校裡,才會研習科學;只有物理、化學、生物等,才算是科學。其實,即使是日常的個人生活,很多時也很需要運用到,從科學研習中,提煉出來的「科學精神」和「科學方法」。

例如,我需要研究「阿安科學」。例如,我發現,如果在之前一晚,十一時就睡覺,早上六時成功起床的機會高達 90%;如果我遲過深夜十二時才睡,早上六時成功起床的機會則近乎 0%。

根據這個數據,我可以完善我的生活規劃。)

無錯。其實,所謂「科學」,就是「以事實為中心」。大部人的生活,也不是以事實為中心,而是以情緒、感覺 和 喜好為中心,導致枉費一生。

「科學」比其他類型的學科,有較明顯的進步,較高速的發展,是因為她堅持「以事實為中心」。如果你可以做到「不樂觀」和「不悲觀」,而保持客觀的話,你就已經是跟從「科學法度」。你的人生,將會有科學級數的進步發展。

所以,所有人都應該受科學的訓練。科學教育的最重點,不在於培養未來的科學家,而在於潛移默化,使大部分未來人,也習慣「以事實為中心」。

— Me@2012.08.03

2012.08.03 Friday (c) All rights reserved by ACHK

Wheeler–DeWitt equation, 4

Lumo ‧ 7 years ago

Dear Anonymous,

I agree that loop quantum gravity may also be viewed as an attempt to make sense out of the WDW equation. Unfortunately it is not a consistent or successful attempt – simply because the Hamiltonian constraint can’t be defined. (In fact, even the physical Hilbert space is not well-defined.) See an older article “A very meaningful paper on loop quantum gravity”.

All the best
Lubos

Anonymous ‧ 7 years ago

But even the Wheeler-deWitt equation needs to be regularized!

Lumo ‧ 7 years ago

That’s right. This is why you need string theory to transmute the WDW equation from a heuristic speculation to a meaningful law of physics.

— Lubos Motl

2012.08.02 Thursday ACHK