2011-02-26

emacs lisp: generate a ID

I want a command to insert random strings, as a ID. So i wrote one:

(defun insert-random-string ()
  "Insert a random alphanumerics string of length 6."
  (interactive)
  (let (mycharset (ii 0) )
    (setq mycharset ["1" "2" "3" "4" "5" "6" "7" "8" "9" "0" "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" ] )

    (while (< ii 6)
      (insert (elt mycharset (random (length mycharset))))
      (setq ii (1+ ii) ) ) ) )

O emacs! ♥

See also: Emacs Lisp Examples (Page 1)

vi, Emacs, Keybinding Design

Perm url with updates: http://xahlee.info/kbd/vi_emacs_keybinding_design.html

vi, Emacs, Keybinding Design

Xah Lee, 2011-02-24

fortunatus <daniel.elia...@excite.com> wrote:

You must be a Windows user. You must also not be an Emacs power user, because you think it's acceptable to use the arrow keys as cursors. If you don't, please use C-b, C-f, C-p, and C-n in place of the arrow keys. It dramatically improves speed.

Rafe Kettler <rafe.kett...@gmail.com> wrote:

Don't go down that path: "vi" has a way-better key binding for cursor movement!!

it should be noted, that vi's h j k l is not optimal. Better is

   i
 jkl 

in inverted T shape.

also, note that vi's Esc is FAST route to RSI. See: Left Wrist side-to-side Motion Pain; vi Esc key Syndrome.

also note, emacs keys and vi keys, are not out of much conscious design. Like unix tool bags, they are piled on over the years without much thinking. It was good enough, at the time. In fact, most things in life are like that. They are not anywhere close to optimal in any sense.

Daniel Weinreb, co-founder of Symbolics, alludes to why emacs keybinding is the way they are. Source

Xah wrote:

Emacs's default cursor moving shortcuts are 【Ctrl+f】, 【Ctrl+b】, 【Ctrl+n】, 【Ctrl+p】. The keys f, b, n, p are scattered around the keyboard and are not under the home row.

Daniel wrote:

That's true. At the time Guy Steele put together the Emacs default key mappings, many people in the target user community (about 20 people at MIT!) were already using these key bindings. It would have been hard to get the new Emacs bindings accepted by the community if they differed for such basic commands. As you point out, anyone using Emacs can very easily change this based on their own ergonomic preferences.

See:

This “design” by evolution applies to Keyboard hardware itself. As it is, it's the worst shit possible. It was good enough in the 1970s, where there are just a handful of programers in the world. And today, but vast majority of people (mom & pop, who occasionally chat online or write email), it's good enough! Even for most programers, who's finger actually dance on keyboard perhaps no more than accumulated 3 hours a day, it's good enough! But for data entry clerks, or programers who seriously type a lot or write docs all day, it's hello RSI. That's why we have so many problems on keybinding debates, radical input device designs, dvorak advocacy, and RSI is a serious medical problem. See: Keyboard Hardware Design Flaws.

this also applies to key layouts. e.g. we all know the story of qwerty and dvorak. But in my study, i found that it's just not that. Most international layout are ergonomic garbage. See:

also note, in the programing industry, if there is one software that induces most cases of RSI, it is emacs, by far. See: Celebrity Programers with RSI (Repetitive Strain Injury).

the emacs's keybinding, in my assessment, of all possible keybinding systems one could devise, with the PC keyboard as given constraint, i rate it near the bottom. Better than random assignment, but not much.

One thing damaging is that GNU Emacs has a tendency to refuse change, much like most unix-bag. Emacs's keybinding today is pretty much identical to emacs of 1970s. But, the landscape of computing has changed tremendously in past 30 years.

why most emacs people don't see this but in fact advocates emacs keybinding? My guess is that most people have not studied the issue. There are tens of thousands of things in life, we learned and use daily by habit, but never thought about it seriously. If you are interested, i think if you actually start to study keybinding, say, in the next 30 days, your job is to research keybinding design 8 hours a day for 30 days, i think you'll have a changed view. (no, i don't mean to brag about how many hundreds of software you've used in past n decades. Me too bro. I mean: stop dead and spend 8 hours a day for the next 30 days to do nothing but study keybindings. Yours truely have done so.)

Xah ∑ http://xahlee.org/

✻ ✻ ✻

This article is originally a post in “gnu.help.emacs” newsgroup. For previous 2 articles leading to this one, see:

What is Al Jazeera? and What's Al Hurra?

Perm url with updates: http://xahlee.org/Periodic_dosage_dir/what_is_Al_Jazeera.html

What is Al Jazeera?

Xah Lee, 2011-02-25

Al Jazeera logo

Discovered Al Jazeera. Recently on the news due to 2011 Egyptian revolution. Some selected quotes:

Al Jazeera is an international news network headquartered in Doha, Qatar. Initially launched as an Arabic news and current affairs satellite TV channel with the same name, Al Jazeera has since expanded into a network with several outlets, including the Internet and specialty TV channels in multiple languages. Al Jazeera is accessible in several world regions and is owned by Qatar Media Corporation.

The original Al Jazeera channel's willingness to broadcast dissenting views, for example on call-in shows, created controversies in the Arab states of the Persian Gulf. The station gained worldwide attention following the September 11, 2001 attacks, when it was the only channel to cover the war in Afghanistan live from its office there.

Where is Qatar?

Qatar is a tiny country in the Persian Gulf. (below Iran and Iraq, to the right of Saudi Arabia, above United Arab Emirates.) Qatar is an oil and gas-rich nation, with the third largest gas reserves It is the wealthiest country in the world, by GDP. Each household in Qatar, makes about twice more than USA. See: List of countries by GDP (PPP) per capita.

mid east map 2003-s

Mid East map.

Al Jazeera is Freedom of the Press?

Looks like it's freedom of the press of the Mid East. Quote:

Al Jazeera's availability (via satellite) throughout the Middle East changed the television landscape of the region. Prior to the arrival of Al Jazeera, many Middle Eastern citizens were unable to watch TV channels other than state-controlled national TV stations. Al Jazeera introduced a level of freedom of speech on TV that was previously unheard of in many of these countries. Al Jazeera presented controversial views regarding the governments of many Persian Gulf states, including Saudi Arabia, Kuwait, Bahrain and Qatar; it also presented controversial views about Syria's relationship with Lebanon, and the Egyptian judiciary. Critics accused Al Jazeera of sensationalism in order to increase its audience share. Al Jazeera's broadcasts have sometimes resulted in drastic action: for example, when, on 27 January 1999, critics of the Algerian government appeared on the channel's live program El-Itidjah el-Mouakass (“The Opposite Direction”), the Algerian government cut the electricity supply to large parts of the capital Algiers (and allegedly also to large parts of the country), to prevent the program from being seen. At that time, Al Jazeera was not yet generally known in the Western world, but where it was known, opinion was often favourable and Al Jazeera claimed to be the only politically independent television station in the Middle East. However, it was not until late 2001 that Al Jazeera achieved worldwide recognition, when it broadcast video statements by al-Qaeda leaders.

Attacks on and censorship of Al Jazeera

now, the juicy part.

On November 13, 2001, during the US invasion of Afghanistan, a U.S. missile strike destroyed Al Jazeera's office in Kabul. There were no casualties.

According to Glenn Greenwald, Al Jazeera is “constantly demonized in the American media.” In the run-up to the 2003 invasion of Iraq, the U.S. Pentagon hired the Rendon Group to target and possibly punish Al Jazeera reporters who did not stay on message. When Al Jazeera went on to do reporting featuring very graphic footage from inside Iraq, US officials decried Al Jazeera as anti-American and as inciting violence because it reported on controversial events.

Examples of censorship in the U.S. came shortly after the start of the invasion. On Monday, 24 March 2003, two Al Jazeera reporters covering the New York Stock Exchange (NYSE) had their credentials revoked. The New York Stock Exchange banned Al Jazeera (as well as several other news organizations whose identities were not revealed) from its trading floor indefinitely. NYSE spokesman Ray Pellechia claimed “security reasons” and that the exchange had decided to give access only to networks that focus “on responsible business coverage”. He denied the revocation has anything to do with the network's Iraq war coverage. However, Robert Zito, the exchange's executive vice president for communications, indicated that Al Jazeera's graphic footage broadcast on Sunday, 22 March 2003, led him to oust Al Jazeera. The move was quickly mirrored by Nasdaq stock market officials. The ban was rescinded a few months later.

In addition, Akamai Technologies, a U.S. company whose founder was killed in 9/11, canceled a contract to provide web services for Al-Jazeera’s English language website.

Death of Tareq Ayyoub

On 8 April 2003, Al Jazeera's office in Baghdad was hit by a U.S. missile, killing reporter Tareq Ayyoub and wounding another. Al Jazeera reports that it had mailed coordinates for their office to the U.S. State Department six weeks earlier and that these should have clearly identified their location. Dima Tareq Tahboub, the widow of Tareq Ayyoub, continues as of 2003[update] to denounce her husband's death and has among other things written for The Guardian and participated in a documentary broadcast on Al Jazeera English.

On 30 January 2005, the New York Times reported that the Qatari government, under pressure from the Bush administration, was speeding up plans to sell the station. However, as of 2011, the station/network has not been sold and it is unclear whether there are still any plans to do so.

Al Jazeera bombing memo

On 22 November 2005, the UK tabloid The Daily Mirror published a story claiming that it had obtained a leaked memo from 10 Downing Street saying that former U.S. President George W. Bush had considered bombing Al Jazeera's Doha headquarters in April 2004, when U.S. Marines were conducting a contentious assault on Fallujah.

In light of this allegation, Al Jazeera has questioned whether it has been targeted deliberately in the past — Al Jazeera's Kabul office was bombed in 2001 and another missile hit its office in Baghdad during the invasion of Iraq, killing correspondent Tareq Ayyoub. Both of these attacks occurred subsequent to Al Jazeera's alleged disclosure of the locations of their offices to the United States.

US Propaganda

US of A, countered with Al Hurra. This is your tax-dollars at work. Quote:

Alhurra is a United States-based satellite TV channel, sponsored by the U.S. government. It began broadcasting on February 14, 2004 in 22 countries across the Middle East. U.S. Government sources generally refer to the channel as Al-Hurra. Like all forms of U.S. public diplomacy, the station is forbidden from broadcasting within the U.S. itself under the 1948 Smith-Mundt Act concerning the broadcast of propaganda.

2011-02-25

Emacs Custom Keybinding to Enhance Productivity

Perm url with updates: http://xahlee.org/emacs/emacs_useful_user_keybinding.html

Emacs Custom Keybinding to Enhance Productivity

Xah Lee, 2005, ..., 2011-02-24

Here are some practical emacs keybinding suggestions to enhance productivity. If you don't know the basics of how to define keys, see: Emacs: How to Define Keyboard Shortcuts.

Insert Special Characters

You can create a shortcut to insert frequently used unicode characters. You can add few chars, or a systematic character set.

;; example of systematic keys to insert math symbols
(global-set-key (kbd "<kp-6>") "→") ; numeral keypad 6
(global-set-key (kbd "M-i a") "α") ; Alt+i a
(global-set-key (kbd "M-i b") "β")
(global-set-key (kbd "M-i t") "θ")
;; some Hyper keys to insert common unicode chars
(global-set-key (kbd "H-3") (lambda () (interactive) (insert "•"))) ; bullet
(global-set-key (kbd "H-4") (lambda () (interactive) (insert "◇"))) ; white diamond
(global-set-key (kbd "H-$") (lambda () (interactive) (insert "◆"))) ; black diamond
(global-set-key (kbd "H-5") (lambda () (interactive) (insert "†"))) ; dagger
(global-set-key (kbd "H-%") (lambda () (interactive) (insert "‡"))) ; double dagger
(global-set-key (kbd "H-6") (lambda () (interactive) (insert "▸"))) ; small right pointing triangle

If you do math a lot, you might try a more complete system. See: Emacs Unicode Math Symbols Input Mode (xmsi-mode).

See also: Emacs and Unicode Tips.

To define Hyper and Super key, see: Emacs: How to define Hyper & Super Keys.

Remap Frequently Used Shortcuts

;; make cursor movement keys under right hand's home-row.
(global-set-key (kbd "M-i") 'previous-line) ; was tab-to-tab-stop
(global-set-key (kbd "M-j") 'backward-char) ; was indent-new-comment-line
(global-set-key (kbd "M-k") 'next-line) ; was kill-sentence
(global-set-key (kbd "M-l") 'forward-char)  ; was downcase-word

(global-set-key (kbd "M-SPC") 'set-mark-command) ; was just-one-space
;; type parens in pairs with Hyper and right hands's home-row
(global-set-key (kbd "H-j") (lambda () (interactive) (insert "{}") (backward-char 1)))
(global-set-key (kbd "H-k") (lambda () (interactive) (insert "()") (backward-char 1)))
(global-set-key (kbd "H-l") (lambda () (interactive) (insert "[]") (backward-char 1)))

For a more systematic change, see: ErgoEmacs Keybinding.

Template Insertion

Define keys to insert text you frequently use. Header, footer, signature, copyright template, ….

(global-set-key (kbd "<f5>") 'insert-my-header)
(global-set-key (kbd "<f6>") 'insert-my-footer)
(global-set-key (kbd "<f7>") 'insert-signature)

;; example of defining a template insertion command
(defun 'insert-paragraph-tag ()
  "Insert <p></p> at cursor point."
  (interactive)
  (insert "<p></p>")
  (backward-char 4))

If you want elaborate template system for programing, you should use YASnippet instead. See: Emacs Templates with YASnippet.

Adding Web Browser Keys to Emacs

You can make your mouse's 4th and 5th button do backward and forward. See: Adding Web Browser Keys to Emacs.

Open Frequently Used Files

(global-set-key (kbd "<f5>") 'open-unicode-template)

(defun open-unicode-template ()
 "Open a file containing frequently used unicode chars"
 (interactive)
 (find-file "~/web/emacs/unicode.txt"))

I press 1 button on number pad to open ibuffer, bookmark, recently opened file list.

;; some number pad keys to open some files
(global-set-key (kbd "<kp-5>") 'recentf-open-files) ; numpad5
(global-set-key (kbd "<kp-7>") 'bookmark-bmenu-list)
(global-set-key (kbd "<kp-8>") 'ibuffer)

Here's some hotkey i use to open specific files i use few times a hour.

(global-set-key (kbd "<C-kp-9>") 'open-twitter) ; Ctrl+ numpad9
(global-set-key (kbd "<C-kp-6>") 'open-todo)

Note: depending on what OS and what emacs distro you are using, defining keys for the numeric keypad may not work.

You can see all my personal keybinding here: xah_emacs_keybinding.el (they change about weekly).

2011-02-23

bad advice programers give for keyboarding

Perm url with updates: http://xahlee.org/kbd/programer_keyboarding_advice.html

Bad Programer Keyboarding Advices (Typing, Ergonomics, RSI)

Xah Lee, 2011-02-23

Here are some points i think most programers are not aware of when discussing keybinding choices or keyboarding practices.

Important Variables in Keyboarding

• What keyboard you use matters a lot. e.g. full sized keyboard vs laptop keyboard. Even between full sized keyboard, there are major differences that effect good keybinding design or good keyboarding habit. e.g. standard PC keyboard vs split ergonomic ones vs radical ones such as Kinesis.

• There's major difference between those who touch type and those who don't. Good typing tips or keybinding design for one is usually not good for the other.

• Expected amount of typing per day matters a lot. Good advices for programers are not good for heavy duty data entry clerks (or writers and editors). Among programers, the actual amount of typing varies a lot too. Good advices or key choices for 3 hours per day typer is different for designs for those 6 hours per day.

Examples of Bad Advice

Some programer claim to have used certain habit for n decades and never have a problem. Sure. Maybe the actual time their fingers are pushing keys is just 4 hours per day. If they do data entry work, perhaps they'll have RSI within a week.

Some programer claim hunt'n'peck is best practice to avoid RSI. This is like saying the best way to avoid sport injury is not to go pro. Sure, you can hunt'n'peck as a programer and still be considered a fast coder, but don't think that is a good advice about typing ergonomics.

Some programer will claim that they do fine on normal straight pc keyboard and conclude that those curved split ergonomic keyboard are not useful or “depends”. Be careful here. This does not mean the ergonomic quality of keyboards amount to just personal preferences.

You can test many aspects of keyboarding yourself and for yourself, since scientific report usually seems remote and impersonal. You can conduct experiment to see which key choices are better among 2. Or, which of the 2 keyboard is more ergonomic. Or, whether swapping Ctrl and Caps Lock is better or Ctrl Alt. You just have to be careful in the experiment in eliminating bias, such as your habit, familiarity. This is especially important when you give out your advices to others.

2011-02-22

internet and the future

Perm url with updates: http://xahlee.org/js/old_internetnet_terms.html

Internet, Future, Old School Net Lingoes

Xah Lee, 2011-02-22

NY Times is reporting how South Korea is planning on gigabit internet, and how it, and Japan, and few other countries are way ahead of US.

The article is here: 〔http://www.nytimes.com/2011/02/22/technology/22iht-broadband22.html〕 (i despise NY Times that requires you to register. Fuck them & die.)

As tech gallops into the future, it is apparent that internet is the primary pillar of communication. In many respects, it's already more important than telephone networks, or radio, TV .

See also: Internet Users World Map.

Geek Reaction to New Media

For many of us older geeks, many of the internet phenomena were regarded as gimmicks and fads. I can remember the dot com days, where online stores first started (was called e-store or e-commerce), and it was experimental — nobody knew if they gonna stick. Then there's AOL flood (see: Eternal September), then there's instant messaging, blogs, and there's web based email apps (e.g. yahoo, Hotmail, gmail). Many of these, in the beginning several years, Tech Geeker's thinking on them is like “what has the world coming to, where idiotic teens had to use IM to chat about what they are eating, or writing embarrassing diaries in public”. And the web-based email is quite silly. I remember, when it began in late 1990s, i was thinking, there are tens of email apps. Why would people use web-based one? Sure the access-everywhere is a advantage, but there's laptops. Dedicated email app has much more features, much more convenient to use. Web-based email are awkward to use, with limited storage, and most importantly, you don't have privacy.

Then, in the last few years, there's twitter and facebook. My first reaction after having learned what twitter is, was: “another idiotic instant drivel system used by teen girls”. And for facebook, it was like: “another social shit”. But as we know, many of these techs have changed the world in significant ways, largely for the better. (e.g. blogs are key in several important political events in US in 2000s. Also witness recent Egyptian Revolution of 2011, and Wikileak.)

Here's some Wikipedia links:

Old School Net Lingoes

For the sake of nostalgia, here's some terms that have gone.

Cyberspace. In 1990s, this was the word for the brand new concept of the internet. Now this word is only used in sci-fi context (which is the word's origin). Now, people just say “online”.

Hyperlink. Now just “links”. The word “URL” is also dying in public.

• “e-mail”. Now it's just “email”. Also, email today basically mean one of web-based emails services: gmail, yahoo mail, hotmail. People don't know what dedicated Email Apps are. (e.g. the oldies are: Eudora, Outlook/Express, Netscape, AOL mail “you've got mail!”.) And, email addresses (e.g. “xah@xahlee.org”) that doesn't end in one of “gmail.com” or yahoo msn site, is becoming strange. Sometimes, people will ask you if that's correct. You hardy see these addresses anymore.

Snail mail. The term was popular for a few years in around 2000 i think. To contrast it to email. People don't say “snail mail” anymore. It's email or postal mail.

• e-store. It's now just online store. The word “e-commerce” is dead too. Internet IS Commerce.

• “brick-n-stone store”. Was popular around 2000. It refers to stores on the street, as opposed to online store.

It's interesting to know the change of terms because it gives us some understanding and predictive power about terms that are currently popular, such as SEO, Social Networking, Social Media, tweet, facebook, etc.

I'm sure, the evolution of terms and lingoes have been explored by specialized linguistics. But anyhow, here's some of my random thoughts.

The term “blog” certainly survived, at least for now. I think it's because ultimately “blog” has different connotation than online diary or online log.

Many other terms died because as internet becomes more popular and critical, the special term used for the online version is simply not necessary anymore. For example, commerce is just commerce, not e-commerce. Store is just a store, not e-store, though people might refer specifically to the online version, as online store, but more often today people simply say “their website”, “amazon site”.

I do not think SEO, tweet, facebook, or even social networking will survive. As internet becomes more daily routine, there's no need to distinguish the online version. SEO is search engine optimization, particularly today connected with Google Search. But in say 5 or 10 years, it is questionable “search engine” per se will still have a prominent role on the web. It might just be “ask it”, or “search it”, or “search it in library”, where the “library” here refers to the web. Likewise, tweet and facebook are probably ephemeral, just like Instant Messaging and Skype has been. For “social networking” … We human animals do networking thousands years ago. The reach and penetration of our networking is directly proportional to our comm tech. Before internet, we network by phone and contacts and among co-workers and in professional communities by conferences and meetings. Before industrial age, we network by sending postal mails. Before that, by traveling by foot, sometimes across continents, to meet someone.

Also, TV broadcasting might be dead in few years. Instead, we just have internet tech based broadcasting.

emacs rant or a glimpse of xah lee life 2010

my wanton emacs rant, sure to alienate supportors. Source groups.google.com

2011-02-21

emacs lisp: vectors & lists

Perm url with updates: http://xahlee.org/emacs/elisp_list_vector.html

Emacs Lisp Tutorial: List & Vector

Xah Lee, 2011-02-21

This page is a practical tutorial of Emacs Lisp's list and vector datatype. If you don't know elisp, first take a look at Emacs Lisp Basics.

Lisp has vector and list datatypes. These are similar to other language's list, vector, array.

Vectors

To create a vector, write it like this (vector a b …).

If you do not want the elements evaluated, write it like this: [a b …].

Creating a vector:

;; creating a vector
(setq v (vector 3 4 5)) ; each element will be evaluated
(setq v [3 4 5]) ; each element will NOT be evaluated

Length:

(length (vector 3 4 5))

Getting a Element:

use “elt”.

(elt (vector 3 4 5) 0) ; ⇒ 3. index starts at 0

Changing a Element:

use “aset”.

(setq v [3 4 5])
(aset v 0 "b")
v  ; ⇒ ["b" 4 5]

Nested Vector

;; nested vector
[[1 2] [3 4]] ; 2 by 2 matrix
[8 [3 [2 9] c] 7 [4 "b"]] ; random nested vector

Looping Thru Vector

One simple way to go thru a vector is using “mapcar”. Note that it returns a list, not vector.

;; add 1 to each
(mapcar '1+ [3 4 5] ) ; ⇒ (4 5 6)

;; get first element of each row
(mapcar (lambda (x) (elt x 0)) [[1 2] [3 4]] ) ; ⇒ (1 3)

You can also use “while”. Example:

(setq v [3 4 5])
(setq i 0)
(while (< i (length v))
  (insert (format "%i" (elt v i)))
  (setq i (1+ i))  ) ; inserts "345"

Join and Misc

You can join 2 vectors into a new vector by “vconcat”. You can convert a vector to list by “append”, e.g. (append myVector nil).

(info "(elisp) Vector Functions")

List

To create a list, write it like this (list a b …).

If you do not want the elements evaluated, write it like this: '(a b …).

; prints a list
(message "%S" '(a b c))

; assign a list to a var
(setq mylist '(a b c))

; create a list of values of variables
(let ((x 3) (y 4) (z 5))
  (message "%S" (list x y z))
) ; prints "(3 4 5)"
Some List Element Extraction Functions
FunctionPurpose
(car mylist)first element
(nth n mylist)nth element (start from 0)
(car (last mylist))last element
(cdr mylist)2nd to last elements
(nthcdr n mylist)nth to last elements
(butlast mylist n)without the last n elements

Here's some example of lists and element extraction.

(car (list "a" "b" "c") )   ; ⇒ "a"
(nth 1 (list "a" "b" "c") ) ; ⇒ "b"
(car (last (list "a" "b" "c")) )   ; ⇒ "c"

(cdr (list "a" "b" "c") )   ; ⇒ ("b" "c")
Basic List Functions
FunctionPurpose
(length mylist)number of elements
(cons x mylist)add x to front
(append mylist1 mylist2)join two lists

Examples:

(length (list "a" "b" "c") ) ; ⇒ 3

(cons "a" (list "c" "d") ) ; ⇒ ("a" "c" "d")
(cons (list "a" "b") (list "c" "d") ) ; ⇒ (("a" "b") "c" "d")

(append (list "a" "b") (list "c" "d") ) ; ⇒ ("a" "b" "c" "d")
Functions that modify a list variable
FunctionPurpose
(pop mylist)Remove first element from the variable. Returns the removed element.
(nbutlast mylist n)Remove last n elements from the variable. Returns the new value of the variable.
(setcar mylist x)replaces the first element in mylist with x. Returns x.
(setcdr mylist x)replaces the rest of elements in mylist with x. Returns x.

The weird names “car”, “cdr”, and “cons” are like that for historical reasons.

(info "(elisp) Lists")

Looping Thru a List

Here's a typical way of going thru a list. It is done with “mapcar”.

; add one to each list member
(mapcar (lambda (x) (+ x 1)) (list 1 2 3 4)) ; ⇒ (2 3 4 5)

; add one to each list member using the build in function 1+
(mapcar '1+ (list 1 2 3 4)) ; ⇒ (2 3 4 5)

; take the 1st element of each element in the list
(mapcar 'car  '((1 2) (3 4) (5 6))) ; ⇒ (1 3 5)
(mapcar 'car  '((1 2) (3 4) (5 6))) ; ⇒ (1 3 5)

; take the 2nd element of each element in the ilst
(mapcar (lambda (x) (nth 1 x))  '((1 2) (3 4) (5 6))) ; ⇒ (2 4 6)

; apply a file processing function to a list of files
(mapcar 'my-update-html-footer
        (list
"/home/vicky/web/3d/viz.html"
"/home/vicky/web/3d/viz2.html"
"/home/vicky/web/dinju/Khajuraho.html"
"/home/vicky/web/dinju/Khajuraho2.html"
"/home/vicky/web/dinju/Khajuraho3.html"
        )
)

The “lambda” above pretty much means “subroutine”. It essentially let you define a function in the middle of your code. The form is (lambda arguments body). For example, (lambda (x y) (+ x y)) would be a function that takes two arguments, x and y, and returns their sum.

Loop thru List with “while”

Another common form to loop thru a list is using the “while” function. In each iteration, “pop” is used to reduce the list. Here's a example of going thru a list using the “while” function.

(let (mylist)
  (setq mylist '(a b c))
  (while mylist
     (message "%s" (pop mylist))
     (sleep-for 1)
  )
)

Following is another example of using “while” to loop thru a list.

; pop head of mylist
; prepend it to mylist2
; resulting a reversed list
(let (mylist mylist2)
  (setq mylist '(a b c))
  (setq mylist2 '())
  (while mylist
    (setq mylist2
          (cons (pop mylist) mylist2)
    )
  )
  mylist2
)

First, use “let” to set a code block, with temporary variables “mylist” and “mylist2”. “mylist” is then set to '(a b c). “mylist2” is set to a empty list. Then, in the body of “while”, the (pop mylist) drops mylist's first element and returns it, the (cons (pop mylist) mylist2) creates a list with the new element prepended to “mylist2”. (Note: This code is to illustrate going thru a list. If you want to reverse a list, use the “reverse” function.)

List vs Vector

Lisp's “list” and “vector” datatypes are also called “sequences”. Many functions, such as “elt”, “mapcar”, work on any sequence type. Here's their primary differences:

  • vector has constant time access to any element.
  • It takes proportionally longer time to access a long list.
  • list's length can grow by pre-pending with “cons” and is fast. “vector”'s length cannot change.

You can nest list and vectors in any way. Example:

;; arbitrary nested list or vector
[ '(3 4) '(5 8) [4 2]]
(list [8 7] '(4 1))
(info "(elisp) Sequences Arrays Vectors")

direct vs indirect communication (Steven Pinker Talk)

Perm url with updates: http://xahlee.org/Periodic_dosage_dir/Language_and_Human_Nature.html

Direct vs Indirect Use of Language (Steven Pinker Talk)

Xah Lee, 2011-02-15

Language and Human Nature

A fantastic video that explains why we often do not talk directly what we mean, to friends, bosses, or peers. The video is just 10 minutes, by Steven Pinker (b1954), a specialist in psycholinguistics, at Harvard University.

“RSA Animate - Language as a Window into Human Nature”

Here's some kinda personal notes about the video. (won't make good sense if you haven't watched the video.)

We use indirect communication all the time. Between friends, on a date, in work place, in meetings... we leave open to others to pick it up and decided its meaning. There are many reasons why we do this. Often, being direct would be impolite, blunt, inappropriate, or even have adverse consequences with respect to our intention when compared to using indirect communication.

• language does 2 things. ① convey some content. ② negotiate a relationship type.

According to anthropologist Alan Fiske (UCLA), there are 3 major human relationships. ① Dominance. (e.g. boss, employer) ② communality. (family, close friends) ③ Reciprocity. (e.g. business relations)

Logic: Individual Knowledge vs Mutual Knowledge. When you know x, and i know x, that's individual knowledge. But when both of us know that the other also knows, that's what he calls “mutual knowledge”. The fact that when you have the info of the type “someone knows”, that can be critical, with major consequences in your decisions. (e.g.: game of Chicken, Poker, negotiation.)

Steven's presentation culminates in a ending that roughly summarized as this: we use indirect language because that avoids the “mutual knowledge” among the parties in the exchange. In other words, it leaves many doors open in the relationship, whatever that is or to become. (e.g. may it be veiled bribe offer, a sexual initiative, aggressive joke, etc. We can pretend nothing happens because we are not certain what's really going on.) If we use direct language, whatever is exchanged becomes “mutual knowledge”, and that sets in stone some intentions of the parties. (and this, without being said, is presumably not desired, because it make relationship awkward or hard to maintain, or possibly becoming hostile)

My Personal Reflection

Personally, i've always been the direct type of guy, and is known to be rude or inappropriate, and had plenty experiences of embarrassment or awkwardness. (sometimes as major stress: panic attack).

I am like that partly because of social ineptness by nature, but partly intentionally because i have always been deeply effected by my obsessive study of logic and math. I have always been striving to become a pure calculating machine sans emotions whatsoever. Part of this desire for direction communication is loosely tied to the concept of honesty, or today's political word “transparency”.

(For some period of my life i've been wishing to become a computer program as depicted in some sci-fi as in Mr Data, “The Schizoid Man” (StarTrek), or themes in Japanese animations e.g. “Ghost in the Shell”. amazon (for most of my life, i lived as a city hermit).)

Steven Pinker's presentation is certainly on the spot. I like to make some comment. Note that we have a assumption here that human relationships always falls (roughly) into 3 types: dominance, communal, reciprocal. Note that this relationship is not set in stone, is open to change. In some contexts, such as work place, bar, family, the type of relationship is often clearly suggested, if not already established. While, often other times, when you meet someone, the type of relationship is yet to be established. Also, a relationship can change, and often does. e.g. from stranger to lover to spouse. From stranger to coworker. From businessmen to friends.

After watching this video, in retrospect, i think i have always tried to impose a type relationship onto others, which explains that i have no qualms or regrets in my behavior of a direct communication style. For example, in workplace, my relationship with my superiors, from my mind, have always been absolutely clear to be of a reciprocal type, and never a dominance type. In my behaviors or use of language, i often (subconsciously) made this clear. To me, as a hired programer in a company, my stance of the relations between me and my “manager” have always been that of functional, sans any connotation of submissiveness. This demeanor is not conventional, and sometimes creates friction between me and my “superior”. That is, they perceive me to not have given them due respect.

In dating, i also often sets certain context by my demeanor. My philosophy on courtship and mating is that of mutual type.

Over all, Steven Pinker's presentation explains why we use indirect communication. But note that it EXPLAINS. It is not a survey of conformity, not a description of etiquette, not a advice. For most people in most situations, i think it's beneficial to use indirect communication at times. Though, what i want to mention is that it's not absolute.

For example, in the scenario in the video where a employee wonders whether to address his boss by name. If you are the type of strong philosophies (e.g. Richard Stallman, certain activitists, philosopher, leader), you might address him by his first name bluntly without hesitation. And if his response is negative, then you know that he's not fit to be your employor, and perhaps can plan to move on. In dating, if you bluntly ask a girl for sex, and from her response you can discern directly whether the girl is really the one that is compatible with your style. (presuming your goal is not just to have sex that night)

In summary, indirect communication is usually beneficial for negotiation, peddling, diplomacy, type of situations. While, if the parties involved are all direct types, geeks, it's much more efficient. One extreme is to think of machine-to-machine communication.

Ultimately, when 2 human animals communicate, in some sense there's a gaming going on, such as in math's game theory. You want to know who he is, what he wants, and he or she wonders about the same of you. If all human animals are by nature of certain ideologized peace-loving collaborative type, then none of this would happen. But because we are not selfless collaborative types by nature, thus was born indirect communication. It is a form of strategy of gaming.

2011-02-20

Chinese revolution à la Egypt?

Perm url with updates: http://xahlee.org/Periodic_dosage_dir/chinese_revolution_a_la_egypt.html

Chinese Revolution à la Egypt?

Xah Lee, 2011-02-20

It appears, there's some news report about how chinese people is calling for a revolution following the recent 2010–2011 Tunisian uprising and Egyptian Revolution of 2011.

After 20 min digging, i find this report most reliable:

China police break up ‘protests’ after online appeal (2011-02-20) @ Source www.bbc.co.uk

Quote:

Calls for people to protest and shout "we want food, we want work, we want housing, we want fairness", were circulated on Chinese microblog sites.

The message was first posted on a US-based Chinese-language website.

Several rights activists were detained beforehand and three people were arrested in Shanghai, but the call for mass protests was not well answered.

So, who started it? The “US-based Chinese-language website”, according from another site, seems to be “Boxun.com” (also boxun.us, peachhall.com). I spent 10 min but couldn't find any info about who they are. The Boxun.com is running on Google's blogger service. The site says it's a temp location due to cyber attack.

just found that they have a Wikipedia entry: Boxun. According to it, the site is started by Watson Meng. The article seems to be written by a single person. Checking on its edit history, the article appeared in 2007-01, and is edited by total about 7 people.

Here's another news report on this:

"Call me if there's a revolution" (2011-02-20) By Melissa Chan. @ Source blogs.aljazeera.net

Remember, today, there are tens of thousands of random newsites. Be sure you know who's behind the site.

2011-02-28

It appears, that the Boxun site is indeed a front group of Falung Gong. See: Falun Gong Front Groups.

emacs lisp: all about find & replace

Find & Replace is a central mechanism in text processing, especially in emacs. I took few hours to edit and re-organize several articles i've written that are all related to find & replace. Here's the re-organized index. It gives a much more clear view on what each article is about and how they relate to each other.

Using a Elisp Function for Dynamic Replacement String

Multi-Pair String Replacement

If you like them, please support the site. You can support by buying computer stuff from Amazon links here. USB drives, keyboard & mouse, iPad, mobile phones, laptops, DVDs, …. Or, you get get all my emacs and elisp tutorial (~300 articles) for just $5. (See bottom of Xah Emacs Tutorial.) Or, you can support the site by a donation. Any amount counts. Thanks!

You can also ask emacs questions here: Ask Emacs.