blog/content/post/2009/11/12/2009-11-12-00001268.md

53 lines
3.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: リストの n 番目を取り出す
author: kazu634
date: 2009-11-12
wordtwit_post_info:
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4927;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- Emacs
- Lisp
---
<div class="section">
<p>
Emacs Lisp でリストの n 番目を取り出す命令がありました。リストを配列みたいに使う時には nth というのがあるみたいです。
</p>
<dl>
<dt>
nth
</dt>
<dd>
(nth index list)。indexは0から始まる。
</dd>
</dl>
<p>
使ってみました。今回は「_」区切りの文字列を分割してみます。
</p>
<pre class="syntax-highlight">
<span class="synSpecial">(</span><span class="synStatement">defun</span> generate_candidate <span class="synSpecial">(</span>str<span class="synSpecial">)</span>
<span class="synSpecial">(</span><span class="synStatement">let*</span>
<span class="synSpecial">((</span>str_list <span class="synSpecial">(</span>split-string str <span class="synConstant">&#34;_&#34;</span><span class="synSpecial">))</span> <span class="synComment">;; 引数として与えられたものを「_」で区切ってリストに</span>
<span class="synSpecial">(</span>site <span class="synSpecial">(</span><span class="synStatement">nth</span> <span class="synConstant"></span> str_list<span class="synSpecial">))</span> <span class="synComment">;; リストの0番目をローカル変数「site」に代入。以下同じ。</span>
<span class="synSpecial">(</span>shopname <span class="synSpecial">(</span><span class="synStatement">nth</span> <span class="synConstant">1</span> str_list<span class="synSpecial">))</span>
<span class="synSpecial">(</span>address <span class="synSpecial">(</span><span class="synStatement">nth</span> <span class="synConstant">2</span> str_list<span class="synSpecial">))</span>
<span class="synSpecial">(</span>url <span class="synSpecial">(</span><span class="synStatement">nth</span> <span class="synConstant">3</span> str_list<span class="synSpecial">))</span>
<span class="synSpecial">(</span>tel <span class="synSpecial">(</span><span class="synStatement">nth</span> <span class="synConstant">4</span> str_list<span class="synSpecial">)))</span>
<span class="synSpecial">(</span>message <span class="synSpecial">(</span><span class="synStatement">format</span> <span class="synConstant">&#34;[%s] %s&#34;</span> site shopname<span class="synSpecial">))))</span>
<span class="synSpecial">(</span>generate_candidate
<span class="synConstant">&#34;Hotpepper_赤ちり亭 大森店_東京都大田区大森北1-3-7サファイア北ビル1_http://www.hotpepper.jp/strJ000030775/?vos=nhppalsa000016_03-5753-0399&#34;</span><span class="synSpecial">)</span>
</pre>
<p>
こうすることで、「[Hotpepper] 赤ちり亭 大森店」と表示されます。
</p>
<p>
こいつを使って、anythingのソースを作ってみるぞ
</p>
</div>