Folding an infinite list

One big difference is that right folds work on infinite lists, whereas left ones don’t! To put it plainly, if you take an infinite list at some point and you fold it up from the right, you’ll eventually reach the beginning of the list. However, if you take an infinite list at a point and you try to fold it up from the left, you’ll never reach an end!

Learn You a Haskell for Great Good!

Note that the key difference between a left and a right fold is not the order in which the list is traversed, which is always from left to right, but rather how the resulting function applications are nested.

  • With foldr, they are nested on “the inside”

    foldr f y (x:xs) = f x (foldr f y xs)

    Here, the first iteration will result in the outermost application of f. Thus, f has the opportunity to be lazy so that the second argument is either not always evaluated, or it can produce some part of a data structure without forcing its second argument.

  • With foldl, they are nested on “the outside”

    foldl f y (x:xs) = foldl f (f x y) xs

    Here, we can’t evaluate anything until we have reached the outermost application of f, which we will never reach in the case of an infinite list, regardless of whether f is strict or not.

— edited Oct 24 ’11 at 12:21, answered Sep 13 ’11 at 5:17, hammar

— Stack Overflow

2015.08.23 Sunday by ACHK

Problem 14.1b

A First Course in String Theory
 
 
14.1 Counting bosonic states

~~~

Let N(n, k) = {n + k - 1 \choose k - 1}, the number of ways to put n indistinguishable balls into k boxes.

p.318 “For open bosonic strings \alpha' M^2 = N^\perp - 1, …”

 
When \alpha' M^2 = 3, N^\perp = 4, the cases are: 

1. four a_1^\dagger‘s:

N(4,24) = 17550

2. one a_2^\dagger and two a_1^\dagger‘s:

24 \times N(2,24) = 24 \times 300

3. two a_2^\dagger‘s:

N(2,24) = 300

4. one a_3^\dagger and one a_1^\dagger:

24 \times 24 = 576

5. one a_4^\dagger:

24

 
Total number of possible states for N^\perp = 4 is 25650.

— Me@2015-08-13 12:05:57 PM
 
 
 
2015.08.13 Thursday (c) All rights reserved by ACHK

回到過去 1.2

甲:這次考得這麼差。如果可以回到過去,我一定可以考得好一點。

乙:那樣,回到過去前,你必須先清除試題記憶。否則,那就是作弊。
   
甲:那樣,「回到過去」還有什麼用?

乙:沒有用。

— Me@2015-07-27 09:01:17 PM

— Me@2015-08-07 08:01:48 PM

2015.08.08 Saturday (c) All rights reserved by ACHK

注定外傳 1.3

我不能話你這個講法錯。但是,如果你真是這樣問,我大概只可以答「不可以」,因為,如果真的是「百份百相同」的情境,又怎可能有不同的結果呢?

如果有可能有不同結果,那樣,引起另一結果的因素,總會與引起原本結果的因素,至少有一點不同。

(問:不是呀。在量子力學中,即使有兩組百分百一樣的物理系統,即使它們獲得完全相同的輸入,都可能有不同的輸出。)

你大概正確。但是,你要留意,量子力學中的「百分百一樣」物理系統,未必是你心目中的「百分百一樣」。

第一,量子力學(或其他任何科學)中的數學公式,只是數學模型,簡稱「理論」。模型的意思是,現實的近似,而不是現實的全部細節。

例如,地圖是實地的大概。試想想,一幅地圖比實地小那麼多,又怎可能包含了實地的所有細節呢?

作為實地的大概,只要令到閱者,準確到達目的地,一幅地圖,就已經盡了它的責任。

假設有一位地圖顧客(甲),向一位地圖製作人(乙),要求一幅準確一點的地圖。

乙:沒有問題。如果想令一幅地圖準確一點,我可以把地圖畫得大一點,那就可以包括更多細節。

甲:不行。那還不夠準確。

乙:沒有問題。我可以把地圖畫得再大一點,以包括再多細節。

甲:不行。那還不夠準確。

乙:沒有問題。我可以把地圖畫得又再大一點,以包括又再多細節。

甲:不行。那還不夠準確。

乙:你想多準確?

甲:完全、百分百、鉅細無遺、分毫不差。

乙:那唯有造一張「一比一的地圖」。

甲:什麼是「一比一的地圖」?

乙:即是與實地一樣大小的地圖。

換句話說,即是以實地為地圖。那樣,地圖就會失去了它原本的意義。

甲:什麼是「意義」?

乙:用途就是意義。

地圖本來的用途,是把實地的重點表達出來,從而帶領你,找到要去的地方。

當地圖的比例是一比一,與實地一樣大時,你就不能使用它了。以實地為地圖,即是沒有地圖。

地圖不是百分百準確,並不是地圖的缺點。相反,那是地圖的優點,因為,正正由於地圖只是實地的大概,它比實地小很多,可以指出重點,引導你到達目的地。

同理,科學理論中的數學模型,並不是現實的全部。理論不會包括實際的所有細節,並不是理論的缺點。

— Me@2015-08-04 07:57:59 AM

2015.08.05 Wednesday (c) All rights reserved by ACHK

Exercise One

You Could Have Invented Monads! (And Maybe You Already Have.)

Write the function bind.

——————————

bind f' (gx,gs) = (fx, gs++fs)
                  where
                    (fx,fs) = f' gx

The meaning of bind here:

1. apply f' to the last result data gx, generating the new data fx; and

2. concat the last debug string, gs, to the string generating by f', fs.

— Me@2015.07.19 10:25 PM

2015.07.27 Monday (c) All rights reserved by ACHK

Problem 14.1a

A First Course in String Theory
 
 
14.1 Counting bosonic states

~~~

n is the number of a‘s.

k is the number of different a‘s.

For a^{i_1} a^{i_2}, n=2.

(Indices i_1 and i_2 are not powers. Instead, they are just upper indices for representing different a‘s.) 

When all a‘s commute, a^1 a^2 and a^2 a^1, for example, represent the same state. So we have to avoid double-counting, except for the same a‘s states, such as a^3 a^3.

By direct counting, without using the formula, the number of products of the form a^{i_1} a^{i_2} can be built is

\frac{k(k-1)}{2} + k
= \frac{(k + 1)k}{2}

Let N(n, k) = {n + k - 1 \choose k - 1}, the number of ways to put n indistinguishable balls into k boxes.

By using the formula, the number of products of the form a^{i_1} a^{i_2} can be built is

N(2,k)

= \frac{(2+k-1)!}{2!(k-1)!}

= \frac{(k+1)k}{2}

— Me@2015-07-26 08:43:22 AM
 
 
 
2015.07.26 Sunday (c) All rights reserved by ACHK

Share

Publish! 7.2

We like to share because we like our information, ideas, and insights to have a future.

— Me@2011.08.07

2015.07.19 Sunday (c) All rights reserved by ACHK

避免犯錯 1.2

二十分開始 3

這段改編自 2010 年 8 月 11 日的對話。

.

You probably will make those mistakes anyway.

You cannot choose whether to make those mistakes or not.

All you can choose is whether to make those mistakes before or during the exams.

— Me@2015.07.16

.

平日要操練題目時,不要害怕犯錯。

反而,你要有一個心態:

在大部分情況下,你都始終會犯那些錯誤。

你不能選擇,犯不犯那些錯誤。

你可以選擇的,就只是「什麼時候犯」。

你想考試前犯?還是考試時?

— Me@2018-02-14 09:51:15 PM

.

.

2015.07.17 Friday (c) All rights reserved by ACHK

Haskell/Understanding monads/IO, Exercises 2

Haskell

Generalize the bunny invasion example in the list monad chapter for an arbitrary number of generations.

— Wikibooks

——————————

import Control.Monad

generation = replicate 3
gen n = foldr (<=<) return (replicate n generation)

---

["bunny"] >>= gen n

Additional notes:

m >>= g ≡ join (fmap g m)
xs >>= f = concat (map f xs) -- for the List Monad

---

f >=> g
= (\x -> f x) >>= g
= concat (map g (\x -> f x))


---


xs >>= f >=> g
xs >>= (f >=> g)
xs >>= ((\x -> f x) >>= g)
(xs >>= f) >>= g

xs >>= (return >=> g >=> h)
((xs >>= return) >>= g) >>= h


---


xs >>= return
= concat (map f xs)
= concat (map return xs)
= concat (map return ["bunny"])
= concat ([return "bunny"])
= concat ([["bunny"]])
= ["bunny"]


---


-- foldr f acc []     = acc
-- foldr f acc (x:xs) = f x (foldr f acc xs)

-- foldr (<=<) return (replicate n generation)

foldr (<=<) return [h,g]
= (<=<) h (foldr (<=<) return [g])
= (<=<) h ((<=<) g (foldr return []))
= (<=<) h ((<=<) g return)
= h <=< g <=< return


--- 


compose :: [b -> [b]] -> b -> [b]
compose = foldr (<=<) return

foldr (<=<) return (replicate n generation)
= compose (replicate n generation)

— Me@2015-07-08 11:29:40 PM

2015.07.15 Wednesday (c) All rights reserved by ACHK

Problem 14.1 | Problem 12.11

A First Course in String Theory
 
 
14.1 Counting bosonic states

12.11 Counting symmetric products

~~~

a^{I_1} a^{I_2} ... a^{I_n}

i = 1, 2, ..., n

I_i = 1, 2, ..., k

n is the number of a‘s.

k is the number of different a‘s.

For k=6 and n=9, one of the possible configurations is

a^1 a^2 a^2 a^4 a^4 a^4 a^5 a^6 a^6

The problem is equivalent to that of assigning n balls to k boxes.

Box 1 collects the a‘s with index 1.

Box 2 collects the a‘s with index 2.

— Me@2015-07-13 03:34:32 PM
 
 
 
2015.07.14 Tuesday (c) All rights reserved by ACHK

Things You Should Never Do

It’s important to remember that when you start from scratch there is absolutely no reason to believe that you are going to do a better job than you did the first time. First of all, you probably don’t even have the same programming team that worked on version one, so you don’t actually have “more experience”. You’re just going to make most of the old mistakes again, and introduce some new problems that weren’t in the original version.

— Things You Should Never Do, Part I

— Joel Spolsky

2015.07.12 Sunday ACHK

Passion Test

The Top Idea in Your Mind, 6 | 事業愛情觀 6

.

To check whether a project is your true love, ask yourself:

Am I willing to spend INFINITE time on it?

— Me@2015-07-05 04:26:24 PM

.

Do you want it to be one of your lifelong projects?

Are you willing to follow it up for your whole life?

— Me@2015-07-12 11:00:11 AM

.

.

2015.07.12 Sunday (c) All rights reserved by ACHK

注定外傳 1.2

(問:但是,有沒有一個可能是,其實,將來所有事情的所有方面,都是注定的。所謂的「將來」,其實和「過去」一樣,都完全是固定的。

還有,你的講法的另一個不當之處是,當一個人問,某一件事是不是注定的,即使問的時間,是在該事件發生之後,他問題的意思,當然是「那件事,在事前是否注定的?」

否則,問者就是一個傻子。試想想,又怎會有人問,「那件事,在事後是否注定」呢?)

無錯。當一個人問一件事是不是注定時,意思往往是問,該件事可不可以預防。換句話說,問題可以翻譯成:

下次如果遇到類似的情境,可不可以有不同的結果?

(問:為什麼你要講「類似」?

如果只是「類似」,不是「相同」的情況,當然可以有不同結果。為什麼你不直接問:

下次如果遇到相同的情境,可不可以有不同的結果?

我認為,那才是「是否注定」問題的真正意思。)

我不能話你這個講法錯。但是,如果你真是這樣問,我大概只可以答「不可以」,因為,如果真的是「百份百相同」的情境,又怎可能有不同的結果呢?

如果有可能有不同結果,那樣,引起另一結果的因素,總會與引起原本結果的因素,至少有一點不同。 

(問:不是呀。在量子力學中,即使有兩組百分百一樣的物理系統,即使它們獲得完全相同的輸入,都可能有不同的輸出。)

你大概正確。

— Me@2015.05.26

2015.07.10 Friday (c) All rights reserved by ACHK

Euler problem 30

Haskell

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

1634 = 1^4 + 6^4 + 3^4 + 4^4
8208 = 8^4 + 2^4 + 0^4 + 8^4
9474 = 9^4 + 4^4 + 7^4 + 4^4

As 1 = 1^4 is not a sum it is not included.

The sum of these numbers is 1634 + 8208 + 9474 = 19316.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits. 

——————————

sum_5th_powers n = sum $ map ((^5) . digitToInt) (show n)

-- sum_5th_powers 999999 == 354294
-- sum_5th_powers 9999999 == 413343

For a number having more than 6 digits, the fifth powers of its digits cannot be the number itself. So we should consider only numbers with less than 7 digits.

-- sum_5th_powers ______ <= 354294

For the numbers not greater than 354294, the number whose sum_5th_powers can represent the largest number is …

When 2 –> 1, in exchange for 4 –> 9, we earn — we get 354199;
When 4 –> 3, in exchange for 1 –> 9, we earn — we get 353999;
When 5 –> 4, in exchange for 3 –> 9, we earn — we get 349999;
When 3 –> 2, in exchange for 4 –> 9, we earn — we get 299999.

So the maximum number sum_5th_powers can represent is …

-- sum_5th_powers 299999 == 295277

-- 295277
-- 199999

-- sum_5th_powers 199999 == 295246

p30 = [n | n <- [10..m], n == sum_5th_powers n]
    where m = 295246

——————————

— Me@2015-07-06 08:42:07 PM

2015.07.07 Tuesday (c) All rights reserved by ACHK

Quick Calculation 14.8

A First Course in String Theory
 

What sector(s) can be combined with a left-moving NS- to form a consistent closed string sector?

~~~

— This answer is my guess. —
 
 
What is the meaning of “left-moving NS”?

p.322 “Conventionally, the first input in (\cdot, \cdot) is the left-moving sector and the second input is the right-moving sector.”
 
 
What is the meaning of “+” in “NS+”?

p.321 “… we truncate the NS sector to the set of states with (-1)^F = +1. The resulting states comprise the so-called NS+ sector.”

“The NS+ sector contains the massless states and throw away the tachyonic states.”
 
 
What is the meaning of “consistent” closed string sector?

p.322 “… guarantees that the left and right sectors give identical contributions to the mass-squared:”

\alpha' M_L^2 = \alpha' M_R^2

If NS+ is the left sector, the corresponding candidate right sectors are NS+, R+, R-. 
 
 
M in NS-sector, Equation (14.37):

M_{NS}^2 = \frac{1}{\alpha'} \left( - \frac{1}{2} + N^\perp \right)

M in R-sector, Equation (14.53):

M_{Ramond}^2 = \frac{1}{\alpha'} \sum_{n \ge 1} \left( \alpha_{-n}^I \alpha_n^I + n d_{-n}^I d_n^1 \right) = \frac{1}{\alpha'} N^\perp

Equation (14.78):

\frac{1}{2} \alpha' M^2 = \alpha' M_L^2 + \alpha' M_R^2
 
 
NS+ equations of (14.38):

\alpha'M^2=0,~~N^\perp = \frac{1}{2}:~~~~b_{-1/2}^I |NS \rangle \otimes |p^+, \vec p_T \rangle,

\alpha'M^2=1,~~N^\perp = \frac{3}{2}:~~~~\{ ... \} |NS \rangle \otimes |p^+, \vec p_T \rangle,

...

NS- equations of (14.38):

\alpha'M^2=-\frac{1}{2},~~N^\perp = 0:~~~~\{ ... \} |NS \rangle \otimes |p^+, \vec p_T \rangle,

\alpha'M^2=\frac{1}{2},~~N^\perp = 1:~~~~\{ ... \} |NS \rangle \otimes |p^+, \vec p_T \rangle,

...

Mass levels of R+ and R- (Equations 14.54):

\alpha'M^2=0,~~N^\perp = 0:~~~~|R_a \rangle~~||~~|R_{\bar a} \rangle

\alpha'M^2=1,~~N^\perp = 1:~~~~...

...

There are no mass levels in NS+, R+, or R- that can match those in NS-. So NS- can be paired only with NS-:

(NS-, NS-)

— This answer is my guess. —
 
 
— Me@2015-07-05 11:09:32 PM
 
 
 
2015.07.05 Sunday (c) All rights reserved by ACHK

Intelligence and Wisdom

A few days ago I finally figured out something I’ve wondered about for 25 years: the relationship between wisdom and intelligence. Anyone can see they’re not the same by the number of people who are smart, but not very wise. And yet intelligence and wisdom do seem related. How?

What is wisdom? I’d say it’s knowing what to do in a lot of situations. I’m not trying to make a deep point here about the true nature of wisdom, just to figure out how we use the word. A wise person is someone who usually knows the right thing to do.

If wisdom and intelligence are the average and peaks of the same curve, then they converge as the number of points on the curve decreases. If there’s just one point, they’re identical: the average and maximum are the same. But as the number of points increases, wisdom and intelligence diverge. And historically the number of points on the curve seems to have been increasing: our ability is tested in an ever wider range of situations.

The wise are all much alike in their wisdom, but very smart people tend to be smart in distinctive ways.

— Is It Worth Being Wise?

— Paul Graham

2015.07.03 Friday ACHK

技巧管理

這段改編自 2010 年 8 月 11 日的對話。

.

有時,你可能收集了,太多重要的讀書技巧,有吃不消的感覺。你可以這樣處理:

第一,你要意識到,讀書技巧夠用就可以。你並沒有必要,學懂或者執行,「所有」的讀書技巧。

第二,你試試每次,只針對一個問題,只執行一個讀書技巧,把它用到最盡為止。例如,我所講的「魔法筆記」技巧,並不只適用於數學科。你在其他科中,亦應大量使用。

到該個技巧自動運作時,你讀書上的下一個問題,自然會浮現出來。那時,你才考慮嘗試,下一個讀書技巧,針對該個新問題。

— Me@2015.06.29

.

.

2015.07.02 Thursday (c) All rights reserved by ACHK