<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>AquarLife &#187; AquarHEAD</title>
	<atom:link href="http://www.aquarhead.com/author/admin/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.aquarhead.com</link>
	<description>天空中的羽翼距离你还有多远 在你隐藏的感情之中寻找我的踪迹</description>
	<lastBuildDate>Wed, 01 Feb 2012 17:08:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/><cloud domain='www.aquarhead.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Sequel Pro连接Vagrant中的MySQL</title>
		<link>http://www.aquarhead.com/2011/12/sequel-pro-connect-mysql-in-vagrant/</link>
		<comments>http://www.aquarhead.com/2011/12/sequel-pro-connect-mysql-in-vagrant/#comments</comments>
		<pubDate>Mon, 12 Dec 2011 10:24:20 +0000</pubDate>
		<dc:creator>AquarHEAD</dc:creator>
				<category><![CDATA[Force Research]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Sequel Pro]]></category>
		<category><![CDATA[Vagrant]]></category>

		<guid isPermaLink="false">http://www.aquarhead.com/?p=1103</guid>
		<description><![CDATA[Sequel Pro还真是傲娇, 用Vagrant的端口映射不好使, 研究了一番之后用SSH连接成功. 暂记一下, 然后有时间整理整理再写一篇(一系列?)关于Vagrant, Puppet以及其他一些Python/Django的工具的笔记. 参考资料 首先在Vagrantfile中配置安装了MySQL的虚拟机的IP,例如: config.vm.network&#40;&#34;33.33.33.10&#34;&#41; 重启虚拟机之后在/home/vagrant/.ssh/authorized_keys后面加上自己的公钥~/.ssh/id_rsa.pub 最后在Sequel Pro连接时选择SSH方式 MySQL的Host填127.0.0.1, 其他参数正常填, SSH Host填上面的IP, SSH User填vagrant, SSH Port填22, 密码空着, 连接的时候问密码的话直接OK就行. Tags:MySQL,Sequel Pro,Vagrant]]></description>
			<content:encoded><![CDATA[<p>Sequel Pro还真是傲娇, 用Vagrant的端口映射不好使, 研究了一番之后用SSH连接成功.<br />
暂记一下, 然后有时间整理整理再写一篇(一系列?)关于Vagrant, Puppet以及其他一些Python/Django的工具的笔记.<br />
<a href="http://stevelosh.com/blog/2011/06/django-advice/" title="Django Advice">参考资料</a></p>
<p>首先在Vagrantfile中配置安装了MySQL的虚拟机的IP,例如:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  config.<span style="color:#9900CC;">vm</span>.<span style="color:#9900CC;">network</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;33.33.33.10&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>重启虚拟机之后在/home/vagrant/.ssh/authorized_keys后面加上自己的公钥~/.ssh/id_rsa.pub<br />
最后在Sequel Pro连接时选择SSH方式<br />
MySQL的Host填127.0.0.1, 其他参数正常填, SSH Host填上面的IP, SSH User填vagrant, SSH Port填22, 密码空着,<br />
连接的时候问密码的话直接OK就行.</p>

	Tags:<a href="http://www.aquarhead.com/tag/mysql/" title="MySQL" rel="tag">MySQL</a>,<a href="http://www.aquarhead.com/tag/sequel-pro/" title="Sequel Pro" rel="tag">Sequel Pro</a>,<a href="http://www.aquarhead.com/tag/vagrant/" title="Vagrant" rel="tag">Vagrant</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.aquarhead.com/2011/12/sequel-pro-connect-mysql-in-vagrant/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Django项目自动dotcloud部署&amp;本地测试</title>
		<link>http://www.aquarhead.com/2011/12/django-auto-deploy-on-dotcloud-with-local-test-enable/</link>
		<comments>http://www.aquarhead.com/2011/12/django-auto-deploy-on-dotcloud-with-local-test-enable/#comments</comments>
		<pubDate>Sat, 03 Dec 2011 19:50:50 +0000</pubDate>
		<dc:creator>AquarHEAD</dc:creator>
				<category><![CDATA[Code Fortress]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[dotCloud]]></category>
		<category><![CDATA[local]]></category>
		<category><![CDATA[timepaw]]></category>
		<category><![CDATA[部署]]></category>

		<guid isPermaLink="false">http://www.aquarhead.com/?p=1077</guid>
		<description><![CDATA[现时循环的, C.C.的角色歌, 略带魔幻色彩 然后再吐槽一句, 为啥IMKQIM用一次shift切换中英文之后就自动变成中文符号了啊... 已经无语&#038;泪目N久了... 其实在dali'an接近0.1的时候就已经用makefile实现了dotcloud的自动部署, 这几天做测试的时候也都放到网上去弄了, 不过dotcloud确实是有些慢的, 如果调试前端的话在等待刷新上频繁地浪费时间也没意义, 所以今天又改进了一下, 做到本地和dotcloud上同时可用. 写在最前面的一点是, 项目传到dotcloud上之后是在/home/dotcloud/current目录下面, 而且项目的目录没有加载到PATH里面, 所以呢, 比如你有个app1, 那么你最好在import的时候写成 from project_name.app1.xxx import xxx, 当然你也可以用os提供的功能在wsgi.py里面手动添加项目目录到PATH里面, 不过我比较喜欢前面一种方法 :D 接下来为settings.py和urls.py添加针对dotcloud的文件, 比如settings_server.py和urls_server.py. 在settings_server.py中可以用 1 2 with open&#40;'/home/dotcloud/environment.json'&#41; as f: dotcloud_env = json.load&#40;f&#41; 这样把一起配置的数据库等信息加载到dotcloud_env里面, 然后就可以像下面这样修改数据库的相关配置: 1 2 3 4 5 6 7 8 9 10 DATABASES = &#123; 'default': &#123; 'ENGINE': [...]]]></description>
			<content:encoded><![CDATA[<p>现时循环的, C.C.的角色歌, 略带魔幻色彩<br />
<script type="text/javascript" src="http://www.xiami.com/widget/player-single?uid=0&#038;sid=1769149998&#038;mode=js"></script></p>
<p>然后再吐槽一句, 为啥IMKQIM用一次shift切换中英文之后就自动变成中文符号了啊... 已经无语&#038;泪目N久了...</p>
<p>其实在dali'an接近0.1的时候就已经用makefile实现了dotcloud的自动部署, 这几天做测试的时候也都放到网上去弄了, 不过dotcloud确实是有些慢的, 如果调试前端的话在等待刷新上频繁地浪费时间也没意义, 所以今天又改进了一下, 做到本地和dotcloud上同时可用.</p>
<p>写在最前面的一点是, 项目传到dotcloud上之后是在/home/dotcloud/current目录下面, 而且项目的目录没有加载到PATH里面, 所以呢, 比如你有个app1, 那么你最好在import的时候写成 from project_name.app1.xxx import xxx, 当然你也可以用os提供的功能在wsgi.py里面手动添加项目目录到PATH里面, 不过我比较喜欢前面一种方法 :D</p>
<p>接下来为settings.py和urls.py添加针对dotcloud的文件, 比如settings_server.py和urls_server.py.<br />
在settings_server.py中可以用</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">with</span> <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/home/dotcloud/environment.json'</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> f:
    dotcloud_env = json.<span style="color: black;">load</span><span style="color: black;">&#40;</span>f<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>这样把一起配置的数据库等信息加载到dotcloud_env里面, 然后就可以像下面这样修改数据库的相关配置:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">DATABASES = <span style="color: black;">&#123;</span>
    <span style="color: #483d8b;">'default'</span>: <span style="color: black;">&#123;</span>
        <span style="color: #483d8b;">'ENGINE'</span>: <span style="color: #483d8b;">'django.db.backends.postgresql_psycopg2'</span>,
        <span style="color: #483d8b;">'NAME'</span>: <span style="color: #483d8b;">'template1'</span>,
        <span style="color: #483d8b;">'USER'</span>: dotcloud_env<span style="color: black;">&#91;</span><span style="color: #483d8b;">'DOTCLOUD_DB_SQL_LOGIN'</span><span style="color: black;">&#93;</span>,
        <span style="color: #483d8b;">'PASSWORD'</span>: dotcloud_env<span style="color: black;">&#91;</span><span style="color: #483d8b;">'DOTCLOUD_DB_SQL_PASSWORD'</span><span style="color: black;">&#93;</span>,
        <span style="color: #483d8b;">'HOST'</span>: dotcloud_env<span style="color: black;">&#91;</span><span style="color: #483d8b;">'DOTCLOUD_DB_SQL_HOST'</span><span style="color: black;">&#93;</span>,
        <span style="color: #483d8b;">'PORT'</span>: <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>dotcloud_env<span style="color: black;">&#91;</span><span style="color: #483d8b;">'DOTCLOUD_DB_SQL_PORT'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>,
    <span style="color: black;">&#125;</span>
<span style="color: black;">&#125;</span></pre></td></tr></table></div>

<p>如果用mysql的话大致如下:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">DATABASES = <span style="color: black;">&#123;</span>
    <span style="color: #483d8b;">'default'</span>: <span style="color: black;">&#123;</span>
        <span style="color: #483d8b;">'ENGINE'</span>: <span style="color: #483d8b;">'django.db.backends.mysql'</span>,
        <span style="color: #483d8b;">'NAME'</span>: <span style="color: #483d8b;">'mysql'</span>, <span style="color: #808080; font-style: italic;">#这里我记不太清楚了, 应该是这个, 如果不行的话自己google下吧, 别忘了在下面留言 :D</span>
        <span style="color: #483d8b;">'USER'</span>: dotcloud_env<span style="color: black;">&#91;</span><span style="color: #483d8b;">'DOTCLOUD_DB_MYSQL_LOGIN'</span><span style="color: black;">&#93;</span>,
        <span style="color: #483d8b;">'PASSWORD'</span>: dotcloud_env<span style="color: black;">&#91;</span><span style="color: #483d8b;">'DOTCLOUD_DB_MYSQL_PASSWORD'</span><span style="color: black;">&#93;</span>,
        <span style="color: #483d8b;">'HOST'</span>: dotcloud_env<span style="color: black;">&#91;</span><span style="color: #483d8b;">'DOTCLOUD_DB_MYSQL_HOST'</span><span style="color: black;">&#93;</span>,
        <span style="color: #483d8b;">'PORT'</span>: <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>dotcloud_env<span style="color: black;">&#91;</span><span style="color: #483d8b;">'DOTCLOUD_DB_MYSQL_PORT'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>,
    <span style="color: black;">&#125;</span>
<span style="color: black;">&#125;</span></pre></td></tr></table></div>

<p>顺便, 我没用mysql是因为没找到方法改它的字符集设定 :(, postgresql倒是直接支持存中文, 接下来是有关静态文件的设置:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">MEDIA_ROOT = <span style="color: #483d8b;">'/home/dotcloud/data/media/'</span>
&nbsp;
MEDIA_URL = <span style="color: #483d8b;">'http://{ project_url }/media/'</span>
&nbsp;
STATIC_ROOT = <span style="color: #483d8b;">'/home/dotcloud/data/static/'</span> <span style="color: #808080; font-style: italic;"># 这个地址是用来存放通过collectstatic收集的静态文件</span>
&nbsp;
STATIC_URL = <span style="color: #483d8b;">'/static/'</span>
&nbsp;
STATICFILES_DIRS = <span style="color: black;">&#40;</span>
    <span style="color: #483d8b;">'/home/dotcloud/current/{ project_name }/static'</span>, <span style="color: #808080; font-style: italic;"># 总之是你各种静态文件的目录 也可以用下面模板目录的写法</span>
<span style="color: black;">&#41;</span>
&nbsp;
TEMPLATE_DIRS = <span style="color: black;">&#40;</span>
    <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">dirname</span><span style="color: black;">&#40;</span>__file__<span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'templates'</span><span style="color: black;">&#41;</span>,
<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>然后是urls_server.py其实这个文件只要把urls.py中本地测试服务器需要的两个服务静态文件的条目删掉就可以了, 嗯, 也就是说本地的测试服务器要支持静态文件的话只要加上:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">   url<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^static/(?P&lt;path&gt;.*)$'</span>, <span style="color: #483d8b;">'django.views.static.serve'</span>, <span style="color: black;">&#123;</span>
           <span style="color: #483d8b;">'document_root'</span>: settings.<span style="color: black;">STATIC_ROOT</span>,
       <span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>,
   url<span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^media/(?P&lt;path&gt;.*)$'</span>, <span style="color: #483d8b;">'django.views.static.serve'</span>, <span style="color: black;">&#123;</span>
            <span style="color: #483d8b;">'document_root'</span>: settings.<span style="color: black;">MEDIA_ROOT</span>,
        <span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>,</pre></td></tr></table></div>

<p>这两条就OK了.</p>
<p>除了这些, 如果你有些参数本地测试和dotcloud上需要用不同的, 那么就把它单独弄到一个文件里, 然后像上面一样做成两份加以区分.</p>
<p>接下来建立dotcloud_conf这个文件夹, 里面放上各种配置dotcloud需要的文件:<br />
dotcloud.yml (最基本的配置文件, 这个需要贴么...)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="" style="font-family:monospace;">www:
  type: python 
db:
  type: postgresql</pre></td></tr></table></div>

<p>requirements.txt (用来告诉服务器上的pip自己的project都需要哪些库, 列出pip install xxx中的xxx就OK了)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="" style="font-family:monospace;">Django
oauth2
feedparser
PIL
django-celery</pre></td></tr></table></div>

<p>nginx.conf (配置静态文件服务)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="" style="font-family:monospace;">location /media/ <span class="br0">&#123;</span> root /home/dotcloud/data ; <span class="br0">&#125;</span>
location /static/ <span class="br0">&#123;</span> root /home/dotcloud/data ; <span class="br0">&#125;</span></pre></td></tr></table></div>

<p>wsgi.py (apache的mod_wsgi调用的接口)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #dc143c;">os</span>.<span style="color: black;">environ</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'DJANGO_SETTINGS_MODULE'</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">'{ project_name }.settings'</span>
<span style="color: #ff7700;font-weight:bold;">import</span> django.<span style="color: black;">core</span>.<span style="color: black;">handlers</span>.<span style="color: black;">wsgi</span>
application = django.<span style="color: black;">core</span>.<span style="color: black;">handlers</span>.<span style="color: black;">wsgi</span>.<span style="color: black;">WSGIHandler</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>postinstall (安装完成后自动执行的一些命令)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
python <span style="color: #7a0874; font-weight: bold;">&#123;</span> project_name <span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #000000; font-weight: bold;">/</span>manage.py syncdb <span style="color: #660033;">--noinput</span>
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>dotcloud<span style="color: #000000; font-weight: bold;">/</span>data<span style="color: #000000; font-weight: bold;">/</span>media
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>dotcloud<span style="color: #000000; font-weight: bold;">/</span>data<span style="color: #000000; font-weight: bold;">/</span>static
python <span style="color: #7a0874; font-weight: bold;">&#123;</span> project_name <span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #000000; font-weight: bold;">/</span>manage.py collectstatic <span style="color: #660033;">--noinput</span></pre></td></tr></table></div>

<p>最后是真正来搞这个自动部署的makefile啦(隐身属性终于解除了么...)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="" style="font-family:monospace;">deploy: settings_server.py
	mkdir ../dotcloud_tmp
	mkdir ../dotcloud_tmp/<span class="br0">&#123;</span> project_name <span class="br0">&#125;</span>
	cp -a . ../dotcloud_tmp/<span class="br0">&#123;</span> project_name <span class="br0">&#125;</span>
	mv ../dotcloud_tmp/ .
	mv dotcloud_tmp/<span class="br0">&#123;</span> project_name <span class="br0">&#125;</span>/dotcloud_conf/* dotcloud_tmp/
	rm -f dotcloud_tmp/<span class="br0">&#123;</span> project_name <span class="br0">&#125;</span>/settings.py
	rm -f dotcloud_tmp/<span class="br0">&#123;</span> project_name <span class="br0">&#125;</span>/urls.py
	mv dotcloud_tmp/<span class="br0">&#123;</span> project_name <span class="br0">&#125;</span>/settings_server.py dotcloud_tmp/<span class="br0">&#123;</span> project_name <span class="br0">&#125;</span>/settings.py
	mv dotcloud_tmp/<span class="br0">&#123;</span> project_name <span class="br0">&#125;</span>/urls_server.py dotcloud_tmp/<span class="br0">&#123;</span> project_name <span class="br0">&#125;</span>/urls.py
	dotcloud push <span class="br0">&#123;</span> project_name <span class="br0">&#125;</span> dotcloud_tmp
	rm -rf dotcloud_tmp</pre></td></tr></table></div>

<p>如果dotcloud push的时候出问题跳出的话(比如无法连接等等), 就手动执行一下最后一句吧, 然后如果有其他的本地/dotcloud不同的配置文件就模仿一下7-10行的内容吧...</p>
<p>吐槽: 喂, 那些大大的{ project_xxx }知道改的吧...</p>
<p>搞完这些之后就可以在本地用python manage.py runserver来做测试, 然后一个make就直接部署到dotcloud上去了 :D</p>
<p>_: dotcloud君V587不解释啊.... 咱服务器啥时候能写到一个make自动搞完就好了... 泪目<br />
__: 整个鲁路修里面最喜欢的其实是R2结局的那首<a href="http://www.xiami.com/song/3492858" title="Continued Story" target="_blank">Continued Story</a>, 最后一集看了好几遍就为了这歌...<br />
___: 最近做这个项目其实学了很多东西, 但是反倒找不到地方去吐槽了...</p>
<p>各种吐槽完毕, 以上</p>

	Tags:<a href="http://www.aquarhead.com/tag/django/" title="Django" rel="tag">Django</a>,<a href="http://www.aquarhead.com/tag/dotcloud/" title="dotCloud" rel="tag">dotCloud</a>,<a href="http://www.aquarhead.com/tag/local/" title="local" rel="tag">local</a>,<a href="http://www.aquarhead.com/tag/timepaw/" title="timepaw" rel="tag">timepaw</a>,<a href="http://www.aquarhead.com/tag/%e9%83%a8%e7%bd%b2/" title="部署" rel="tag">部署</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.aquarhead.com/2011/12/django-auto-deploy-on-dotcloud-with-local-test-enable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>达利安(dalian)v0.1发布日志</title>
		<link>http://www.aquarhead.com/2011/11/dalian-v01-release-notes/</link>
		<comments>http://www.aquarhead.com/2011/11/dalian-v01-release-notes/#comments</comments>
		<pubDate>Sat, 26 Nov 2011 08:15:30 +0000</pubDate>
		<dc:creator>AquarHEAD</dc:creator>
				<category><![CDATA[Code Fortress]]></category>
		<category><![CDATA[dalian]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[dotCloud]]></category>

		<guid isPermaLink="false">http://www.aquarhead.com/?p=1072</guid>
		<description><![CDATA[前前后后的鼓捣了一个月时间(参见https://github.com/AquarHEAD/dalian/commits/master, 难道我只有六天在写代码么...) 总之忍受着渣CSS和渣网络等等各种因素终于搞出了个像样的版本, 原本只是想用来替换各个浏览器里面万年不变的google.com 进一步就打算整合进以前t.aquarhead.com的吐槽功能和笔记/源整合系统(虽然暂时还没做) 嗯, 好像没啥可以继续吐槽的了, 那就上地址吧, 里面有安装说明的嗯嗯... https://github.com/AquarHEAD/dalian 噢, 话说里面的各种名字来自丹特丽安的书架和Steins;Gate. 以上... Tags:dalian,Django,dotCloud]]></description>
			<content:encoded><![CDATA[<p>前前后后的鼓捣了一个月时间(参见https://github.com/AquarHEAD/dalian/commits/master, 难道我只有六天在写代码么...)<br />
总之忍受着渣CSS和渣网络等等各种因素终于搞出了个像样的版本, 原本只是想用来替换各个浏览器里面万年不变的google.com<br />
进一步就打算整合进以前t.aquarhead.com的吐槽功能和笔记/源整合系统(虽然暂时还没做)</p>
<p>嗯, 好像没啥可以继续吐槽的了, 那就上地址吧, 里面有安装说明的嗯嗯...<br />
<a href="https://github.com/AquarHEAD/dalian">https://github.com/AquarHEAD/dalian</a></p>
<p>噢, 话说里面的各种名字来自丹特丽安的书架和Steins;Gate.</p>
<p>以上...</p>

	Tags:<a href="http://www.aquarhead.com/tag/dalian/" title="dalian" rel="tag">dalian</a>,<a href="http://www.aquarhead.com/tag/django/" title="Django" rel="tag">Django</a>,<a href="http://www.aquarhead.com/tag/dotcloud/" title="dotCloud" rel="tag">dotCloud</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.aquarhead.com/2011/11/dalian-v01-release-notes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>关于几个动漫和Timepaw的碎碎念</title>
		<link>http://www.aquarhead.com/2011/09/thoughts-about-anime-and-timepaw/</link>
		<comments>http://www.aquarhead.com/2011/09/thoughts-about-anime-and-timepaw/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 21:12:52 +0000</pubDate>
		<dc:creator>AquarHEAD</dc:creator>
				<category><![CDATA[Innocence Life]]></category>
		<category><![CDATA[Code Geass]]></category>
		<category><![CDATA[eternal feather]]></category>
		<category><![CDATA[Last Exile]]></category>
		<category><![CDATA[timepaw]]></category>
		<category><![CDATA[动漫]]></category>
		<category><![CDATA[反叛的鲁路修]]></category>
		<category><![CDATA[悠久之翼]]></category>
		<category><![CDATA[空之境界]]></category>

		<guid isPermaLink="false">http://www.aquarhead.com/?p=1060</guid>
		<description><![CDATA[最近看了不少动漫, 起先是无聊了逛VeryCD开始追, 然后偶然在虾米上听到了Over the sky, 于是下了看, 完了就一发不可收拾地看完了, 还有... 那就来吐槽一下吧, 其实我觉得最终流放不算烂尾吧... 不过最后几集确实蛮纠结的, 没有前面精彩. 结局也还算可以啦 给我的感觉总是很黑暗, 而且真的是得放在一起看才有前因后果的, 应该说整个8集剧场版放在一起的话水准是非常高的, 个人最喜欢第七集, 其次是第五集, 七近似于完结性质的, 五则是完全的刺激了. 鲁路修确实跟死亡笔记蛮像的, 不过鲁路修的目的比月明确, 手段也很强硬, 但感觉第一季后面的几集跟死亡笔记后面的部分一样, 虽然想知道剧情的发展, 不过感觉并不怎么喜欢, 而第二季要好的多, 敢死的人才可以杀人, 鲁路修能把全世界的仇恨集中在自己身上, 然后再导演一场刺杀来释放, 这个结局要比死亡笔记深刻的多, 虽然最后貌似还是没死跟CC私奔去了 :-( ef哎... 真心催泪, 虽然我确实没让它流出来... 实话说不太喜欢第一季里面莲治和千寻的分支, 另一条线还行, 其实感觉第一季蛮平凡的, 但第二季的确拿出了更高的水准, 虽然优子的复仇有些黑暗, 修一的固执有些发狂之外, 能感觉出第二季的感情更加细腻, 结局也不是传统意义上完全的happy ending, 不过也蛮感动的... 吐槽完了动漫, 再吐槽一下先锋学长带我做的项目吧 之前由于各种原因本来就没做多少, 结果MBP的硬盘线gg了, 硬盘貌似也有问题, 索性都换了... 这几天鼓捣好Lion之后开始重写代码, 已经初步实现了用户注册/登陆, 添加数据源(暂时只有新浪微薄)和手动获取数据的功能, [...]]]></description>
			<content:encoded><![CDATA[<p>最近看了不少动漫, 起先是无聊了逛VeryCD开始追<丹特丽安的书架>, 然后偶然在虾米上听到了Over the sky, 于是下了<Last Exile>看, 完了就一发不可收拾地看完了<空之境界>, <反叛的鲁路修>还有<悠久之翼>...</p>
<p>那就来吐槽一下吧, 其实我觉得最终流放不算烂尾吧... 不过最后几集确实蛮纠结的, 没有前面精彩. 结局也还算可以啦</p>
<p><空之境界>给我的感觉总是很黑暗, 而且真的是得放在一起看才有前因后果的, 应该说整个8集剧场版放在一起的话水准是非常高的, 个人最喜欢第七集, 其次是第五集, 七近似于完结性质的, 五则是完全的刺激了.</p>
<p>鲁路修确实跟死亡笔记蛮像的, 不过鲁路修的目的比月明确, 手段也很强硬, 但感觉第一季后面的几集跟死亡笔记后面的部分一样, 虽然想知道剧情的发展, 不过感觉并不怎么喜欢, 而第二季要好的多, 敢死的人才可以杀人, 鲁路修能把全世界的仇恨集中在自己身上, 然后再导演一场刺杀来释放, 这个结局要比死亡笔记深刻的多, 虽然最后貌似还是没死跟CC私奔去了 :-(</p>
<p>ef哎... 真心催泪, 虽然我确实没让它流出来... 实话说不太喜欢第一季里面莲治和千寻的分支, 另一条线还行, 其实感觉第一季蛮平凡的, 但第二季的确拿出了更高的水准, 虽然优子的复仇有些黑暗, 修一的固执有些发狂之外, 能感觉出第二季的感情更加细腻, 结局也不是传统意义上完全的happy ending, 不过也蛮感动的...</p>
<p>吐槽完了动漫, 再吐槽一下先锋学长带我做的项目吧</p>
<p>之前由于各种原因本来就没做多少, 结果MBP的硬盘线gg了, 硬盘貌似也有问题, 索性都换了... 这几天鼓捣好Lion之后开始重写代码, 已经初步实现了用户注册/登陆, 添加数据源(暂时只有新浪微薄)和手动获取数据的功能, 接下来有两个方向要着手继续做了, 一个是要用Scheduler实现自动获取数据, 另一个就是添加更多的数据源, 总之慢慢搞吧, 嗯~</p>
<p>刚刚成功地用scp从dotcloud上把t.aquarhead.com的代码弄下来了, 还用以前的空间中转了下, 泪目啊... 突然想到这个t不是tweet是tucao(吐槽)啊啊啊!!! 抽时间弄下换页和删除就满意了</p>

	Tags:<a href="http://www.aquarhead.com/tag/code-geass/" title="Code Geass" rel="tag">Code Geass</a>,<a href="http://www.aquarhead.com/tag/eternal-feather/" title="eternal feather" rel="tag">eternal feather</a>,<a href="http://www.aquarhead.com/tag/last-exile/" title="Last Exile" rel="tag">Last Exile</a>,<a href="http://www.aquarhead.com/tag/timepaw/" title="timepaw" rel="tag">timepaw</a>,<a href="http://www.aquarhead.com/tag/%e5%8a%a8%e6%bc%ab/" title="动漫" rel="tag">动漫</a>,<a href="http://www.aquarhead.com/tag/%e5%8f%8d%e5%8f%9b%e7%9a%84%e9%b2%81%e8%b7%af%e4%bf%ae/" title="反叛的鲁路修" rel="tag">反叛的鲁路修</a>,<a href="http://www.aquarhead.com/tag/%e6%82%a0%e4%b9%85%e4%b9%8b%e7%bf%bc/" title="悠久之翼" rel="tag">悠久之翼</a>,<a href="http://www.aquarhead.com/tag/%e7%a9%ba%e4%b9%8b%e5%a2%83%e7%95%8c/" title="空之境界" rel="tag">空之境界</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.aquarhead.com/2011/09/thoughts-about-anime-and-timepaw/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Deploy Django on dotCloud</title>
		<link>http://www.aquarhead.com/2011/08/deploy-django-on-dotcloud/</link>
		<comments>http://www.aquarhead.com/2011/08/deploy-django-on-dotcloud/#comments</comments>
		<pubDate>Tue, 23 Aug 2011 01:48:02 +0000</pubDate>
		<dc:creator>AquarHEAD</dc:creator>
				<category><![CDATA[Code Fortress]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[dotCloud]]></category>
		<category><![CDATA[PaaS]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[扯淡]]></category>

		<guid isPermaLink="false">http://www.aquarhead.com/?p=1043</guid>
		<description><![CDATA[好久没写东西了 嗯... 前几天还跟CPY说都快没有驾驭文字的能力了... 该多写点儿东西了~ so 总结一下最近在dotCloud上部署Django应用的一些经验, dotCloud允许你任意组合不同的语言/框架/数据库, 而且免费的plan就很划算啦, 每个应用可以选择两种Services, 不过可以创建任意数量的应用~ 她应该算一种PaaS吧?(话说第一次听说*aaS还是yo2老大跟我提起的 好惭愧~) 哦对了 她网址就是http://www.dotcloud.com啦~ 接下来说正题, 在dotCloud上部署Django先参考这篇教程. 不过这个东西最坑爹的就是那个样例wsgi.py, 我copy到vi里面结果缩进不对... 就一直提示找不到wsgi application... 总之注意改好了就OK了... 还有就是其中提到的三个文件(dotcloud.yml requirements.txt 和 wsgi.py)都要放在实际的Django Project的外面, 按这个教程的意思目录结构大概是这样的: django-hello-world/ - dotcloud.yml - requirements.txt - wsgi.py - mysite/ -- manage.py -- settings.py ... 嗯, 不过这个是最简单的Django应用了, 我们在部署已经写好的应用时要注意改好一下几个地方: 首先是数据库, 如果你用sqlite3的话 那个NAME要填绝对路径, /home/dotcloud/xxx.db就好了, 如果你用的是MySQL或者其他什么东西, 就去参考她的教程, 应该没什么难度的~ 然后是settings.py里面的TEMPLATE_DIRS这个, 注意你push的时候整个目录在服务器上是 /home/dotcloud/current/ 这个再加上项目名字然后/templates就OK了, [...]]]></description>
			<content:encoded><![CDATA[<p>好久没写东西了 嗯... 前几天还跟CPY说都快没有驾驭文字的能力了... 该多写点儿东西了~ so<br />
总结一下最近在dotCloud上部署Django应用的一些经验, dotCloud允许你任意组合不同的语言/框架/数据库, 而且免费的plan就很划算啦, 每个应用可以选择两种Services, 不过可以创建任意数量的应用~<br />
她应该算一种PaaS吧?(话说第一次听说*aaS还是yo2老大跟我提起的 好惭愧~)</p>
<p>哦对了 她网址就是<a href="http://www.dotcloud.com" title="dotCloud" target="_blank">http://www.dotcloud.com</a>啦~</p>
<p>接下来说正题, 在dotCloud上部署Django先参考<a href="http://docs.dotcloud.com/tutorials/python/django-hello-world/" target="_blank">这篇教程</a>.<br />
不过这个东西最坑爹的就是那个样例wsgi.py, 我copy到vi里面结果缩进不对... 就一直提示找不到wsgi application... 总之注意改好了就OK了...<br />
还有就是其中提到的三个文件(dotcloud.yml requirements.txt 和 wsgi.py)都要放在实际的Django Project的外面, 按这个教程的意思目录结构大概是这样的:<br />
django-hello-world/<br />
- dotcloud.yml<br />
- requirements.txt<br />
- wsgi.py<br />
- mysite/<br />
-- manage.py<br />
-- settings.py<br />
...<br />
嗯, 不过这个是最简单的Django应用了, 我们在部署已经写好的应用时要注意改好一下几个地方:</p>
<p>首先是数据库, 如果你用sqlite3的话 那个NAME要填绝对路径, /home/dotcloud/xxx.db就好了, 如果你用的是MySQL或者其他什么东西, 就去参考她的教程, 应该没什么难度的~</p>
<p>然后是settings.py里面的TEMPLATE_DIRS这个, 注意你push的时候整个目录在服务器上是 /home/dotcloud/current/ 这个再加上项目名字然后/templates就OK了, 其实所有绝对路径的地方你都要修改的(废话...)</p>
<p>另外如果有静态文件啥的话可能要参考下dotCloud文档里面的相关内容, 我自己的一个小应用里面貌似暂时没用到,这方面以后再更新吧~</p>
<p>最后, 初始化数据库用这条命令(要在push了整个项目之后哦~~)</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">dotcloud run xxx.www <span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>dotcloud<span style="color: #000000; font-weight: bold;">/</span>current<span style="color: #000000; font-weight: bold;">/</span>xxx<span style="color: #000000; font-weight: bold;">/</span>manage.py syncdb</pre></div></div>

<p>现在你的项目应该可以顺利运行了, 整个push最后会给你一个url就是项目的网址啦~</p>
<p>如果你觉得不爽 也可以绑定自己的域名, 就是</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">dotcloud <span style="color: #7a0874; font-weight: bold;">alias</span> add xxx.www abc.example.com</pre></div></div>

<p>这样啦, 然后她会告诉你去域名服务商那里添加一条CNAME记录就OK啦~</p>
<p>早上起来刚搞定了以前做的一个类似微博的小应用(连验证都没做~ 而且渣CSS见笑啦~~)在dotCloud上的部署, 本来之前做的都对了 就是一直卡在wsgi.py那里... 太坑爹了啊...<br />
嗯.. 欢迎围观<a href="http://t.aquarhead.com" target="_blank">http://t.aquarhead.com</a></p>

	Tags:<a href="http://www.aquarhead.com/tag/django/" title="Django" rel="tag">Django</a>,<a href="http://www.aquarhead.com/tag/dotcloud/" title="dotCloud" rel="tag">dotCloud</a>,<a href="http://www.aquarhead.com/tag/paas/" title="PaaS" rel="tag">PaaS</a>,<a href="http://www.aquarhead.com/tag/python/" title="Python" rel="tag">Python</a>,<a href="http://www.aquarhead.com/tag/%e6%89%af%e6%b7%a1/" title="扯淡" rel="tag">扯淡</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.aquarhead.com/2011/08/deploy-django-on-dotcloud/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>About You, About Me</title>
		<link>http://www.aquarhead.com/2011/04/missing/</link>
		<comments>http://www.aquarhead.com/2011/04/missing/#comments</comments>
		<pubDate>Wed, 27 Apr 2011 18:44:38 +0000</pubDate>
		<dc:creator>AquarHEAD</dc:creator>
				<category><![CDATA[Innocence Life]]></category>

		<guid isPermaLink="false">http://www.aquarhead.com/?p=1040</guid>
		<description><![CDATA["琦 我想你" 那天睡不着了, 用手机QQ给你留言, 刚点发送程序就崩溃了, 我不知道你有没有看到, 以前, 能看到你挂QQ到很晚很晚, 我却只是看着你的头像, 从未鼓起勇气按下. 一如现在光标闪烁着却按不下键盘, 关于你关于我的那些点点滴滴, 幸福难过在这一年里循环沉积, 到如今演变成疏离. 如果有一天 我可以不再让你承受异地的痛苦 我一定会去找你. This Post has no tag.]]></description>
			<content:encoded><![CDATA[<p>"琦 我想你"</p>
<p>那天睡不着了, 用手机QQ给你留言, 刚点发送程序就崩溃了, 我不知道你有没有看到, 以前, 能看到你挂QQ到很晚很晚, 我却只是看着你的头像, 从未鼓起勇气按下. 一如现在光标闪烁着却按不下键盘, 关于你关于我的那些点点滴滴, 幸福难过在这一年里循环沉积, 到如今演变成疏离.</p>
<p>如果有一天 我可以不再让你承受异地的痛苦 我一定会去找你.</p>
This Post has no tag.]]></content:encoded>
			<wfw:commentRss>http://www.aquarhead.com/2011/04/missing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>改进的Nitrogen聊天室</title>
		<link>http://www.aquarhead.com/2011/01/nitrogen-chatroom-reload/</link>
		<comments>http://www.aquarhead.com/2011/01/nitrogen-chatroom-reload/#comments</comments>
		<pubDate>Thu, 27 Jan 2011 10:48:39 +0000</pubDate>
		<dc:creator>AquarHEAD</dc:creator>
				<category><![CDATA[Code Fortress]]></category>
		<category><![CDATA[chatroom]]></category>
		<category><![CDATA[Erlang]]></category>
		<category><![CDATA[Nitrogen]]></category>

		<guid isPermaLink="false">http://www.aquarhead.com/?p=1033</guid>
		<description><![CDATA[几个月前在上海买了Erlang Programming, 研究了一段时间(因为考试中断)感觉很喜欢, 前几天重新看了Nitrogen的tutorial, 非常棒的框架! 按着里面的指导做了一个chatroom, 回去之后又稍微改进了一些, 增加了清空页面, 发送消息后自动清空输入框等等. 待改进的是需要加一个数据库的支持.准备研究一下CouchDB.. chat.erl 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 [...]]]></description>
			<content:encoded><![CDATA[<p>几个月前在上海买了Erlang Programming, 研究了一段时间(因为考试中断)感觉很喜欢, 前几天重新看了Nitrogen的tutorial, 非常棒的框架!<br />
按着里面的指导做了一个chatroom, 回去之后又稍微改进了一些, 增加了清空页面, 发送消息后自动清空输入框等等.<br />
待改进的是需要加一个数据库的支持.准备研究一下CouchDB..</p>
<p>chat.erl</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="erlang" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">%% -*- mode: nitrogen -*-</span>
<span style="color: #014ea4;">-</span><span style="color: #ff3c00;">module</span> <span style="color: #109ab8;">&#40;</span>chat<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">compile</span><span style="color: #109ab8;">&#40;</span>export_all<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">include_lib</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;nitrogen/include/wf.hrl&quot;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">include</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;records.hrl&quot;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">main</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #186895;">case</span> wf:<span style="color: #ff3c00;">role</span><span style="color: #109ab8;">&#40;</span>chatroom_user<span style="color: #109ab8;">&#41;</span> <span style="color: #186895;">of</span>
    true <span style="color: #6bb810;">-&gt;</span>
      #template <span style="color: #109ab8;">&#123;</span> file<span style="color: #014ea4;">=</span><span style="color: #ff7800;">&quot;./site/templates/bare.html&quot;</span> <span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">;</span>
    false <span style="color: #6bb810;">-&gt;</span>
      <span style="color: #006600;">wf</span>:<span style="color: #ff3c00;">redirect_to_login</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;/login&quot;</span><span style="color: #109ab8;">&#41;</span>
  <span style="color: #186895;">end</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">title</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> <span style="color: #ff7800;">&quot;Chatroom&quot;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">body</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #006600;">wf</span>:<span style="color: #ff3c00;">comet_global</span><span style="color: #109ab8;">&#40;</span><span style="color: #186895;">fun</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> <span style="color: #ff3c00;">wait_message</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #186895;">end</span><span style="color: #6bb810;">,</span> chatroom_pool<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
  wf:<span style="color: #ff3c00;">flash</span><span style="color: #109ab8;">&#40;</span>#span <span style="color: #109ab8;">&#123;</span> text<span style="color: #014ea4;">=</span><span style="color: #ff7800;">&quot;Welcome &quot;</span> <span style="color: #014ea4;">++</span> wf:<span style="color: #ff3c00;">session</span><span style="color: #109ab8;">&#40;</span>userID<span style="color: #109ab8;">&#41;</span> <span style="color: #014ea4;">++</span> <span style="color: #ff7800;">&quot;!&quot;</span> <span style="color: #109ab8;">&#125;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
  #panel <span style="color: #109ab8;">&#123;</span> style<span style="color: #014ea4;">=</span><span style="color: #ff7800;">&quot;margin: 50px;&quot;</span><span style="color: #6bb810;">,</span> body<span style="color: #014ea4;">=</span><span style="color: #109ab8;">&#91;</span>
    #flash <span style="color: #109ab8;">&#123;</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
    #h1 <span style="color: #109ab8;">&#123;</span> style<span style="color: #014ea4;">=</span><span style="color: #ff7800;">&quot;margin-bottom: 5px;&quot;</span><span style="color: #6bb810;">,</span> text<span style="color: #014ea4;">=</span><span style="color: #ff7800;">&quot;A Real Time Chatroom&quot;</span> <span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
    #textbox <span style="color: #109ab8;">&#123;</span> id<span style="color: #014ea4;">=</span>message_box<span style="color: #6bb810;">,</span> style<span style="color: #014ea4;">=</span><span style="color: #ff7800;">&quot;margin-left: 10px;&quot;</span><span style="color: #6bb810;">,</span> text<span style="color: #014ea4;">=</span><span style="color: #ff7800;">&quot;input message here...&quot;</span><span style="color: #6bb810;">,</span> next<span style="color: #014ea4;">=</span>submit_button <span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
    #button <span style="color: #109ab8;">&#123;</span> id<span style="color: #014ea4;">=</span>submit_button<span style="color: #6bb810;">,</span> text<span style="color: #014ea4;">=</span><span style="color: #ff7800;">&quot;Enter&quot;</span><span style="color: #6bb810;">,</span> postback<span style="color: #014ea4;">=</span>submit_message <span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
    #button <span style="color: #109ab8;">&#123;</span> id<span style="color: #014ea4;">=</span>clear_button<span style="color: #6bb810;">,</span> text<span style="color: #014ea4;">=</span><span style="color: #ff7800;">&quot;Clear&quot;</span><span style="color: #6bb810;">,</span> postback<span style="color: #014ea4;">=</span>clear_history <span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
    #button <span style="color: #109ab8;">&#123;</span> id<span style="color: #014ea4;">=</span>logout_button<span style="color: #6bb810;">,</span> text<span style="color: #014ea4;">=</span><span style="color: #ff7800;">&quot;Logout&quot;</span><span style="color: #6bb810;">,</span> postback<span style="color: #014ea4;">=</span>log_out <span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
    #hr <span style="color: #109ab8;">&#123;</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
    #panel <span style="color: #109ab8;">&#123;</span> id<span style="color: #014ea4;">=</span>message_list <span style="color: #109ab8;">&#125;</span>
  <span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">wait_message</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #186895;">receive</span>
    <span style="color: #109ab8;">&#123;</span>msg<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Message</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Author</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Time</span><span style="color: #109ab8;">&#125;</span> <span style="color: #6bb810;">-&gt;</span>
      <span style="color: #109ab8;">&#123;</span><span style="color: #109ab8;">&#123;</span><span style="color: #45b3e6;">Year</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Mouth</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Day</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#123;</span><span style="color: #45b3e6;">Hour</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Minute</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Second</span><span style="color: #109ab8;">&#125;</span><span style="color: #109ab8;">&#125;</span> <span style="color: #014ea4;">=</span> <span style="color: #45b3e6;">Time</span><span style="color: #6bb810;">,</span>
      <span style="color: #45b3e6;">Header</span> <span style="color: #014ea4;">=</span> <span style="color: #ff4e18;">io_lib</span>:<span style="color: #ff3c00;">format</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot; @ ~4p.~2p.~2p, ~2p:~2p:~2p&quot;</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">Year</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Mouth</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Day</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Hour</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Minute</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Second</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
      wf:<span style="color: #ff3c00;">insert_top</span><span style="color: #109ab8;">&#40;</span>message_list<span style="color: #6bb810;">,</span> #panel <span style="color: #109ab8;">&#123;</span> style<span style="color: #014ea4;">=</span><span style="color: #ff7800;">&quot;border: solid 1px blue; padding: 5px; margin-bottom: 5px;&quot;</span><span style="color: #6bb810;">,</span> body<span style="color: #014ea4;">=</span><span style="color: #109ab8;">&#91;</span>
        <span style="color: #45b3e6;">Author</span><span style="color: #6bb810;">,</span>
        #span <span style="color: #109ab8;">&#123;</span> style<span style="color: #014ea4;">=</span><span style="color: #ff7800;">&quot;font-size: 0.75em&quot;</span><span style="color: #6bb810;">,</span> text<span style="color: #014ea4;">=</span><span style="color: #45b3e6;">Header</span> <span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
        #br <span style="color: #109ab8;">&#123;</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
        #span <span style="color: #109ab8;">&#123;</span> style<span style="color: #014ea4;">=</span><span style="color: #ff7800;">&quot;margin-left: 10px; font-size: 1.5em;&quot;</span><span style="color: #6bb810;">,</span> text<span style="color: #014ea4;">=</span><span style="color: #45b3e6;">Message</span> <span style="color: #109ab8;">&#125;</span>
      <span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#125;</span><span style="color: #109ab8;">&#41;</span>
  <span style="color: #186895;">end</span><span style="color: #6bb810;">,</span>
  wf:<span style="color: #ff3c00;">flush</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
  <span style="color: #ff3c00;">wait_message</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">event</span><span style="color: #109ab8;">&#40;</span>clear_history<span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #006600;">wf</span>:<span style="color: #ff3c00;">flash</span><span style="color: #109ab8;">&#40;</span>#span <span style="color: #109ab8;">&#123;</span> text<span style="color: #014ea4;">=</span><span style="color: #ff7800;">&quot;Chat history cleared.&quot;</span> <span style="color: #109ab8;">&#125;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
  wf:<span style="color: #ff3c00;">replace</span><span style="color: #109ab8;">&#40;</span>message_list<span style="color: #6bb810;">,</span> #panel <span style="color: #109ab8;">&#123;</span> id<span style="color: #014ea4;">=</span>message_list <span style="color: #109ab8;">&#125;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">;</span>
<span style="color: #ff3c00;">event</span><span style="color: #109ab8;">&#40;</span>submit_message<span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #186895;">case</span> wf:<span style="color: #ff3c00;">role</span><span style="color: #109ab8;">&#40;</span>chatroom_user<span style="color: #109ab8;">&#41;</span> <span style="color: #186895;">of</span>
    true <span style="color: #6bb810;">-&gt;</span>
      <span style="color: #006600;">wf</span>:<span style="color: #ff3c00;">send_global</span><span style="color: #109ab8;">&#40;</span>chatroom_pool<span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#123;</span>msg<span style="color: #6bb810;">,</span> wf:<span style="color: #ff3c00;">q</span><span style="color: #109ab8;">&#40;</span>message_box<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span> wf:<span style="color: #ff3c00;">session</span><span style="color: #109ab8;">&#40;</span>userID<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span> <span style="color: #ff4e18;">calendar</span>:<span style="color: #006600;">local_</span><span style="color: #fa6fff;">time</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#125;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
      wf:<span style="color: #ff3c00;">replace</span><span style="color: #109ab8;">&#40;</span>message_box<span style="color: #6bb810;">,</span> #textbox <span style="color: #109ab8;">&#123;</span> id<span style="color: #014ea4;">=</span>message_box<span style="color: #6bb810;">,</span> style<span style="color: #014ea4;">=</span><span style="color: #ff7800;">&quot;margin-left: 10px;&quot;</span><span style="color: #6bb810;">,</span> next<span style="color: #014ea4;">=</span>submit_button <span style="color: #109ab8;">&#125;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">;</span>
    false <span style="color: #6bb810;">-&gt;</span>
      <span style="color: #006600;">wf</span>:<span style="color: #ff3c00;">redirect_to_login</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;/login&quot;</span><span style="color: #109ab8;">&#41;</span>
  <span style="color: #186895;">end</span><span style="color: #6bb810;">;</span>
<span style="color: #ff3c00;">event</span><span style="color: #109ab8;">&#40;</span>log_out<span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #006600;">wf</span>:<span style="color: #ff3c00;">logout</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
  wf:<span style="color: #ff3c00;">redirect</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;/chat&quot;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span></pre></td></tr></table></div>

<p>login.erl(其实这个在tutorial里面都有写, 还是发一下好了)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="erlang" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">%% -*- mode: nitrogen -*-</span>
<span style="color: #014ea4;">-</span><span style="color: #ff3c00;">module</span> <span style="color: #109ab8;">&#40;</span>login<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">compile</span><span style="color: #109ab8;">&#40;</span>export_all<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">include_lib</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;nitrogen/include/wf.hrl&quot;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">include</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;records.hrl&quot;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">main</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> #template <span style="color: #109ab8;">&#123;</span> file<span style="color: #014ea4;">=</span><span style="color: #ff7800;">&quot;./site/templates/bare.html&quot;</span> <span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">title</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> <span style="color: #ff7800;">&quot;Login&quot;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">body</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #006600;">wf</span>:<span style="color: #ff3c00;">wire</span><span style="color: #109ab8;">&#40;</span>submit<span style="color: #6bb810;">,</span> username<span style="color: #6bb810;">,</span> #validate <span style="color: #109ab8;">&#123;</span> validators<span style="color: #014ea4;">=</span><span style="color: #109ab8;">&#91;</span>
    #is_required <span style="color: #109ab8;">&#123;</span> text<span style="color: #014ea4;">=</span><span style="color: #ff7800;">&quot;Required.&quot;</span> <span style="color: #109ab8;">&#125;</span>
  <span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#125;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
  wf:<span style="color: #ff3c00;">wire</span><span style="color: #109ab8;">&#40;</span>submit<span style="color: #6bb810;">,</span> password<span style="color: #6bb810;">,</span> #validate <span style="color: #109ab8;">&#123;</span> validators<span style="color: #014ea4;">=</span><span style="color: #109ab8;">&#91;</span>
    #is_required <span style="color: #109ab8;">&#123;</span> text<span style="color: #014ea4;">=</span><span style="color: #ff7800;">&quot;Required.&quot;</span> <span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
    #custom <span style="color: #109ab8;">&#123;</span>
      text<span style="color: #014ea4;">=</span><span style="color: #ff7800;">&quot;Invalid password.&quot;</span><span style="color: #6bb810;">,</span>
      <span style="color: #fa6fff;">function</span><span style="color: #014ea4;">=</span><span style="color: #186895;">fun</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">_</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Value</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> <span style="color: #45b3e6;">Value</span> <span style="color: #014ea4;">==</span> <span style="color: #ff7800;">&quot;chatroom&quot;</span> <span style="color: #186895;">end</span>
    <span style="color: #109ab8;">&#125;</span>
  <span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#125;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
  #panel <span style="color: #109ab8;">&#123;</span> style<span style="color: #014ea4;">=</span><span style="color: #ff7800;">&quot;margin: 50px;&quot;</span><span style="color: #6bb810;">,</span> body<span style="color: #014ea4;">=</span><span style="color: #109ab8;">&#91;</span>
    #flash <span style="color: #109ab8;">&#123;</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
    #label <span style="color: #109ab8;">&#123;</span> text<span style="color: #014ea4;">=</span><span style="color: #ff7800;">&quot;Username&quot;</span> <span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
    #textbox <span style="color: #109ab8;">&#123;</span> id<span style="color: #014ea4;">=</span>username<span style="color: #6bb810;">,</span> next<span style="color: #014ea4;">=</span>password <span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
    #br <span style="color: #109ab8;">&#123;</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
    #label <span style="color: #109ab8;">&#123;</span> text<span style="color: #014ea4;">=</span><span style="color: #ff7800;">&quot;Password&quot;</span> <span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
    #password <span style="color: #109ab8;">&#123;</span> id<span style="color: #014ea4;">=</span>password<span style="color: #6bb810;">,</span> next<span style="color: #014ea4;">=</span>submit <span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
    #br <span style="color: #109ab8;">&#123;</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
    #button <span style="color: #109ab8;">&#123;</span> text<span style="color: #014ea4;">=</span><span style="color: #ff7800;">&quot;Login&quot;</span><span style="color: #6bb810;">,</span> id<span style="color: #014ea4;">=</span>submit<span style="color: #6bb810;">,</span> postback<span style="color: #014ea4;">=</span>login <span style="color: #109ab8;">&#125;</span>
  <span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">event</span><span style="color: #109ab8;">&#40;</span>login<span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #006600;">wf</span>:<span style="color: #ff3c00;">role</span><span style="color: #109ab8;">&#40;</span>chatroom_user<span style="color: #6bb810;">,</span> true<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
  wf:<span style="color: #ff3c00;">session_default</span><span style="color: #109ab8;">&#40;</span>userID<span style="color: #6bb810;">,</span> wf:<span style="color: #ff3c00;">q</span><span style="color: #109ab8;">&#40;</span>username<span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
  wf:<span style="color: #ff3c00;">session</span><span style="color: #109ab8;">&#40;</span>userID<span style="color: #6bb810;">,</span> wf:<span style="color: #ff3c00;">q</span><span style="color: #109ab8;">&#40;</span>username<span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
  wf:<span style="color: #ff3c00;">redirect_from_login</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;/&quot;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span></pre></td></tr></table></div>


	Tags:<a href="http://www.aquarhead.com/tag/chatroom/" title="chatroom" rel="tag">chatroom</a>,<a href="http://www.aquarhead.com/tag/erlang/" title="Erlang" rel="tag">Erlang</a>,<a href="http://www.aquarhead.com/tag/nitrogen/" title="Nitrogen" rel="tag">Nitrogen</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.aquarhead.com/2011/01/nitrogen-chatroom-reload/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Real World Haskell 第三章练习题</title>
		<link>http://www.aquarhead.com/2010/12/real-world-haskell-exercise3/</link>
		<comments>http://www.aquarhead.com/2010/12/real-world-haskell-exercise3/#comments</comments>
		<pubDate>Thu, 30 Dec 2010 07:18:51 +0000</pubDate>
		<dc:creator>AquarHEAD</dc:creator>
				<category><![CDATA[Code Fortress]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Real World Haskell]]></category>
		<category><![CDATA[习题]]></category>
		<category><![CDATA[函数式语言]]></category>

		<guid isPermaLink="false">http://www.aquarhead.com/?p=1029</guid>
		<description><![CDATA[这几天又玩了一下下Haskell, 感觉和Erlang有很多相似的东西 Tuple List Guard 等等,所以学起来不是太困难 读的是online version的Real World Haskell,下面是第三章最后的12道习题我的代码. 最后一题是写一个凸包的算法,我得复习复习才能写好了,写好再update哈~ 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 [...]]]></description>
			<content:encoded><![CDATA[<p>这几天又玩了一下下Haskell, 感觉和Erlang有很多相似的东西<br />
Tuple List Guard 等等,所以学起来不是太困难<br />
读的是online version的Real World Haskell,下面是第三章最后的12道习题我的代码.<br />
最后一题是写一个凸包的算法,我得复习复习才能写好了,写好再update哈~</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
71
72
73
74
75
76
77
78
79
80
81
82
</pre></td><td class="code"><pre class="haskell" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">-- Prob 1&amp;2</span>
ex3<span style="color: #339933; font-weight: bold;">_</span>len <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Int</span>
ex3<span style="color: #339933; font-weight: bold;">_</span>len <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">0</span>
ex3<span style="color: #339933; font-weight: bold;">_</span>len xs <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">+</span><span style="color: green;">&#41;</span> <span style="color: red;">1</span> <span style="color: green;">&#40;</span>ex3<span style="color: #339933; font-weight: bold;">_</span>len <span style="color: green;">&#40;</span><span style="font-weight: bold;">tail</span> xs<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- Prob 3</span>
ex3<span style="color: #339933; font-weight: bold;">_</span>avg <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Double</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Double</span>
ex3<span style="color: #339933; font-weight: bold;">_</span>avg <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">0.0</span>
ex3<span style="color: #339933; font-weight: bold;">_</span>avg xs <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">/</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>ex3<span style="color: #339933; font-weight: bold;">_</span>sum xs<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">fromIntegral</span> <span style="color: green;">&#40;</span>ex3<span style="color: #339933; font-weight: bold;">_</span>len xs<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
    <span style="color: #06c; font-weight: bold;">where</span> ex3<span style="color: #339933; font-weight: bold;">_</span>sum <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">0.0</span>
          ex3<span style="color: #339933; font-weight: bold;">_</span>sum <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">+</span><span style="color: green;">&#41;</span> x <span style="color: green;">&#40;</span>ex3<span style="color: #339933; font-weight: bold;">_</span>sum xs<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- Prob 4</span>
ex3<span style="color: #339933; font-weight: bold;">_</span>palind <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span>t<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span>t<span style="color: green;">&#93;</span>
ex3<span style="color: #339933; font-weight: bold;">_</span>palind xs <span style="color: #339933; font-weight: bold;">=</span> xs <span style="color: #339933; font-weight: bold;">++</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">reverse</span> xs<span style="color: green;">&#41;</span>
    <span style="color: #06c; font-weight: bold;">where</span> <span style="font-weight: bold;">reverse</span> <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span>
          <span style="font-weight: bold;">reverse</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
          <span style="font-weight: bold;">reverse</span> <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">reverse</span> xs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">++</span> <span style="color: green;">&#91;</span>x<span style="color: green;">&#93;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- Prob 5</span>
ex3<span style="color: #339933; font-weight: bold;">_</span>ispalind <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Bool</span>
ex3<span style="color: #339933; font-weight: bold;">_</span>ispalind <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">=</span> True
ex3<span style="color: #339933; font-weight: bold;">_</span>ispalind xs <span style="color: #339933; font-weight: bold;">|</span> <span style="font-weight: bold;">null</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">tail</span> xs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> True
ex3<span style="color: #339933; font-weight: bold;">_</span>ispalind <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">let</span> la <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">last</span> xs
                          nolast <span style="color: green;">&#40;</span>t:ts<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">|</span> <span style="font-weight: bold;">null</span> ts <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
                          nolast <span style="color: green;">&#40;</span>t:ts<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> t : <span style="color: green;">&#40;</span>nolast ts<span style="color: green;">&#41;</span>
                          nola <span style="color: #339933; font-weight: bold;">=</span> nolast xs
                      <span style="color: #06c; font-weight: bold;">in</span> <span style="color: #06c; font-weight: bold;">if</span> x <span style="color: #339933; font-weight: bold;">/=</span> la
                          <span style="color: #06c; font-weight: bold;">then</span> False
                          <span style="color: #06c; font-weight: bold;">else</span> ex3<span style="color: #339933; font-weight: bold;">_</span>ispalind nola
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- Prob 6</span>
ex3<span style="color: #339933; font-weight: bold;">_</span>sortLength <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span>t<span style="color: green;">&#93;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span>t<span style="color: green;">&#93;</span><span style="color: green;">&#93;</span>
ex3<span style="color: #339933; font-weight: bold;">_</span>sortLength <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
ex3<span style="color: #339933; font-weight: bold;">_</span>sortLength <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">let</span> seperate <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span><span style="color: green;">&#91;</span><span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#40;</span><span style="color: green;">&#91;</span><span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span>
                            seperate <span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">,</span> r<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">_</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">,</span> r<span style="color: green;">&#41;</span>
                            seperate <span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">,</span> r<span style="color: green;">&#41;</span> tt <span style="color: green;">&#40;</span>t:ts<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: #06c; font-weight: bold;">if</span> <span style="color: green;">&#40;</span>ex3<span style="color: #339933; font-weight: bold;">_</span>len t<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&lt;</span> <span style="color: green;">&#40;</span>ex3<span style="color: #339933; font-weight: bold;">_</span>len tt<span style="color: green;">&#41;</span>
                                                        <span style="color: #06c; font-weight: bold;">then</span> seperate <span style="color: green;">&#40;</span>t:l<span style="color: #339933; font-weight: bold;">,</span> r<span style="color: green;">&#41;</span> tt ts
                                                        <span style="color: #06c; font-weight: bold;">else</span> seperate <span style="color: green;">&#40;</span>l<span style="color: #339933; font-weight: bold;">,</span> t:r<span style="color: green;">&#41;</span> tt ts
                            sep <span style="color: #339933; font-weight: bold;">=</span> seperate <span style="color: green;">&#40;</span><span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><span style="color: green;">&#41;</span> x xs
                            left <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">fst</span> sep
                            right <span style="color: #339933; font-weight: bold;">=</span> <span style="font-weight: bold;">snd</span> sep
                        <span style="color: #06c; font-weight: bold;">in</span> ex3<span style="color: #339933; font-weight: bold;">_</span>sortLength<span style="color: green;">&#40;</span>left<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">++</span> <span style="color: green;">&#91;</span>x<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">++</span> ex3<span style="color: #339933; font-weight: bold;">_</span>sortLength<span style="color: green;">&#40;</span>right<span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- Prob 7</span>
ex3<span style="color: #339933; font-weight: bold;">_</span>intersperse <span style="color: #339933; font-weight: bold;">::</span> a <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span>a<span style="color: green;">&#93;</span>
ex3<span style="color: #339933; font-weight: bold;">_</span>intersperse <span style="color: #339933; font-weight: bold;">_</span> <span style="color: green;">&#91;</span><span style="color: green;">&#91;</span><span style="color: green;">&#93;</span><span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
ex3<span style="color: #339933; font-weight: bold;">_</span>intersperse <span style="color: #339933; font-weight: bold;">_</span> <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">|</span> <span style="font-weight: bold;">null</span> xs <span style="color: #339933; font-weight: bold;">=</span> x
ex3<span style="color: #339933; font-weight: bold;">_</span>intersperse a <span style="color: green;">&#40;</span>x:xs<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> x <span style="color: #339933; font-weight: bold;">++</span> <span style="color: green;">&#40;</span>a : <span style="color: green;">&#40;</span>ex3<span style="color: #339933; font-weight: bold;">_</span>intersperse a xs<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- Prob 8</span>
<span style="color: #06c; font-weight: bold;">data</span> Tree a <span style="color: #339933; font-weight: bold;">=</span> Node a <span style="color: green;">&#40;</span>Tree a<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>Tree a<span style="color: green;">&#41;</span>
            <span style="color: #339933; font-weight: bold;">|</span> Empty
              <span style="color: #06c; font-weight: bold;">deriving</span> <span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Show</span><span style="color: green;">&#41;</span>
&nbsp;
ex3<span style="color: #339933; font-weight: bold;">_</span>treeHeight <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#40;</span>Tree a<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Int</span>
ex3<span style="color: #339933; font-weight: bold;">_</span>treeHeight Empty <span style="color: #339933; font-weight: bold;">=</span> <span style="color: red;">0</span>
ex3<span style="color: #339933; font-weight: bold;">_</span>treeHeight <span style="color: green;">&#40;</span>Node a l r<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">+</span><span style="color: green;">&#41;</span> <span style="color: red;">1</span> <span style="color: green;">&#40;</span>bigger <span style="color: green;">&#40;</span>ex3<span style="color: #339933; font-weight: bold;">_</span>treeHeight l<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>ex3<span style="color: #339933; font-weight: bold;">_</span>treeHeight r<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
    <span style="color: #06c; font-weight: bold;">where</span> bigger <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Int</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: #cccc00; font-weight: bold;">Int</span>
          bigger a b <span style="color: #339933; font-weight: bold;">|</span> a <span style="color: #339933; font-weight: bold;">&lt;</span> b <span style="color: #339933; font-weight: bold;">=</span> b
          bigger a <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">=</span> a
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- Prob 9&amp;10</span>
<span style="color: #06c; font-weight: bold;">data</span> Direction <span style="color: #339933; font-weight: bold;">=</span> TurnLeft
               <span style="color: #339933; font-weight: bold;">|</span> TurnRight
               <span style="color: #339933; font-weight: bold;">|</span> GoStraight
                 <span style="color: #06c; font-weight: bold;">deriving</span> <span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Eq</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: #cccc00; font-weight: bold;">Show</span><span style="color: green;">&#41;</span>
&nbsp;
<span style="color: #06c; font-weight: bold;">data</span> Point <span style="color: #339933; font-weight: bold;">=</span> Point <span style="color: #cccc00; font-weight: bold;">Double</span> <span style="color: #cccc00; font-weight: bold;">Double</span>
             <span style="color: #06c; font-weight: bold;">deriving</span> <span style="color: green;">&#40;</span><span style="color: #cccc00; font-weight: bold;">Eq</span><span style="color: #339933; font-weight: bold;">,</span> <span style="color: #cccc00; font-weight: bold;">Show</span><span style="color: green;">&#41;</span>
&nbsp;
ex3<span style="color: #339933; font-weight: bold;">_</span>whichDirection <span style="color: #339933; font-weight: bold;">::</span> Point <span style="color: #339933; font-weight: bold;">-&gt;</span> Point <span style="color: #339933; font-weight: bold;">-&gt;</span> Point <span style="color: #339933; font-weight: bold;">-&gt;</span> Direction
ex3<span style="color: #339933; font-weight: bold;">_</span>whichDirection <span style="color: green;">&#40;</span>Point x1 y1<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>Point x2 y2<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span>Point x3 y3<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> testCrossProduct <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">-</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">*</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">-</span><span style="color: green;">&#41;</span> x2 x1<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">-</span><span style="color: green;">&#41;</span> y3 y1<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">*</span><span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">-</span><span style="color: green;">&#41;</span> x3 x1<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="color: green;">&#40;</span><span style="color: #339933; font-weight: bold;">-</span><span style="color: green;">&#41;</span> y2 y1<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span><span style="color: green;">&#41;</span>
    <span style="color: #06c; font-weight: bold;">where</span> testCrossProduct <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Double</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> Direction
          testCrossProduct <span style="color: red;">0</span> <span style="color: #339933; font-weight: bold;">=</span> GoStraight
          testCrossProduct n <span style="color: #339933; font-weight: bold;">|</span> n <span style="color: #339933; font-weight: bold;">&gt;</span> <span style="color: red;">0</span> <span style="color: #339933; font-weight: bold;">=</span> TurnLeft
          testCrossProduct <span style="color: #339933; font-weight: bold;">_</span> <span style="color: #339933; font-weight: bold;">=</span> TurnRight
&nbsp;
<span style="color: #5d478b; font-style: italic;">-- Prob 11</span>
ex3<span style="color: #339933; font-weight: bold;">_</span>directions <span style="color: #339933; font-weight: bold;">::</span> <span style="color: green;">&#91;</span>Point<span style="color: green;">&#93;</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span>Direction<span style="color: green;">&#93;</span>
ex3<span style="color: #339933; font-weight: bold;">_</span>directions notEnough <span style="color: #339933; font-weight: bold;">|</span> <span style="color: green;">&#40;</span>ex3<span style="color: #339933; font-weight: bold;">_</span>len notEnough<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">&lt;</span> <span style="color: red;">3</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: green;">&#93;</span>
ex3<span style="color: #339933; font-weight: bold;">_</span>directions <span style="color: green;">&#40;</span>p:ps<span style="color: green;">&#41;</span> <span style="color: #339933; font-weight: bold;">=</span> ex3<span style="color: #339933; font-weight: bold;">_</span>whichDirection p <span style="color: green;">&#40;</span><span style="font-weight: bold;">head</span> ps<span style="color: green;">&#41;</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">head</span> <span style="color: green;">&#40;</span><span style="font-weight: bold;">tail</span> ps<span style="color: green;">&#41;</span><span style="color: green;">&#41;</span> : <span style="color: green;">&#40;</span>ex3<span style="color: #339933; font-weight: bold;">_</span>directions ps<span style="color: green;">&#41;</span></pre></td></tr></table></div>


	Tags:<a href="http://www.aquarhead.com/tag/functional-programming/" title="functional programming" rel="tag">functional programming</a>,<a href="http://www.aquarhead.com/tag/haskell/" title="Haskell" rel="tag">Haskell</a>,<a href="http://www.aquarhead.com/tag/real-world-haskell/" title="Real World Haskell" rel="tag">Real World Haskell</a>,<a href="http://www.aquarhead.com/tag/%e4%b9%a0%e9%a2%98/" title="习题" rel="tag">习题</a>,<a href="http://www.aquarhead.com/tag/%e5%87%bd%e6%95%b0%e5%bc%8f%e8%af%ad%e8%a8%80/" title="函数式语言" rel="tag">函数式语言</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.aquarhead.com/2010/12/real-world-haskell-exercise3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>x86汇编写的16进制加法计算器</title>
		<link>http://www.aquarhead.com/2010/12/hex-add-in-assembly-language/</link>
		<comments>http://www.aquarhead.com/2010/12/hex-add-in-assembly-language/#comments</comments>
		<pubDate>Tue, 28 Dec 2010 06:12:58 +0000</pubDate>
		<dc:creator>AquarHEAD</dc:creator>
				<category><![CDATA[Code Fortress]]></category>
		<category><![CDATA[16进制]]></category>
		<category><![CDATA[ASM]]></category>

		<guid isPermaLink="false">http://www.aquarhead.com/?p=1021</guid>
		<description><![CDATA[汇编课的第二次作业,很久之前写了一点儿,今天又花了一个小时给搞好了,代码很纠结...也没怎么想优化结构等等,逆向的第二次作业是分析重定位= =&#124;&#124;唉... P.S. 下周就是最后一节汇编课了唉= = 刚才优化了一下代码,又改了一下缩进. 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 [...]]]></description>
			<content:encoded><![CDATA[<p>汇编课的第二次作业,很久之前写了一点儿,今天又花了一个小时给搞好了,代码很纠结...也没怎么想优化结构等等,逆向的第二次作业是分析重定位= =||唉...</p>
<p>P.S. 下周就是最后一节汇编课了唉= = 刚才优化了一下代码,又改了一下缩进.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
71
72
73
74
75
76
77
78
79
80
81
82
83
</pre></td><td class="code"><pre class="asm" style="font-family:monospace;"><span style="color: #339933;">.</span><span style="color: #000000; font-weight: bold;">286</span>
<span style="color: #000000; font-weight: bold;">data</span> <span style="color: #000000; font-weight: bold;">segment</span>
buf <span style="color: #000000; font-weight: bold;">db</span> <span style="color: #0000ff;">5</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">5</span> <span style="color: #000000; font-weight: bold;">dup</span><span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #0000ff;">0</span><span style="color: #009900; font-weight: bold;">&#41;</span>
cr <span style="color: #000000; font-weight: bold;">db</span> <span style="color: #0000ff;">0Dh</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0Ah</span><span style="color: #339933;">,</span> <span style="color: #7f007f;">'$'</span>
hex <span style="color: #000000; font-weight: bold;">db</span> <span style="color: #7f007f;">'0123456789ABCDEF'</span>
<span style="color: #000000; font-weight: bold;">data</span> <span style="color: #000000; font-weight: bold;">ends</span>
<span style="color: #000000; font-weight: bold;">code</span> <span style="color: #000000; font-weight: bold;">segment</span>
<span style="color: #000000; font-weight: bold;">assume</span> <span style="color: #00007f;">cs</span><span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">code</span><span style="color: #339933;">,</span> <span style="color: #00007f;">ds</span><span style="color: #339933;">:</span><span style="color: #000000; font-weight: bold;">data</span>
main<span style="color: #339933;">:</span>
    <span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">ax</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">data</span>
    <span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">ds</span><span style="color: #339933;">,</span> <span style="color: #00007f;">ax</span>
    <span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">cx</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">2</span>
read<span style="color: #339933;">:</span>    
    <span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">ah</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0Ah</span>
    <span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">dx</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">offset</span> buf
    <span style="color: #00007f; font-weight: bold;">int</span> <span style="color: #0000ff;">21h</span>
    <span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">ah</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">9</span>
    <span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">dx</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">offset</span> cr
    <span style="color: #00007f; font-weight: bold;">int</span> <span style="color: #0000ff;">21h</span>
    <span style="color: #00007f; font-weight: bold;">lea</span> <span style="color: #00007f;">bx</span><span style="color: #339933;">,</span> buf<span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #0000ff;">2</span><span style="color: #009900; font-weight: bold;">&#93;</span>
    <span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #00007f;">cx</span>
    <span style="color: #00007f; font-weight: bold;">xor</span> <span style="color: #00007f;">cx</span><span style="color: #339933;">,</span> <span style="color: #00007f;">cx</span>
    <span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">cl</span><span style="color: #339933;">,</span> buf<span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #0000ff;">1</span><span style="color: #009900; font-weight: bold;">&#93;</span>
    <span style="color: #00007f; font-weight: bold;">xor</span> <span style="color: #00007f;">ax</span><span style="color: #339933;">,</span> <span style="color: #00007f;">ax</span>
    <span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #00007f;">ax</span>
atoi<span style="color: #339933;">:</span>
    <span style="color: #00007f; font-weight: bold;">xor</span> <span style="color: #00007f;">dx</span><span style="color: #339933;">,</span> <span style="color: #00007f;">dx</span>
    <span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">al</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">bx</span><span style="color: #009900; font-weight: bold;">&#93;</span>
check<span style="color: #339933;">:</span>
    <span style="color: #00007f; font-weight: bold;">lea</span> <span style="color: #00007f;">di</span><span style="color: #339933;">,</span> hex
    <span style="color: #00007f; font-weight: bold;">add</span> <span style="color: #00007f;">di</span><span style="color: #339933;">,</span> <span style="color: #00007f;">dx</span>
    <span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">ah</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">di</span><span style="color: #009900; font-weight: bold;">&#93;</span>
    <span style="color: #00007f; font-weight: bold;">cmp</span> <span style="color: #00007f;">al</span><span style="color: #339933;">,</span> <span style="color: #00007f;">ah</span>
    <span style="color: #00007f; font-weight: bold;">je</span> checked
    <span style="color: #00007f; font-weight: bold;">inc</span> <span style="color: #00007f;">dx</span>
    <span style="color: #00007f; font-weight: bold;">jmp</span> check
checked<span style="color: #339933;">:</span>
    <span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #00007f;">ax</span>
    <span style="color: #00007f; font-weight: bold;">shl</span> <span style="color: #00007f;">ax</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">4</span>
    <span style="color: #00007f; font-weight: bold;">or</span> <span style="color: #00007f;">ax</span><span style="color: #339933;">,</span> <span style="color: #00007f;">dx</span>
    <span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #00007f;">ax</span>
    <span style="color: #00007f; font-weight: bold;">inc</span> <span style="color: #00007f;">bx</span>
    <span style="color: #00007f; font-weight: bold;">loop</span> atoi
    <span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #00007f;">ax</span>
    <span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #00007f;">cx</span>
    <span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #00007f;">ax</span>
    <span style="color: #00007f; font-weight: bold;">loop</span> read 
    <span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #00007f;">ax</span>
    <span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #00007f;">dx</span>
    <span style="color: #00007f; font-weight: bold;">add</span> <span style="color: #00007f;">ax</span><span style="color: #339933;">,</span> <span style="color: #00007f;">dx</span>
    <span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #00007f;">ax</span>
    <span style="color: #00007f; font-weight: bold;">pushf</span>
    <span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #00007f;">cx</span>
    <span style="color: #00007f; font-weight: bold;">and</span> <span style="color: #00007f;">cl</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">01h</span>
    <span style="color: #00007f; font-weight: bold;">test</span> <span style="color: #00007f;">cl</span><span style="color: #339933;">,</span> <span style="color: #00007f;">cl</span>
    <span style="color: #00007f; font-weight: bold;">jz</span> nocarry
    <span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">ah</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">2</span>
    <span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">dl</span><span style="color: #339933;">,</span> <span style="color: #7f007f;">'1'</span>
    <span style="color: #00007f; font-weight: bold;">int</span> <span style="color: #0000ff;">21h</span>
    <span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #00007f;">ax</span>
nocarry<span style="color: #339933;">:</span>
    <span style="color: #00007f; font-weight: bold;">xor</span> <span style="color: #00007f;">cx</span><span style="color: #339933;">,</span> <span style="color: #00007f;">cx</span>
    <span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">cl</span><span style="color: #339933;">,</span> buf<span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #0000ff;">1</span><span style="color: #009900; font-weight: bold;">&#93;</span>
next<span style="color: #339933;">:</span>
    <span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">dx</span><span style="color: #339933;">,</span> <span style="color: #00007f;">ax</span>
    <span style="color: #00007f; font-weight: bold;">and</span> <span style="color: #00007f;">dh</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0F0h</span>
    <span style="color: #00007f; font-weight: bold;">shr</span> <span style="color: #00007f;">dh</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">4</span>
    <span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">dl</span><span style="color: #339933;">,</span> <span style="color: #00007f;">dh</span>
    <span style="color: #00007f; font-weight: bold;">xor</span> <span style="color: #00007f;">dh</span><span style="color: #339933;">,</span> <span style="color: #00007f;">dh</span>
    <span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">di</span><span style="color: #339933;">,</span> <span style="color: #00007f;">dx</span>
    <span style="color: #00007f; font-weight: bold;">push</span> <span style="color: #00007f;">ax</span>
    <span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">ah</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">2</span>
    <span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">dl</span><span style="color: #339933;">,</span> hex<span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">di</span><span style="color: #009900; font-weight: bold;">&#93;</span>
    <span style="color: #00007f; font-weight: bold;">int</span> <span style="color: #0000ff;">21h</span>
    <span style="color: #00007f; font-weight: bold;">pop</span> <span style="color: #00007f;">ax</span>
    <span style="color: #00007f; font-weight: bold;">inc</span> <span style="color: #00007f;">bx</span>
    <span style="color: #00007f; font-weight: bold;">shl</span> <span style="color: #00007f;">ax</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">4</span>
    <span style="color: #00007f; font-weight: bold;">loop</span> next
done<span style="color: #339933;">:</span>
    <span style="color: #00007f; font-weight: bold;">mov</span> <span style="color: #00007f;">ah</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">4ch</span>
    <span style="color: #00007f; font-weight: bold;">int</span> <span style="color: #0000ff;">21h</span>
<span style="color: #000000; font-weight: bold;">code</span> <span style="color: #000000; font-weight: bold;">ends</span>
<span style="color: #000000; font-weight: bold;">end</span> main</pre></td></tr></table></div>


	Tags:<a href="http://www.aquarhead.com/tag/16%e8%bf%9b%e5%88%b6/" title="16进制" rel="tag">16进制</a>,<a href="http://www.aquarhead.com/tag/asm/" title="ASM" rel="tag">ASM</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.aquarhead.com/2010/12/hex-add-in-assembly-language/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Erlang Programming Exercise 4</title>
		<link>http://www.aquarhead.com/2010/12/erlang-programming-exercise-4/</link>
		<comments>http://www.aquarhead.com/2010/12/erlang-programming-exercise-4/#comments</comments>
		<pubDate>Sun, 26 Dec 2010 16:31:10 +0000</pubDate>
		<dc:creator>AquarHEAD</dc:creator>
				<category><![CDATA[Code Fortress]]></category>
		<category><![CDATA[Erlang]]></category>
		<category><![CDATA[Erlang Programming]]></category>
		<category><![CDATA[Process]]></category>
		<category><![CDATA[习题]]></category>

		<guid isPermaLink="false">http://www.aquarhead.com/?p=1017</guid>
		<description><![CDATA[一共两道题,第一题比较简单,只要注意一下(书上有提示)用内部格式{print, Term}这样,防止调用echo:print(stop)的时候会导致程序退出.上代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 -module&#40;echo&#41;. -export&#40;&#91;start/0, print/1, stop/0, loop/0&#93;&#41;. &#160; loop&#40;&#41; -&#62; receive &#123;print, Term&#125; -&#62; io:format&#40;&#34;~p~n&#34;,&#91;Term&#93;&#41;, loop&#40;&#41;; stop -&#62; true end. &#160; start&#40;&#41; -&#62; register&#40;echo, spawn&#40;echo, loop, &#91;&#93;&#41;&#41;. &#160; print&#40;Term&#41; -&#62; echo ! &#123;print, Term&#125;. &#160; stop&#40;&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>一共两道题,第一题比较简单,只要注意一下(书上有提示)用内部格式{print, Term}这样,防止调用echo:print(stop)的时候会导致程序退出.上代码:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="erlang" style="font-family:monospace;"><span style="color: #014ea4;">-</span><span style="color: #5400b3;">module</span><span style="color: #109ab8;">&#40;</span>echo<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">export</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#91;</span>start<span style="color: #014ea4;">/</span><span style="color: #ff9600;">0</span><span style="color: #6bb810;">,</span> print<span style="color: #014ea4;">/</span><span style="color: #ff9600;">1</span><span style="color: #6bb810;">,</span> stop<span style="color: #014ea4;">/</span><span style="color: #ff9600;">0</span><span style="color: #6bb810;">,</span> loop<span style="color: #014ea4;">/</span><span style="color: #ff9600;">0</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">loop</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #186895;">receive</span>
    <span style="color: #109ab8;">&#123;</span>print<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Term</span><span style="color: #109ab8;">&#125;</span> <span style="color: #6bb810;">-&gt;</span>
      <span style="color: #ff4e18;">io</span>:<span style="color: #ff3c00;">format</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;~p~n&quot;</span><span style="color: #6bb810;">,</span><span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">Term</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
      <span style="color: #ff3c00;">loop</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">;</span>
    stop <span style="color: #6bb810;">-&gt;</span> <span style="color: #006600;">true</span>
  <span style="color: #186895;">end</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">start</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #fa6fff;">register</span><span style="color: #109ab8;">&#40;</span>echo<span style="color: #6bb810;">,</span> <span style="color: #fa6fff;">spawn</span><span style="color: #109ab8;">&#40;</span>echo<span style="color: #6bb810;">,</span> loop<span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">print</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Term</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #006600;">echo</span> <span style="color: #014ea4;">!</span> <span style="color: #109ab8;">&#123;</span>print<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Term</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">stop</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #006600;">echo</span> <span style="color: #014ea4;">!</span> stop<span style="color: #6bb810;">.</span></pre></td></tr></table></div>

<p>第二题我的代码就有点儿复杂了,有几个地方用到了receive来同步线程,另一个后来经过改进的方案是给proc多传了一个Head的参数,表示整个环的头部,之前的方案是register一个ring_head,但是这样的话在shell里面就只能执行一次ring:start了,而且会有内存泄露的问题,所以改掉了这部分.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="erlang" style="font-family:monospace;"><span style="color: #014ea4;">-</span><span style="color: #5400b3;">module</span><span style="color: #109ab8;">&#40;</span>ring<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">export</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#91;</span>start<span style="color: #014ea4;">/</span><span style="color: #ff9600;">3</span><span style="color: #6bb810;">,</span> proc<span style="color: #014ea4;">/</span><span style="color: #ff9600;">2</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">start</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">_NM</span><span style="color: #6bb810;">,</span> <span style="color: #ff9600;">0</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">_M</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #006600;">true</span><span style="color: #6bb810;">;</span>
<span style="color: #ff3c00;">start</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff9600;">0</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">_NP</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">_M</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #006600;">true</span><span style="color: #6bb810;">;</span>
<span style="color: #ff3c00;">start</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">NumMessage</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">NumProcess</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Message</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #45b3e6;">Next</span> <span style="color: #014ea4;">=</span> <span style="color: #fa6fff;">spawn</span><span style="color: #109ab8;">&#40;</span>ring<span style="color: #6bb810;">,</span> proc<span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">NumProcess</span> <span style="color: #014ea4;">-</span> <span style="color: #ff9600;">1</span><span style="color: #6bb810;">,</span> <span style="color: #fa6fff;">self</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
  <span style="color: #186895;">receive</span>
    crf <span style="color: #6bb810;">-&gt;</span> <span style="color: #006600;">true</span> <span style="color: #666666; font-style: italic;">% create ring finished</span>
  <span style="color: #186895;">end</span><span style="color: #6bb810;">,</span>
  <span style="color: #ff3c00;">sendMsg</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">NumMessage</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Message</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Next</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
  <span style="color: #45b3e6;">Next</span> <span style="color: #014ea4;">!</span> stop<span style="color: #6bb810;">,</span>
  <span style="color: #186895;">receive</span>
    stop <span style="color: #6bb810;">-&gt;</span> <span style="color: #006600;">true</span>
  <span style="color: #186895;">end</span><span style="color: #6bb810;">,</span>
  <span style="color: #ff4e18;">io</span>:<span style="color: #ff3c00;">format</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;~p~n&quot;</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #ff7800;">&quot;Main process terminated&quot;</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">proc</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff9600;">0</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Head</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #45b3e6;">Head</span> <span style="color: #014ea4;">!</span> crf<span style="color: #6bb810;">,</span>
  <span style="color: #ff3c00;">proc_loop</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Head</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">;</span>
<span style="color: #ff3c00;">proc</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">NumProcess</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Head</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #45b3e6;">Next</span> <span style="color: #014ea4;">=</span> <span style="color: #fa6fff;">spawn</span><span style="color: #109ab8;">&#40;</span>ring<span style="color: #6bb810;">,</span> proc<span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">NumProcess</span> <span style="color: #014ea4;">-</span> <span style="color: #ff9600;">1</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Head</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
  <span style="color: #ff3c00;">proc_loop</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Next</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">proc_loop</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">NextProcess</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #186895;">receive</span>
    <span style="color: #109ab8;">&#123;</span>msg<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Message</span><span style="color: #109ab8;">&#125;</span> <span style="color: #6bb810;">-&gt;</span>
      <span style="color: #ff4e18;">io</span>:<span style="color: #ff3c00;">format</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;This is proc: ~p, the msg is: ~p~n&quot;</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #fa6fff;">self</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Message</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
      <span style="color: #45b3e6;">NextProcess</span> <span style="color: #014ea4;">!</span> <span style="color: #109ab8;">&#123;</span>msg<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Message</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
      <span style="color: #ff3c00;">proc_loop</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">NextProcess</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">;</span>
    stop <span style="color: #6bb810;">-&gt;</span>
      <span style="color: #45b3e6;">NextProcess</span> <span style="color: #014ea4;">!</span> stop<span style="color: #6bb810;">,</span>
      <span style="color: #ff4e18;">io</span>:<span style="color: #ff3c00;">format</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;Process ~p terminated.~n&quot;</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #fa6fff;">self</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span>
  <span style="color: #186895;">end</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">sendMsg</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff9600;">0</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">_M</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">_P</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span> <span style="color: #006600;">true</span><span style="color: #6bb810;">;</span>
<span style="color: #ff3c00;">sendMsg</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">NumMessage</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Message</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Pid</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #45b3e6;">Pid</span> <span style="color: #014ea4;">!</span> <span style="color: #109ab8;">&#123;</span>msg<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Message</span><span style="color: #109ab8;">&#125;</span><span style="color: #6bb810;">,</span>
  <span style="color: #186895;">receive</span>
    <span style="color: #109ab8;">&#123;</span>msg<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Message</span><span style="color: #109ab8;">&#125;</span> <span style="color: #6bb810;">-&gt;</span>
      <span style="color: #ff4e18;">io</span>:<span style="color: #ff3c00;">format</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;~p rounds left.~n&quot;</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #45b3e6;">NumMessage</span> <span style="color: #014ea4;">-</span> <span style="color: #ff9600;">1</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
      <span style="color: #ff3c00;">sendMsg</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">NumMessage</span> <span style="color: #014ea4;">-</span> <span style="color: #ff9600;">1</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Message</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Pid</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">;</span>
    <span style="color: #45b3e6;">_</span> <span style="color: #6bb810;">-&gt;</span> <span style="color: #006600;">true</span>
  <span style="color: #186895;">end</span><span style="color: #6bb810;">.</span></pre></td></tr></table></div>

<p>最后是ring的测试:</p>

<div class="wp_syntax"><div class="code"><pre class="erlang" style="font-family:monospace;"><span style="color: #45b3e6;">AquarMBP</span>:<span style="color: #006600;">Ex4</span> aquarhead$ erl
<span style="color: #45b3e6;">Erlang</span> <span style="color: #45b3e6;">R14B01</span> <span style="color: #109ab8;">&#40;</span>erts<span style="color: #014ea4;">-</span>5<span style="color: #6bb810;">.</span>8<span style="color: #6bb810;">.</span>2<span style="color: #109ab8;">&#41;</span> <span style="color: #109ab8;">&#91;</span>source<span style="color: #109ab8;">&#93;</span> <span style="color: #109ab8;">&#91;</span>smp:<span style="color: #ff9600;">2</span>:<span style="color: #ff9600;">2</span><span style="color: #109ab8;">&#93;</span> <span style="color: #109ab8;">&#91;</span>rq:<span style="color: #ff9600;">2</span><span style="color: #109ab8;">&#93;</span> <span style="color: #109ab8;">&#91;</span>async<span style="color: #014ea4;">-</span>threads:<span style="color: #ff9600;">0</span><span style="color: #109ab8;">&#93;</span> <span style="color: #109ab8;">&#91;</span>hipe<span style="color: #109ab8;">&#93;</span> <span style="color: #109ab8;">&#91;</span>kernel<span style="color: #014ea4;">-</span>poll:<span style="color: #006600;">false</span><span style="color: #109ab8;">&#93;</span>
&nbsp;
<span style="color: #45b3e6;">Eshell</span> <span style="color: #45b3e6;">V5</span><span style="color: #6bb810;">.</span>8<span style="color: #6bb810;">.</span>2  <span style="color: #109ab8;">&#40;</span>abort with ^G<span style="color: #109ab8;">&#41;</span>
<span style="color: #ff9600;">1</span><span style="color: #014ea4;">&gt;</span> <span style="color: #ff3c00;">c</span><span style="color: #109ab8;">&#40;</span>ring<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
<span style="color: #109ab8;">&#123;</span>ok<span style="color: #6bb810;">,</span>ring<span style="color: #109ab8;">&#125;</span>
<span style="color: #ff9600;">2</span><span style="color: #014ea4;">&gt;</span> ring:<span style="color: #ff3c00;">start</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff9600;">2</span><span style="color: #6bb810;">,</span> <span style="color: #ff9600;">4</span><span style="color: #6bb810;">,</span> <span style="color: #ff7800;">&quot;test1&quot;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
<span style="color: #45b3e6;">This</span> is pro<span style="color: #ff4e18;">c</span>: <span style="color: #014ea4;">&lt;</span>0<span style="color: #6bb810;">.</span>38<span style="color: #6bb810;">.</span>0<span style="color: #014ea4;">&gt;</span><span style="color: #6bb810;">,</span> the msg is: <span style="color: #ff7800;">&quot;test1&quot;</span>
<span style="color: #45b3e6;">This</span> is pro<span style="color: #ff4e18;">c</span>: <span style="color: #014ea4;">&lt;</span>0<span style="color: #6bb810;">.</span>39<span style="color: #6bb810;">.</span>0<span style="color: #014ea4;">&gt;</span><span style="color: #6bb810;">,</span> the msg is: <span style="color: #ff7800;">&quot;test1&quot;</span>
<span style="color: #45b3e6;">This</span> is pro<span style="color: #ff4e18;">c</span>: <span style="color: #014ea4;">&lt;</span>0<span style="color: #6bb810;">.</span>40<span style="color: #6bb810;">.</span>0<span style="color: #014ea4;">&gt;</span><span style="color: #6bb810;">,</span> the msg is: <span style="color: #ff7800;">&quot;test1&quot;</span>
<span style="color: #45b3e6;">This</span> is pro<span style="color: #ff4e18;">c</span>: <span style="color: #014ea4;">&lt;</span>0<span style="color: #6bb810;">.</span>41<span style="color: #6bb810;">.</span>0<span style="color: #014ea4;">&gt;</span><span style="color: #6bb810;">,</span> the msg is: <span style="color: #ff7800;">&quot;test1&quot;</span>
<span style="color: #ff9600;">1</span> rounds left<span style="color: #6bb810;">.</span>
<span style="color: #45b3e6;">This</span> is pro<span style="color: #ff4e18;">c</span>: <span style="color: #014ea4;">&lt;</span>0<span style="color: #6bb810;">.</span>38<span style="color: #6bb810;">.</span>0<span style="color: #014ea4;">&gt;</span><span style="color: #6bb810;">,</span> the msg is: <span style="color: #ff7800;">&quot;test1&quot;</span>
<span style="color: #45b3e6;">This</span> is pro<span style="color: #ff4e18;">c</span>: <span style="color: #014ea4;">&lt;</span>0<span style="color: #6bb810;">.</span>39<span style="color: #6bb810;">.</span>0<span style="color: #014ea4;">&gt;</span><span style="color: #6bb810;">,</span> the msg is: <span style="color: #ff7800;">&quot;test1&quot;</span>
<span style="color: #45b3e6;">This</span> is pro<span style="color: #ff4e18;">c</span>: <span style="color: #014ea4;">&lt;</span>0<span style="color: #6bb810;">.</span>40<span style="color: #6bb810;">.</span>0<span style="color: #014ea4;">&gt;</span><span style="color: #6bb810;">,</span> the msg is: <span style="color: #ff7800;">&quot;test1&quot;</span>
<span style="color: #45b3e6;">This</span> is pro<span style="color: #ff4e18;">c</span>: <span style="color: #014ea4;">&lt;</span>0<span style="color: #6bb810;">.</span>41<span style="color: #6bb810;">.</span>0<span style="color: #014ea4;">&gt;</span><span style="color: #6bb810;">,</span> the msg is: <span style="color: #ff7800;">&quot;test1&quot;</span>
<span style="color: #ff9600;">0</span> rounds left<span style="color: #6bb810;">.</span>
<span style="color: #45b3e6;">Process</span> <span style="color: #014ea4;">&lt;</span>0<span style="color: #6bb810;">.</span>38<span style="color: #6bb810;">.</span>0<span style="color: #014ea4;">&gt;</span> terminated<span style="color: #6bb810;">.</span>
<span style="color: #45b3e6;">Process</span> <span style="color: #014ea4;">&lt;</span>0<span style="color: #6bb810;">.</span>39<span style="color: #6bb810;">.</span>0<span style="color: #014ea4;">&gt;</span> terminated<span style="color: #6bb810;">.</span>
<span style="color: #45b3e6;">Process</span> <span style="color: #014ea4;">&lt;</span>0<span style="color: #6bb810;">.</span>40<span style="color: #6bb810;">.</span>0<span style="color: #014ea4;">&gt;</span> terminated<span style="color: #6bb810;">.</span>
<span style="color: #45b3e6;">Process</span> <span style="color: #014ea4;">&lt;</span>0<span style="color: #6bb810;">.</span>41<span style="color: #6bb810;">.</span>0<span style="color: #014ea4;">&gt;</span> terminated<span style="color: #6bb810;">.</span>
<span style="color: #ff7800;">&quot;Main process terminated&quot;</span>
ok
<span style="color: #ff9600;">3</span><span style="color: #014ea4;">&gt;</span> ring:<span style="color: #ff3c00;">start</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff9600;">1</span><span style="color: #6bb810;">,</span> <span style="color: #ff9600;">6</span><span style="color: #6bb810;">,</span> <span style="color: #ff7800;">&quot;test2&quot;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
<span style="color: #45b3e6;">This</span> is pro<span style="color: #ff4e18;">c</span>: <span style="color: #014ea4;">&lt;</span>0<span style="color: #6bb810;">.</span>43<span style="color: #6bb810;">.</span>0<span style="color: #014ea4;">&gt;</span><span style="color: #6bb810;">,</span> the msg is: <span style="color: #ff7800;">&quot;test2&quot;</span>
<span style="color: #45b3e6;">This</span> is pro<span style="color: #ff4e18;">c</span>: <span style="color: #014ea4;">&lt;</span>0<span style="color: #6bb810;">.</span>44<span style="color: #6bb810;">.</span>0<span style="color: #014ea4;">&gt;</span><span style="color: #6bb810;">,</span> the msg is: <span style="color: #ff7800;">&quot;test2&quot;</span>
<span style="color: #45b3e6;">This</span> is pro<span style="color: #ff4e18;">c</span>: <span style="color: #014ea4;">&lt;</span>0<span style="color: #6bb810;">.</span>45<span style="color: #6bb810;">.</span>0<span style="color: #014ea4;">&gt;</span><span style="color: #6bb810;">,</span> the msg is: <span style="color: #ff7800;">&quot;test2&quot;</span>
<span style="color: #45b3e6;">This</span> is pro<span style="color: #ff4e18;">c</span>: <span style="color: #014ea4;">&lt;</span>0<span style="color: #6bb810;">.</span>46<span style="color: #6bb810;">.</span>0<span style="color: #014ea4;">&gt;</span><span style="color: #6bb810;">,</span> the msg is: <span style="color: #ff7800;">&quot;test2&quot;</span>
<span style="color: #45b3e6;">This</span> is pro<span style="color: #ff4e18;">c</span>: <span style="color: #014ea4;">&lt;</span>0<span style="color: #6bb810;">.</span>47<span style="color: #6bb810;">.</span>0<span style="color: #014ea4;">&gt;</span><span style="color: #6bb810;">,</span> the msg is: <span style="color: #ff7800;">&quot;test2&quot;</span>
<span style="color: #45b3e6;">This</span> is pro<span style="color: #ff4e18;">c</span>: <span style="color: #014ea4;">&lt;</span>0<span style="color: #6bb810;">.</span>48<span style="color: #6bb810;">.</span>0<span style="color: #014ea4;">&gt;</span><span style="color: #6bb810;">,</span> the msg is: <span style="color: #ff7800;">&quot;test2&quot;</span>
<span style="color: #ff9600;">0</span> rounds left<span style="color: #6bb810;">.</span>
<span style="color: #45b3e6;">Process</span> <span style="color: #014ea4;">&lt;</span>0<span style="color: #6bb810;">.</span>43<span style="color: #6bb810;">.</span>0<span style="color: #014ea4;">&gt;</span> terminated<span style="color: #6bb810;">.</span>
<span style="color: #45b3e6;">Process</span> <span style="color: #014ea4;">&lt;</span>0<span style="color: #6bb810;">.</span>44<span style="color: #6bb810;">.</span>0<span style="color: #014ea4;">&gt;</span> terminated<span style="color: #6bb810;">.</span>
<span style="color: #45b3e6;">Process</span> <span style="color: #014ea4;">&lt;</span>0<span style="color: #6bb810;">.</span>45<span style="color: #6bb810;">.</span>0<span style="color: #014ea4;">&gt;</span> terminated<span style="color: #6bb810;">.</span>
<span style="color: #45b3e6;">Process</span> <span style="color: #014ea4;">&lt;</span>0<span style="color: #6bb810;">.</span>46<span style="color: #6bb810;">.</span>0<span style="color: #014ea4;">&gt;</span> terminated<span style="color: #6bb810;">.</span>
<span style="color: #45b3e6;">Process</span> <span style="color: #014ea4;">&lt;</span>0<span style="color: #6bb810;">.</span>47<span style="color: #6bb810;">.</span>0<span style="color: #014ea4;">&gt;</span> terminated<span style="color: #6bb810;">.</span>
<span style="color: #45b3e6;">Process</span> <span style="color: #014ea4;">&lt;</span>0<span style="color: #6bb810;">.</span>48<span style="color: #6bb810;">.</span>0<span style="color: #014ea4;">&gt;</span> terminated<span style="color: #6bb810;">.</span>
<span style="color: #ff7800;">&quot;Main process terminated&quot;</span>
ok</pre></div></div>


	Tags:<a href="http://www.aquarhead.com/tag/erlang/" title="Erlang" rel="tag">Erlang</a>,<a href="http://www.aquarhead.com/tag/erlang-programming/" title="Erlang Programming" rel="tag">Erlang Programming</a>,<a href="http://www.aquarhead.com/tag/process/" title="Process" rel="tag">Process</a>,<a href="http://www.aquarhead.com/tag/%e4%b9%a0%e9%a2%98/" title="习题" rel="tag">习题</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.aquarhead.com/2010/12/erlang-programming-exercise-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>KMP算法的一个C语言实现</title>
		<link>http://www.aquarhead.com/2010/12/kmp-algorithm-in-c/</link>
		<comments>http://www.aquarhead.com/2010/12/kmp-algorithm-in-c/#comments</comments>
		<pubDate>Fri, 24 Dec 2010 03:16:40 +0000</pubDate>
		<dc:creator>AquarHEAD</dc:creator>
				<category><![CDATA[Code Fortress]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[KMP]]></category>

		<guid isPermaLink="false">http://www.aquarhead.com/?p=1010</guid>
		<description><![CDATA[KMP算法 -> 传送门 代码有误,调试中= = 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 #include &#60;stdlib.h&#62; #include &#60;stdio.h&#62; #include &#60;string.h&#62; &#160; int main&#40;&#41; &#123; char a&#91;&#93; = &#34;This is a test for the --kmp algori--kmpthm.&#34;; [...]]]></description>
			<content:encoded><![CDATA[<p>KMP算法 -> <a href="http://www.matrix67.com/blog/archives/115">传送门</a></p>
<p><del datetime="2010-12-24T08:03:15+00:00">代码有误,调试中= =</del></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdlib.h&gt;</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;string.h&gt;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">char</span> a<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;This is a test for the --kmp algori--kmpthm.&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">char</span> b<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;--kmp&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">6</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">,</span> j<span style="color: #339933;">,</span> n <span style="color: #339933;">=</span> strlen<span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> m <span style="color: #339933;">=</span> strlen<span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    p<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>m<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>j <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> b<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> j <span style="color: #339933;">=</span> p<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> b<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> j<span style="color: #339933;">++;</span>
        p<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> j<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>n<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>j <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> a<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> j <span style="color: #339933;">=</span> p<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> a<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> j<span style="color: #339933;">++;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">==</span> m<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;occur: %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            j <span style="color: #339933;">=</span> p<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


	Tags:<a href="http://www.aquarhead.com/tag/c/" title="C++" rel="tag">C++</a>,<a href="http://www.aquarhead.com/tag/kmp/" title="KMP" rel="tag">KMP</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.aquarhead.com/2010/12/kmp-algorithm-in-c/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Erlang Programming -&gt; Exercise 2-3</title>
		<link>http://www.aquarhead.com/2010/12/erlang-programming-exercise-2-3/</link>
		<comments>http://www.aquarhead.com/2010/12/erlang-programming-exercise-2-3/#comments</comments>
		<pubDate>Fri, 10 Dec 2010 12:39:34 +0000</pubDate>
		<dc:creator>AquarHEAD</dc:creator>
				<category><![CDATA[Code Fortress]]></category>
		<category><![CDATA[Erlang]]></category>
		<category><![CDATA[Erlang Programming]]></category>
		<category><![CDATA[习题]]></category>

		<guid isPermaLink="false">http://www.aquarhead.com/?p=1001</guid>
		<description><![CDATA[上周末逛上海书城时买了一本Erlang Programming这几天有时间了就看一看学一点儿,昨晚躺床上看完了第二章最后的几页,刚才做了一下习题,把我的代码发上来献丑了呵呵. 题目就是实现not和and的功能,最后还有一个nand我理解就是not(and)这样... 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 -module&#40;bool&#41;. -export&#40;&#91;b_not/1, b_and/2, b_nand/2&#93;&#41;. &#160; b_not&#40;true&#41; -&#62; false; b_not&#40;false&#41; -&#62; true. &#160; b_and&#40;true, B&#41; -&#62; B; b_and&#40;false, _B&#41; -&#62; false. &#160; b_nand&#40;A, B&#41; -&#62; b_not&#40;b_and&#40;A,B&#41;&#41;. 下面是在shell里面使用的效果. P.S. cd命令只识别'/'作为分隔符,即使是在Windows上. 1 2 3 4 5 6 7 8 9 [...]]]></description>
			<content:encoded><![CDATA[<p>上周末逛上海书城时买了一本Erlang Programming这几天有时间了就看一看学一点儿,昨晚躺床上看完了第二章最后的几页,刚才做了一下习题,把我的代码发上来献丑了呵呵.</p>
<p>题目就是实现not和and的功能,最后还有一个nand我理解就是not(and)这样...</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="erlang" style="font-family:monospace;"><span style="color: #014ea4;">-</span><span style="color: #5400b3;">module</span><span style="color: #109ab8;">&#40;</span>bool<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
<span style="color: #014ea4;">-</span><span style="color: #5400b3;">export</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#91;</span>b_not<span style="color: #014ea4;">/</span><span style="color: #ff9600;">1</span><span style="color: #6bb810;">,</span> b_and<span style="color: #014ea4;">/</span><span style="color: #ff9600;">2</span><span style="color: #6bb810;">,</span> b_nand<span style="color: #014ea4;">/</span><span style="color: #ff9600;">2</span><span style="color: #109ab8;">&#93;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">b_not</span><span style="color: #109ab8;">&#40;</span>true<span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #006600;">false</span><span style="color: #6bb810;">;</span>
<span style="color: #ff3c00;">b_not</span><span style="color: #109ab8;">&#40;</span>false<span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #006600;">true</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">b_and</span><span style="color: #109ab8;">&#40;</span>true<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">B</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #45b3e6;">B</span><span style="color: #6bb810;">;</span>
<span style="color: #ff3c00;">b_and</span><span style="color: #109ab8;">&#40;</span>false<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">_B</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #006600;">false</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #ff3c00;">b_nand</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">A</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">B</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
  <span style="color: #ff3c00;">b_not</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff3c00;">b_and</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">A</span><span style="color: #6bb810;">,</span><span style="color: #45b3e6;">B</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span></pre></td></tr></table></div>

<p>下面是在shell里面使用的效果.<br />
P.S. cd命令只识别'/'作为分隔符,即使是在Windows上.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="erlang" style="font-family:monospace;"><span style="color: #ff9600;">20</span><span style="color: #014ea4;">&gt;</span> <span style="color: #ff3c00;">c</span><span style="color: #109ab8;">&#40;</span>bool<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
<span style="color: #109ab8;">&#123;</span>ok<span style="color: #6bb810;">,</span>bool<span style="color: #109ab8;">&#125;</span>
<span style="color: #ff9600;">21</span><span style="color: #014ea4;">&gt;</span> bool:<span style="color: #ff3c00;">b_not</span><span style="color: #109ab8;">&#40;</span>false<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
true
<span style="color: #ff9600;">22</span><span style="color: #014ea4;">&gt;</span> bool:<span style="color: #ff3c00;">b_not</span><span style="color: #109ab8;">&#40;</span>true<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span> 
false
<span style="color: #ff9600;">23</span><span style="color: #014ea4;">&gt;</span> bool:<span style="color: #ff3c00;">b_and</span><span style="color: #109ab8;">&#40;</span>false<span style="color: #6bb810;">,</span> true<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
false
<span style="color: #ff9600;">24</span><span style="color: #014ea4;">&gt;</span> bool:<span style="color: #ff3c00;">b_and</span><span style="color: #109ab8;">&#40;</span>true<span style="color: #6bb810;">,</span> false<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
false
<span style="color: #ff9600;">25</span><span style="color: #014ea4;">&gt;</span> bool:<span style="color: #ff3c00;">b_and</span><span style="color: #109ab8;">&#40;</span>true<span style="color: #6bb810;">,</span> true<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span> 
true
<span style="color: #ff9600;">26</span><span style="color: #014ea4;">&gt;</span> bool:<span style="color: #ff3c00;">b_and</span><span style="color: #109ab8;">&#40;</span>false<span style="color: #6bb810;">,</span> false<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
false
<span style="color: #ff9600;">27</span><span style="color: #014ea4;">&gt;</span> bool:<span style="color: #ff3c00;">b_nand</span><span style="color: #109ab8;">&#40;</span>false<span style="color: #6bb810;">,</span> false<span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
true</pre></td></tr></table></div>


	Tags:<a href="http://www.aquarhead.com/tag/erlang/" title="Erlang" rel="tag">Erlang</a>,<a href="http://www.aquarhead.com/tag/erlang-programming/" title="Erlang Programming" rel="tag">Erlang Programming</a>,<a href="http://www.aquarhead.com/tag/%e4%b9%a0%e9%a2%98/" title="习题" rel="tag">习题</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.aquarhead.com/2010/12/erlang-programming-exercise-2-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>pthread笔记</title>
		<link>http://www.aquarhead.com/2010/12/notes-on-pthread/</link>
		<comments>http://www.aquarhead.com/2010/12/notes-on-pthread/#comments</comments>
		<pubDate>Wed, 08 Dec 2010 14:24:12 +0000</pubDate>
		<dc:creator>AquarHEAD</dc:creator>
				<category><![CDATA[Code Fortress]]></category>
		<category><![CDATA[pthread]]></category>
		<category><![CDATA[Yo2Lua]]></category>
		<category><![CDATA[笔记]]></category>

		<guid isPermaLink="false">http://www.aquarhead.com/?p=991</guid>
		<description><![CDATA[还是推荐一个网页,另外我还读了UNIX环境高级编程中有关线程的部分,总体感觉多线程本身并没有什么太多的问题,互斥和条件变量可以很好地解决线程同步的问题,我在做试验的时候唯一遇到的问题只存在于执行顺序上,如果在写代码的时候比较注意的话应该没有什么太大的问题. 进入正题,pthread是实现POSIX标准的一个线程库,在Linux上编译的时候只要加上-lpthread的参数就OK了. 线程可以看作是一种轻量级的进程,都用来并发地执行一段指令流,只是线程之间能够很方便地共享内存.在上面那个网页中有一个通过pthread创建线程与通过fork创建进程的开销对比,可以明显地看出线程的轻量.线程的一个用处(也是我学习的主要原因)是避免大开销的I/O操作时不能高效利用CPU资源.使用pthread需要包含对应的库: #include &#60;pthread.h&#62; 创建线程使用pthread_create函数,定义如下: int pthread_create &#40;pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void * &#40;*start_rtn&#41; &#40;void&#41;, void *restrict arg&#41;; P.S. restrict修饰符 --> 传送门 第一个参数是创建的线程的线程ID,第二个参数用来设置线程属性(可以用NULL来代替使用默认属性),接下来两个参数是线程的入口函数地址和参数,注意线程的入口函数应该遵循如下的声明: void * func &#40;void *arg&#41;; 线程可以用pthread_self(void)函数来获取自己的线程ID. 线程的终止可以是执行完毕所有指令之后自行终止,也可以显示地调用exit, _exit等函数来强制终止.正常地方法是使用pthread_exit函数来退出,见一下函数声明: void pthread_exit &#40;void *rval_ptr&#41;; &#160; int pthread_join &#40;pthread_t thread, void **rval_ptr&#41;; 第二个函数pthread_join可以阻塞其调用线程,知道thread指定的那个线程终止后才能继续,并且可以获得pthread_exit抛出的数据.该函数可以用来强制线程的执行顺序. 最后是两个线程同步的方法,互斥体(或称互斥量, Mutex)和条件变量(Condition Variable).两个概念都很简单,只是要求写代码的时候要注意,因为只要有一处没有调用对应的函数就可能导致整个程序的混乱.互斥体本质上是一种锁机制,也就是在获取或修改变量的时候要先加锁,完成操作后再进行解锁,但是锁本身并不绑定,相当于一种约定. 互斥体的类型都是pthread_mutex_t,比较简单的初始化方法是直接用常量PTHREAD_MUTEX_INITIALIZER赋值.另外也可以用pthread_mutex_init给互斥体附加一些属性,最后应当用pthread_mutex_destroy清理互斥体. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; [...]]]></description>
			<content:encoded><![CDATA[<p>还是推荐<a href="https://computing.llnl.gov/tutorials/pthreads/">一个网页</a>,另外我还读了<a href="http://book.douban.com/subject/1788421/">UNIX环境高级编程</a>中有关线程的部分,总体感觉多线程本身并没有什么太多的问题,互斥和条件变量可以很好地解决线程同步的问题,我在做试验的时候唯一遇到的问题只存在于执行顺序上,如果在写代码的时候比较注意的话应该没有什么太大的问题.</p>
<p>进入正题,pthread是实现POSIX标准的一个线程库,在Linux上编译的时候只要加上-lpthread的参数就OK了.</p>
<p>线程可以看作是一种轻量级的进程,都用来并发地执行一段指令流,只是线程之间能够很方便地共享内存.在上面那个网页中有一个通过pthread创建线程与通过fork创建进程的开销对比,可以明显地看出线程的轻量.线程的一个用处(也是我学习的主要原因)是避免大开销的I/O操作时不能高效利用CPU资源.使用pthread需要包含对应的库:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;pthread.h&gt;</span></pre></div></div>

<p>创建线程使用pthread_create函数,定义如下:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> pthread_create <span style="color: #009900;">&#40;</span>pthread_t <span style="color: #339933;">*</span>restrict tidp<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> pthread_attr_t <span style="color: #339933;">*</span>restrict attr<span style="color: #339933;">,</span> 
                       <span style="color: #993333;">void</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">*</span>start_rtn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #993333;">void</span> <span style="color: #339933;">*</span>restrict arg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>P.S. restrict修饰符 --> <a href="http://en.wikipedia.org/wiki/Restrict">传送门</a><br />
第一个参数是创建的线程的线程ID,第二个参数用来设置线程属性(可以用NULL来代替使用默认属性),接下来两个参数是线程的入口函数地址和参数,注意线程的入口函数应该遵循如下的声明:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> <span style="color: #339933;">*</span> func <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span>arg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>线程可以用pthread_self(void)函数来获取自己的线程ID.</p>
<p>线程的终止可以是执行完毕所有指令之后自行终止,也可以显示地调用exit, _exit等函数来强制终止.正常地方法是使用pthread_exit函数来退出,见一下函数声明:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> pthread_exit <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span> <span style="color: #339933;">*</span>rval_ptr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">int</span> pthread_join <span style="color: #009900;">&#40;</span>pthread_t thread<span style="color: #339933;">,</span> <span style="color: #993333;">void</span> <span style="color: #339933;">**</span>rval_ptr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>第二个函数pthread_join可以阻塞其调用线程,知道thread指定的那个线程终止后才能继续,并且可以获得pthread_exit抛出的数据.该函数可以用来强制线程的执行顺序.</p>
<p>最后是两个线程同步的方法,互斥体(或称互斥量, Mutex)和条件变量(Condition Variable).两个概念都很简单,只是要求写代码的时候要注意,因为只要有一处没有调用对应的函数就可能导致整个程序的混乱.互斥体本质上是一种锁机制,也就是在获取或修改变量的时候要先加锁,完成操作后再进行解锁,但是锁本身并不绑定,相当于一种约定.</p>
<p>互斥体的类型都是pthread_mutex_t,比较简单的初始化方法是直接用常量PTHREAD_MUTEX_INITIALIZER赋值.另外也可以用pthread_mutex_init给互斥体附加一些属性,最后应当用pthread_mutex_destroy清理互斥体.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">pthread_mutex_t mutex <span style="color: #339933;">=</span> PTHREAD_MUTEX_INITIALIZER<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">int</span> pthread_mutex_init <span style="color: #009900;">&#40;</span>pthread_mutex_t <span style="color: #339933;">*</span>restrict mutex<span style="color: #339933;">,</span> <span style="color: #993333;">const</span> pthread_mutexattr_t <span style="color: #339933;">*</span>restrict attr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">int</span> pthread_mutex_destroy <span style="color: #009900;">&#40;</span>pthread_mutex_t <span style="color: #339933;">*</span>mutex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>使用互斥体很简单,通常用一个互斥体来保护某一个变量防止多个线程同时访问的时候造成的问题,所以只要在修改对应的变量之前获取并锁住对应的互斥体,使用完后再解锁就可以了,POSIX规范可以保证多个线程同时请求一个互斥体时只有一个线程可以获得这个互斥体,而其他线程会阻塞,如果不想阻塞可以用trylock,具体函数如下:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> pthread_mutex_lock <span style="color: #009900;">&#40;</span>pthread_mutex_t <span style="color: #339933;">*</span>mutex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">int</span> pthread_mutex_trylock <span style="color: #009900;">&#40;</span>pthread_mutex_t <span style="color: #339933;">*</span>mutex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">int</span> pthread_mutex_unlock <span style="color: #009900;">&#40;</span>pthread_mutex_t <span style="color: #339933;">*</span>mutex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>P.S.注意避免死锁, "只有在一个线程试图以与另一个线程相反的顺序锁住互斥量时,才可能出现死锁".</p>
<p>最后说一下条件变量及其用法,如果我们希望线程在满足一定条件的情况下才执行,那么就应当使用条件变量,条件变量必须和互斥体(突然发现应当说 互斥锁 更好一点- -||)一起使用,相关的函数包括:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// 悲剧地发现忘记把书带回来了... 凑合着看吧</span>
<span style="color: #993333;">int</span>   pthread_cond_broadcast<span style="color: #009900;">&#40;</span>pthread_cond_t <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">int</span>   pthread_cond_destroy<span style="color: #009900;">&#40;</span>pthread_cond_t <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">int</span>   pthread_cond_init<span style="color: #009900;">&#40;</span>pthread_cond_t <span style="color: #339933;">*,</span> <span style="color: #993333;">const</span> pthread_condattr_t <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">int</span>   pthread_cond_signal<span style="color: #009900;">&#40;</span>pthread_cond_t <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">int</span>   pthread_cond_timedwait<span style="color: #009900;">&#40;</span>pthread_cond_t <span style="color: #339933;">*,</span> 
          pthread_mutex_t <span style="color: #339933;">*,</span> <span style="color: #993333;">const</span> <span style="color: #993333;">struct</span> timespec <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">int</span>   pthread_cond_wait<span style="color: #009900;">&#40;</span>pthread_cond_t <span style="color: #339933;">*,</span> pthread_mutex_t <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>首先初始化一个条件变量,同样可以直接用PTHREAD_COND_INITIALIZER赋值来使用默认值,需要根据条件激活的线程应当使用wait或者timedwait来阻塞并等待相应的信号,注意(应当)在wait之前用lock来锁住相应的互斥体,之后再解锁(老大说他不上锁也可以正常运行,不过资料上写着wait会主动地解锁,暂时不太清楚为什么).其他线程可以用broadcast或者signal来发出信号,前者能够唤醒所有在等待信号的线程,而后者会只唤醒其中的一个.</p>
<p>具体的样例在文首提到的那个网页里都有的.</p>
<p>最后,Komodo Edit是个蛮好的编辑器,有一个疑问是Songbird(我记得)还有挺多程序都和Firefox一样用xpi格式的扩展,难道有什么框架作为背后的支撑吗?</p>

	Tags:<a href="http://www.aquarhead.com/tag/pthread/" title="pthread" rel="tag">pthread</a>,<a href="http://www.aquarhead.com/tag/yo2lua/" title="Yo2Lua" rel="tag">Yo2Lua</a>,<a href="http://www.aquarhead.com/tag/%e7%ac%94%e8%ae%b0/" title="笔记" rel="tag">笔记</a><br />
]]></content:encoded>
			<wfw:commentRss>http://www.aquarhead.com/2010/12/notes-on-pthread/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

