emacs - c auto insert
[dotfiles.git] / study-note.org
1 #+TITLE: Study Note
2 #+AUTHOR: Peng Li
3 #+EMAIL: seudut@gmail.com
4 #+DATE: 2016-08-18
5
6 * Programming Languages
7 ** Perl 
8 *** One line command
9 **** perl help
10 #+BEGIN_SRC sh :exports both :results output replace
11   perl --help
12 #+END_SRC
13
14 #+RESULTS:
15 #+begin_example
16
17 Usage: perl [switches] [--] [programfile] [arguments]
18   -0[octal]         specify record separator (\0, if no argument)
19   -a                autosplit mode with -n or -p (splits $_ into @F)
20   -C[number/list]   enables the listed Unicode features
21   -c                check syntax only (runs BEGIN and CHECK blocks)
22   -d[:debugger]     run program under debugger
23   -D[number/list]   set debugging flags (argument is a bit mask or alphabets)
24   -e program        one line of program (several -e's allowed, omit programfile)
25   -E program        like -e, but enables all optional features
26   -f                don't do $sitelib/sitecustomize.pl at startup
27   -F/pattern/       split() pattern for -a switch (//'s are optional)
28   -i[extension]     edit <> files in place (makes backup if extension supplied)
29   -Idirectory       specify @INC/#include directory (several -I's allowed)
30   -l[octal]         enable line ending processing, specifies line terminator
31   -[mM][-]module    execute "use/no module..." before executing program
32   -n                assume "while (<>) { ... }" loop around program
33   -p                assume loop like -n but print line also, like sed
34   -s                enable rudimentary parsing for switches after programfile
35   -S                look for programfile using PATH environment variable
36   -t                enable tainting warnings
37   -T                enable tainting checks
38   -u                dump core after parsing program
39   -U                allow unsafe operations
40   -v                print version, patchlevel and license
41   -V[:variable]     print configuration summary (or a single Config.pm variable)
42   -w                enable many useful warnings
43   -W                enable all warnings
44   -x[directory]     ignore text before #!perl line (optionally cd to directory)
45   -X                disable all warnings
46   
47 Run 'perldoc perl' for more help with Perl.
48
49 #+end_example
50
51 **** Used as =grep=
52 #+BEGIN_SRC sh
53   perl -ne 'print if /expression/' xxx.log;
54 #+END_SRC
55
56 #+BEGIN_SRC perl :results output replace
57   my $tt = "hello";
58   ($tt =~ /h(.*)o/ ) and print $1;
59 #+END_SRC
60
61 #+RESULTS:
62 : ell
63
64 Or 
65 #+BEGIN_SRC sh
66   perl -wnl -e '/keyword/ and print;' xxx.log
67 #+END_SRC
68
69 **** Used as =awk=
70 #+BEGIN_SRC sh
71   perl -ne 'print "$1\n" if /aaa=(\d+)\s.*/' xxx.log;
72 #+END_SRC
73
74 #+BEGIN_SRC fundamental :tangle ./temp/aa.txt
75   aa: 1
76   bb: 2
77   cc: 3
78   dd: 4
79 #+END_SRC
80
81 #+BEGIN_SRC sh :results output replace
82   perl -wnl -e 'print "==$.: $_"' ./temp/aa.txt
83 #+END_SRC
84
85 #+RESULTS:
86 : ==1: 
87 : ==2: aa: 1
88 : ==3: bb: 2
89 : ==4: cc: 3
90 : ==5: dd: 4
91
92 *** Special Variable
93 See special variable in perl [[http://perldoc.perl.org/perlvar.html][perlvar]], or =perldoc perlvar=
94 - =$.= Current line number
95
96 ** gnuplog
97
98 1. normal
99 #+BEGIN_SRC gnuplot :exports code :file ./temp/sin.png
100   reset
101   plot sin(x)/x
102 #+END_SRC
103
104 #+RESULTS:
105 [[file:./temp/sin.png]]
106
107 2. plot with data file
108 #+BEGIN_SRC fundamental :tangle ./temp/plot.dat 
109   # x y
110   1 11
111   2 12
112   3 13
113   4 14
114   5 15
115   6 16
116   7 17
117   8 18
118   9 19
119 #+END_SRC
120
121 #+BEGIN_SRC gnuplot :file ./temp/ll.png
122   reset
123   set xrange [0:10]
124   set yrange [0:20]
125   plot "./temp/plot.dat" using 1:2 with lines
126 #+END_SRC
127
128 #+RESULTS:
129 [[file:./temp/ll.png]]
130
131 * TCP/IP
132 ** netstat
133 #+BEGIN_SRC sh :results output replace
134   netstat -atn    # For tcp port
135   netstat -aun    # For udp port
136   netstat -atun   # both
137 #+END_SRC
138
139 ** nc (netcat)
140 #+BEGIN_SRC sh :results output replace
141   # listening on port 1234 for a connection
142   nc -l 1234
143
144   # connect to the machine and port
145   nc 127.0.0.1 1234
146 #+END_SRC
147
148 * Emacs
149 ** =add-hook= vs =eval-after-load=
150 Ref [[http://stackoverflow.com/questions/2736087/eval-after-load-vs-mode-hook][eval-after-load-vs-mode-hook]]
151 #+BEGIN_QUOTE
152 Code wrapped in =eval-after-load= will be executed only once, so it is typically used to perform
153 one-time setup such as setting default global values and behaviour. An example might be setting
154 up a default keymap for a particular mode. In =eval-after-load= code, there's no notion of the 
155 "current buffer".
156
157 Mode hooks exectue once for every buffer in which the mode is enabled, so they're used for per-buffer
158 configuration. Mode hooks are therefore run later than =eval-after-load= code; this lets them take 
159 actions baed upon such information as whether other modes are enabled in the current buffer.
160 #+END_QUOTE
161
162 * ObjectiveC
163 ** =#import= vs. =#include=
164 [[http://stackoverflow.com/questions/439662/what-is-the-difference-between-import-and-include-in-objective-c][what-is-the-difference-between-import-and-include-in-objective-c]]
165 - =#import= directive as added to Objective-C as an improvied version of =#include=
166 - =#import= ensures that a file is only ever included onces so that you never have a problem with recursive includes
167 - Bacically, use =#import= for Objective-C thins, =#include= for C stufff.
168
169
170 * Books
171 ** Intruduction to Algorithms 
172 *** Foundations
173 **** The Role of Algorithms in Computing
174 ** SICP (Structure and Interpretation of Computer Programs)
175 *** Building Abstractions with Procedures
176
177 The acts of the mind, wherein it exerts its power over simple ideas, are chiefly there threee: 1.
178 Combining serveral simple ideas into one compund one, and thus all complex ideas are made. 2. The
179 second is bringing two idears, whether simple or complex, together, and setting them by one another
180 so as to take a view of them at once, without uniting them into one, by which it gets all its ideas
181 of relations. 3. The third is separating them from all other ideas that accmpany them in there real
182 existence: this is called abstraction, and thus all its general ideas are made.
183
184 -- Jone Locke, An Essay Concerning Human Understanding (1690)
185
186 We are about to study the idea of computational process. Computational processes are abstract being
187 that inhabit computers. As they evolve, process manipulate other abstract things called data.
188 The evolution of a process in directed by a pattern of rules called a /programm/. People create
189 programs to direct processes. In effect, we conjure the spirits of the computer with our spells.
190
191     A computaional process is indeed much like a sorcerer's idea of a spirit. It cannot be seen or
192 touched. It is composed of matter at all. However, it is very real. It can perform intellectual work.
193 It can answer questions. It can affect the world by disbursing money at a bank or by controlling 
194 a rebot arm in a factory. The programs we use to conjure processes are like a sorcerer's spell.
195 They are carefully composed from symbolic expressions in arcane and esoteric /programming languages/
196 that prescribe the tasks we want our processes to perform
197
198 **** The Elements of Programming
199
200 When we describe a language, we should pay particular attentation to the means that the language
201 provides for combining simple ideas to form more complex ideas. Every powerful language has three
202 mechanisms for accomplishing this:
203
204 - *primitive expressions*, which represent the simplest entities the language is concerned with,
205 - *means of combination*, by which compound elements are built from simpler ones, and
206 - *means of abstraction*, by which compund elements can be names and manipulated as units.
207
208 In programming, we deal with two kinds of elements: /procedures/ and /data/. (Later we will discover
209 that they are really not so distinct.)
210
211 ***** Expressions
212
213 ***** Naming and the Environment
214
215 #+BEGIN_SRC scheme
216   (define size 2)
217 #+END_SRC
218
219 ***** Evaluating Combinations
220 To evaluate a combination, do the follow:
221 - Evaluate the subexpressions of the combination.
222 - Apply the procedure that is the value of the leftmost subexpression (the operator) to the
223   values of the other subexpressions (the operands).
224
225 ***** Compound Procedures
226
227 We have identified in Lisp some of the elements that must appear in any powerful programming
228 language:
229
230 - Numbers and arithmetic operations are primitive data and procedures.
231 - Nesting of combinations provides a means of combining operations..
232 - Definitions that associate names with values provide a limited means of abstraction.
233
234 The general form of a *procedure definition* is 
235 #+BEGIN_SRC scheme
236   (define (<name> <formal parameters>)
237     <body>)
238 #+END_SRC
239
240 ***** The Substitution Model for Procedure Application
241
242 To apply a compound procedure to arguments, evaluate the body of the procedure with each formal
243 parameter replaced by the corresponding argument.
244
245 Applicative order versus normal order
246
247 ***** Conditional Expressions and Predicates
248  
249 =cond= , which stands for "conditional"
250 #+BEGIN_SRC scheme
251   (cond (<p1> <e1>)
252         (<p2> <e1>)
253         ...
254         (<pn> <en>))
255 #+END_SRC
256
257 consisting of the symbol =cond= followed by parenthesized pais of expressions =(<p> <e>)=
258 called /clauses/. The first expression in each pair is a /predicate/-that is, and expression
259 whose value is interpreted as either true or false.
260
261 =else=, is a special symbol that can be used in place of =<p>= in the final clause of a =cond=
262 #+BEGIN_SRC scheme
263   (define (abs x)
264     (cond ((<x 0) (-x))
265           (else x)))
266 #+END_SRC
267
268 =if=
269 #+BEGIN_SRC scheme
270   (if <predicate> <consequent> <alternative>)
271 #+END_SRC
272
273 - =and=, =or=, =not=
274 #+BEGIN_SRC scheme
275   (and <e1> ... <en>)
276
277   (or <e1> ... <en>)
278
279   (not <e>)
280 #+END_SRC
281
282 ****** Exercise
283 - 1.2
284 #+BEGIN_SRC scheme
285   (/ (+ 5
286         4
287         (- 2
288            (- 3
289               (+ 6
290                  (/ 4 5)))))
291      (* 3
292         (- 6 2)
293         (- 2 7)))
294 #+END_SRC
295
296 - 1.3
297 #+BEGIN_SRC racket
298   (define (sum-two-larger a b c)
299     (cond ((and (> a b) (> c b)) (+ a c))
300           ((and (> c a) (> b a)) (+ c b))
301           ((and (> a c) (> b c)) (+ a b))))
302   ;; test
303   (sum-two-larger 1 2 3)
304   (sum-two-larger 7 3 5)
305
306   ;; another way
307   (define (minimal-of-two a b)
308     (if (> a b)
309         b
310         a))
311
312   (define (sum-two-larger-2 a b c)
313     (-  (+ a b c) (minimal-of-two a (minimal-of-two b c))))
314
315   (sum-two-larger-2 1 2 3)
316 #+END_SRC
317
318 ***** Example: Square Roots by Newton's Method
319
320 ***** Procedures as Black-Box Abstractions
321
322 - Local Name
323
324 - Internal definitions and block structure
325
326
327 **** Procedures and the Processes They Generate   
328
329 ***** 1.2.1 Linear Recursion and Iteration
330 sample, factorial function of n!
331
332 ***** 1.2.2 Tree Recursion
333 Fibonacci number
334
335 ***** 1.2.3 Orders of Grouth
336
337 ***** 1.2.4 Exponentiation
338 b^3
339
340 ***** 1.2.5. Greatest Common Divisior
341
342 ***** 1.2.6 Example: Testing for Primality
343 - Fermat test
344 Fermat's Little Theorem:
345
346 **** Formulating Abstractions with Higher-Order Procedures
347
348 Procedures that manipulate procedures are called /higher-order procedures/. 
349
350 ***** 1.3.1 Procedures as Arguments
351 #+BEGIN_SRC scheme
352   (define (inc n) (+ n 1))
353   (define (cube a) (* a a a))
354   (define (sum-cubes a b)
355     (+ cube a inc b))
356
357   (sum-cubes 1 10)
358 #+END_SRC
359
360 ***** 1.3.2 Constructing Procedures Using lambda
361 #+BEGIN_SRC scheme
362   (lambda (<formal-parameters>) <body>)
363 #+END_SRC
364
365 In general, =lambda= is used to create procedures in the same way as =define= except that no
366 name is specified for the procedure
367
368 Like any expression that has a procedure as its values. a =lambda= expression can be used as 
369 the operator in a combination such as
370 #+BEGIN_SRC scheme
371   ((lambda (x y z) (+ x y (square z)))
372    1 2 3)
373
374   12
375 #+END_SRC
376
377 Useing =let= to create local variable
378 #+BEGIN_SRC scheme
379   (let ((<var1> <exp1>)
380         (<var2> <exp2>)
381         ...
382         (<varn> <expn>))
383     <body>)
384 #+END_SRC
385
386 ***** 1.3.3 Procedures as General Methods
387
388 ***** 1.3.4 Procedures as Returned Values
389 Sample
390 #+BEGIN_SRC scheme
391   (define (average-damp f)
392     (lambda (x) (average x (f x))))
393
394   ((average-damp square) 10)
395   55
396 #+END_SRC
397
398 *** Building Abstractions with Data
399 #+BEGIN_SRC scheme
400   (define a 3)
401   a
402 #+END_SRC
403
404 #+RESULTS:
405 : 3
406