http://jonasboner.com/2008/10/06/real-world-scala-dependency-injection-di/
http://www.warski.org/blog/2010/12/di-in-scala-cake-pattern/
https://channel9.msdn.com/Events/Lang-NEXT/Lang-NEXT-2012/Reflection-and-Compilers
http://scalatutorials.com/code/2013/07/09/scala-cake-pattern/
https://groups.google.com/forum/#!msg/scala-user/DC2vUuCZmI0/RO3JYoKnj20Jcstdi
https://www.reddit.com/r/haskell/comments/1629ld/olegs_go_at_reproducing_scalas_cake_pattern/
http://blog.originate.com/blog/2013/10/21/reader-monad-for-dependency-injection/
Why 'high order procedures' is useful?
Often the same programming pattern will be used with a number of different procedures. To express such patterns as concepts, we will need to construct procedures that can accept procedures as arguments or return procedures as values. Procedures that manipulate procedures are called high-order procedures, they can serve as powerful abstraction mechanisms, vastly increaseing the expressive power of our language.
Sometimes we can use internal definitions to get the same effect as with let.
And in which cases we couldn't get the same effect?
As programmers, we should be alert to oppotunities to identify the underlying abstractions in our programs and to build upon then and generalize them to create more powerful abstractions. This is not to say one should always write programs in the most abstract way possible; expert programmers know how to choose the level of abstraction appropriate to their task. But it's important to be able to think in terms of these abstractions, so that we can be ready to apply them in new contexts.
1.29 Answer:
This is not very hard, just simply translate the simpson rule definition to code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
1.30 Answer:
Put both the recursive and iterative versions here to compare:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
They produce the same results as expected.
1.31 Answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
|
There might be better ways to translate the formula. The solution above the is not veru accurate and if the upper bound is too large (say 100), it will yield to 'nan+'.
1.32 Answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
1.33 Answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
1.34 Answer:
Apply (f f) with get (f 2), while tring to apply (f 2), error will be issued since a procedure is expected but an integer parameter 2 is given.
1.35 Answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
1.36 Answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
|
1.37 Answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
1.38 Answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
1.39 Answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
1.40 Answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
1.41 Answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
1.42 Answer:
1 2 3 4 5 6 7 8 9 |
|
1.43 Answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
1.44 Answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
1.45 Answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
1.46 Answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
Recursive process
The substitution model of a recursive process reveals a shape of expansion followed by constraction. The expansion occurs as the process builds up a chain of defered operations. The contraction occurs as the operations are actually performed.
Iterative process
An iterative process is one whose state can be summarized by a fixed number of state variables, together with a fixed rule that describes how the state variables should be updated as the process moves from state to state and an (optional) end test that specifies the conditions under which the process should terminate.
In the iterative case, the program variables provide a complete description of the state of the process at any point, while in the recursive case, there is some additional "hidden" information, maintained by the interpreter and not contained in the program variables.
In contrasting iteration and recursion, we must be careful not to confuse the notion of a recursive process with the notion of a recursive procedure. When we describe a procedure as recursive, we are referring to the syntactic fact that the procedure definition refers (either directly or indirectly) to the procedure itself. But when we describe a process as following a pattern that is, say, linearly recursive, we are speaking about how the process evolves, not about the syntax of how a procedure is written. It may seem disturbing that we refer to a recursive procedure as generating an iterative process.
If Euclid's Algorithm requires k steps to compute the GCD of some pair, then the smaller number in the pair must be greater than or equal to the kth Fibonacci number.
If n is a prime number and a is any positive integer less than n, then a raised to the nth power is congruent to a modulo n.
1.9 Answer: The first process is recursive and the second one is iterative.
1.10 Answer: from the definition of (A x y), we have:
(A 1 10) = A(0 (A 1 9)) = 2 * (A 1 9) = ... = 2^9 * (A 1 1) = 2^10
(A 2 4) = 65536
(A 3 3) = 65536
(f n) computes 2n
(g n) computes 2^n
(h n) computes 2^2^2... (n times)
1.11 Answer: This is similar with the example of computing Fibonacci number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
1.12
1 2 3 4 5 6 |
|
1.15 Answer:
a. from the the procedure definition, suppose p will be called t times, we have:
a / (3^t) <= 0.1, which leads to:
log(10a) <= t, so t = ceiling (log(10a)) = 5, (the base of the log is 3)
b. both the space and number of steps depend on how many times p is called, so it's O(t) = O(log(a)).
1.16
1 2 3 4 5 6 7 8 9 10 11 12 |
|
1.17
1 2 3 4 5 6 7 |
|
1.18
1 2 3 4 5 6 7 8 9 10 11 |
|
1.19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
1.21
1 2 3 4 5 6 7 8 |
|
1.25 Answer:
Since Scheme has built-in support for arbitrary precision arithmetic, the procedure will produce the same result as the original expmod, however, it will be very inefficient since the huge number arithmetic will take much longer time than the numbers can be represented by a single computer word.
The original expmod used the successive squaring, the numbers to be processed will never be larger than m^2.
1.26 Answer:
With the calling of square, the original problem can be reduced to a sub problem with half of the size at each of the step when even? test is true. So T(n) = T(n/2) = O(logn)
However, if the explicit multiplication used instead, the recursive call of expmod will be evaluated twice, it not only just compute the sub problems two time, it is actually a tree recursion like the first solution for computing fibnacci sequence, so the number of expmod calls grow exponentially, which conclues that T(n) = O(2^n * logn) = O(n)
1.27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
1.28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
Why use Lisp for the book?
The most significant unique feature of Lisp is the fact that Lisp descriptions of processes, called procedures, can themselves be represented and manipulated as Lisp data. The importance of this is that there are powerful program-design techniques that rely on the ability to blur the traditional distinction between "passive" data and "active" processes.
The three mechanisms of combining simple ideas to form more complex ideas in any powerful programming languages:
Applicative order
Evaluate the operator and operands first and then applies the resulting procedure to the resulting arguments. "Fully expand and then reduce"
Normal order
Don't evaluate the operands until their values are needed, instead, substitute operand expressions for parameters until it obtained an expression involving only primitive operators, and would then perform the evaluation. "Evaluate the arguments and then apply"
The contrast between function and procedure is a reflection of the general distinction between describing properties of things and describing how to do things, or, as it is sometimes refered to, the distinction between declarative knowledge and imperative knowledge. In mathematics we are usually concerned with declarative (what is) descriptions, whereas in computer science we are usually concerned with imperative (how to) descriptions.
1.3 Define a procedure that takes three numbers as arguments and return the sum of squares of the two larger numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
1.4 Observe that our model of evaluation allows for combinations whose operators are compound expressions. Use this observation to describe the behavior of the following procedure:
1 2 |
|
if b is greater than 0, the operator that will apply to a and b is +, or else it will be -, so the a-plus-abs-b will always result in a plus abs of b.
1.5 Answer: the interpreter that uses applicative order evaluation will hang due to the infinite recursive call of 'p', while an interpreter that uses normal order evaluation will get 0.
Note: p is a function, (p) is a call to function p.
1.6 Answer: the interpreter will hang due to the infinite recursive call to sqrt-iter. Since List uses applicative order evaluation, in the definition of new-if, the else-clause will always be evaluated no matter the result of the predicate, thus lead to infinite recursive call to sqrt-iter. That's why if needs to be a special form, the predicate expression is evaluated first, and the result determines whether to evaluate the consequent or the alternative expression.
1.7 Answer: For small values, the absolute tolerance 0.001 is too large, so the results become inaccurate. For example, (sqrt 0.001) gives 0.04124542607499115 on my machine. (ubuntu 12.10 x86_64); And for large values, due to the precision limitation of float-point representation, the guess couldn't be refined to a value that can be represented within the tolerance. In such cases, the program can endlessly alternate between two guesses that are more than 0.001 away from the true square root.
so, instead of using absolute tolerance, we changed to use the relative tolerance of two continuous guess values. This can be demonstrated with the below updated good-enough? procedure:
1 2 |
|
1.8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
The only difference between cbrt and sqrt is the improve procedure.
First the excellent parts of the course:
As for the less-good things, I would say:
After taking this cousre, I got a deeper understanding of the functional programming basics and it made me feel more comfortable while picking up SICP again (after 3 years). Now, I am convinced that I am able to go through SICP and finish most of the exercises. Also, I had a firmer grasp of Scala even though I didn't write more than 100 lines of Scala before; I understand more about the scala syntax, idioms and even the motivations behind some of the language structures. E.g., call by name/value, lazy evaluation, currying, pattern matching and so on. I will publish my detailed notes on the lectures and assignments to this blog later.
To conclude, I really enjoyed taking the course and many thanks to Martin , the TAs, and also the coursera staff for offering such a wonderful course.
]]>This module provides the infrastructure for defining abstract base classes (ABCs) in Python. The ABCs define a minimal set of methods that establish the characteristic behavior of the type. For more details about this, see PEP 3119.
The module provides a metaclass used to create ABCs. An ABC can be subclassed directly. The class also has a 'register' method to register unrelated concrete classes (including built-in classes) and unrelated ABCs as 'virtual subclasses'
Also there are two decorators abstractmethod and abstractproperty, which will set the function object's attribute '__isabstractmethod__' to True. Only when all of the abstract methods and abstract properties are overriden, can a class that has a metaclass derived from ABCMeta be instantiated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
See the added comments in line
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
See the added comments in line
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
The code and comment in this function is very clear and straightforward.
Just make sure the different cases needed to check:
In this post, we only talk about the defitions of ABCMeta. We will see the typical usages in the collections module.
This module provides support for maintaining a list in sorted order without having to sort the list after each insertion. It uses a basic bisection algorithm similar with the classic binary search.
According to 'Programming Pearls' by Jon Bentley, It's very hard to write a correct binary search(Unbelievable, Uh?). Below is the note from the book:
I've assigned [binary search] in courses at Bell Labs and IBM. Professional programmers had a couple of hours to convert [its] description into a program in the language of their choice; a high-level pseudocode was fine. At the end of the speci?ed time, almost all the programmers reported that they had correct code for the task. We would then take thirty minutes to examine their code, which the programmers did with test cases. In several classes and with over a hundred programmers, the results varied little: ninety percent of the programmers found bugs in their programs (and I wasn't always convinced of the correctness of the code in which no bugs were found). I was amazed: given ample time, only about ten percent of professional programmers were able to get this small program right. But they aren't the only ones to ?nd this task difficult: in the history in Section 6.2.1 of his 'Sorting and Searching', Knuth points out that while the first binary search was published in 1946, the first published binary search without bugs did not appear until 1962.
Understanding how to use loop invariants in composing a program is very important, so let's first implement a binary search and prove the correctness utilizing this. Here we use a simplified specification compared to the bsearch function in the C standard library.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
It's easy to prove that the loop invariants are strong enough to imple the postcodition of the function(@ensures). Does this function terminate? In the loop body, we have lower < higher, so lower <= mid && mid < higher, then the intervals from lower to mid and from (mid+1) to higher are strictly smaller than the original interval (lower, higher), unless we find the element, the difference between higher and lower will eventually become 0 and we will exit the loop.
The bisect() functions provided in this module are useful for finding the insertion points but can be tricky or awkward to use for common searching tasks.
The above binsearch function will return the first of the elements that equals to the target if there are more than one. Sometimes it's required to find the left most one or the right most one. We can achieve this by using the bisect() functions. Let's examine the pre and post conditions, and also loop invariants.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
After exiting the loop, we have lo == hi. Now we have to distinguish some cases:
We can do the same analysis on the function bisect_left().
Note: In the pre condition I explicitly written down that lo < hi is required, the code will directly return lo when hi is less than or equal to lo, but that's simply meaningless, if this is the case, the sorted order of 'a' after we insert element before the index returned by the function.
In the doc of the module, it's bisect.bisect_right(a, x, lo=0, hi=len(a)) while in the source code, it's def bisect_left(a, x, lo=0, hi=None)
bisect.bisect_right(a, x, lo=0, hi=len(a)) is not valid python code, you will get error like this: NameError: name 'a' is not defined. This is because default values are computed and bound at the function definition time rather than when you call the function. This means that you can't have a default which is dependent on something that is not known until the function is called.
1 2 3 4 5 |
|
See here for more details.
As I installed the jekyll-rst plugin to use rst to write my posts, I thought it should be easy to write latex math because docutils has native support for it since version 0.8 (A :math: role and also a .. math: directive introduced for that). However, after I tried to use these in a octopress post, I found that the post will be rendered to empty. For example, if I insert the following rst code into my post, the whole post becomes empty; but after removing this line, everything is fine.
1
|
|
I also verified that the exact same code can be successfully converted to valid html using the ‘rst2html.py’ script on my system, so I guess maybe something is wrong in ‘RbST’. I found that in RbST, it has its own copies of rst2html and rst2latex tools under /gems/RbST-0.1.3/lib/rst2parts ,
1 2 3 4 |
|
which will be used in rbst.rb. I have even tried to change rbst.rb to use the rst2html.py installed on my system, but this also didn’t get any luck.
1 2 3 4 5 6 7 8 9 |
|
Finally, I gave up on this and opened an issue on this for the jekyll-rst plugin. Hope the author can fix this.
After a google search about this issue, seems that the simplest solution is to use kramdown, which has built-in support for latex.
First, install kramdown:
gem install karmdown
Then, add mathjax configs into
tag, in octopress, just add the below code into/source/_includes/custom/head.html
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
See here for more details. After this, we are ready to test latex math in our post. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
will render as
$$ \begin{align} & \phi(x,y) = \phi \left(\sum{i=1}^n x_ie_i, \sum{j=1}^n y_je_j \right) = \sum{i=1}^n \sum{j=1}^n x_i y_j \phi(e_i, e_j) = \ & (x_1, \ldots, x_n) \left( \begin{array}{ccc} \phi(e_1, e_1) & \cdots & \phi(e_1, e_n) \ \vdots & \ddots & \vdots \ \phi(e_n, e_1) & \cdots & \phi(e_n, e_n) \end{array} \right) \left( \begin{array}{c} y_1 \ \vdots \ y_n \end{array} \right) \end{align} $$
And for inline latex code, just use $\exp(-\frac{x^2}{2})$
, which will give
$\exp(-\frac{x2}{2})$.
The module provides an implementation of heap queue algorithm, also known as priority queue algorithm.
1 2 3 4 5 6 7 8 9 10 |
|
1 2 3 |
|
In python, there are 6 so called "Rich Comparison" methods, x < y calls x.__lt__(y) and others are similar (__le__ and <=; __gt__ and >=; __eq__ and ==; __ne__ and <>). Arguments to rich comparison methods are never coerced. see coercion
This is not obvious at first by seeing the code, given that there is a 'while' loop in _siftup and also a while loop in _siftdown(called in _siftup). Let's look into it further:
As explained in the comment by the module author, this is a ad hoc to reduce the comparisons on the following operations on the heap.
Also, alone the way, I may start another series on some specific 'advanced topics' in python, like descriptor, decorator, method resolution order(mro) and so on. Mainly about why they are introduced into python, how they are used and the typical use cases. This is inspired by the blogs about python history
This post will also be used to track my progress.
Since I am new to ruby, I just follow others' instructions to install ruby by rvm. My first attempt was:
1
|
|
This turned out to cause some troubles in the following steps to install ruby 1.9.2. I don't know the exact reason because I am not familiar with ruby. This link contains detailed information to install rvm. Once it's installed, installing octopress following the offical guide should be straightforward.
This is very simple in Octopress. I mainly followed this, one thing worth mentioning here is during the step to enter the url of your repository after typing
1
|
|
Here, the repo must use the username/username.github.com naming scheme, see here
As you may already know, I am a python fan. So I prefer rst to markdown. jekyll-rst is the plugin to add rst support to Octopress. After enabling the plugin, I changed default post ext in _confg.yml to rst. Code block in rst is quite handy to type.
1 2 3 4 |
|
So far, I have finished the basic settings of this blog. The changes include: