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

Euler problem 27.2

Haskell

——————————

problem_27 = -(2*a-1)*(a^2-a+41)
    where n = 1000
          m = head $ filter (\x -> x^2 - x + 41 > n) [1..]
          a = m - 1 

This is the “official” Haskell solution to Euler problem 27.

This solution is incomprehensible. The following hints are as far as I can get.

Prerequisite considerations:

b must be a prime number, since x^2 + a*x + b must be a prime number when n=0.

a = m - 1 is a choice of the prime number b (in x^2 + a*x + b) just(?) smaller than 1000.


a == 32

(\x -> x^2 – x + 41) 31 == 971

Why not the prime number 997?

— Me@2015.06.14 08:53 AM

It is because 997 is not a prime number generated by the formula x^2 - x + 41.

— Me@2015-06-30 11:07:53 AM

map (\x -> x^2 - x + 41) [0..40]

is for generating 41 primes with the greatest Euler’s lucky number 41.

— Me@2015.06.14 08:34 AM

2015.07.01 Wednesday (c) All rights reserved by ACHK