2008-04-01から1ヶ月間の記事一覧

Ex4.4 The Not Looking after You Don't Leap Problem. その2

ヒントの通りにachieveに引数を追加します。これをappropriate-opsからappropriate-pまで伝播させます。 appropriate-pでいずれかのprotected-goalが削除される場合、適用出来ない様にします。 achieveはachieve-eachから呼ばれているので、achieve-eachも修…

Ex4.4 The Not Looking after You Don't Leap Problem. その1

4章で定義してGPSは最初に見つけたパスでゴールを達成しようとするので、別のオペレータの組み合わせではゴールが達成可能でもそっちは参照しない問題があります。 これを修正するExerciseです。 ;; Exercise 4.4[h] The Not Looking after You Don't Leap …

突発性CL勉強会1回 SLIMEについて

突発性CL勉強会でSLIMEについて勉強。 id:g000001さん、id:mokeheheさん、NANRIさんとid:tszの4人で実施。 新宿大ガード横店は改装工事をしていたので、近くの別店に移動しました。 新宿のルノアールで7時間実施。SLIMEの話題自体は2〜3時間で終わりましたが…

EX4.3 GPSを「うっかり目標達成」に対応 その1

4章で定義したGPSは複数の目標を指定した場合、ある目標達成によって他の目標が部分的に達成できる場合をうまく扱っていない。これを修正する問題。まずは、その様なシチュエーションをオペレータのセットで定義する。 ;; Exercise 4.3[h] GPS does not reco…

4章の問題

PAIPも4章からは、結構規模の大きいプログラムを扱うようになってきたのでどうしようか模索中。 日記に貼れる大きさじゃ無くなってきたなぁと。 取りあえず勉強もかねて、3章までに定義した関数と4章で定義したGPSをdefpackage化してみる。

Ex4.2 順列の生成

リストを取って、その全ての順列を生成する関数を定義する。 ;; Exercise 4.2[m] Write a function that generates all permutations of its input. (defun map/rest (func list &optional (acc nil) (results nil)) (if (null list) results (map/rest func…

Ex4.1 formatの問題2

PAIPも4章に入るといよいよAIの内容に入ります。4章ではExerciseは章末にまとまっています。Ex4.1は章中で定義した、デバッグ用の表示関数dbgを formatディレクティブだけで表現してみようというモノです。章中の定義は以下のように行ってました。 (defvar…

EX3.13 format

formatを使って、単語のリストを取り文として表示する。 その際、 formatは1度だけ呼び出す 先頭の単語の、先頭の文字は大文字に、それ以外は小文字にする。 最後の単語の後ろにはピリオドを打つ を行う。リファレンスを見たけど複雑過ぎてわからない。~@( …

Ex3.10 lcmとnreconcは何をする関数?

lcmとnreconcの機能をマニュアルを使って調べる問題。 今更ながらslimeとHyperSpecがあると便利。hyperspec-lookupをキーバインドしておくとなお便利。 (global-set-key "\C-cH" 'hyperspec-lookup) 調べた結果は、lcm: 最小公倍数を求める関数 nreconc: rev…

Ex3.9 reduceを使ってlengthを定義

reduceを使って、リストの長さを求める問題です。 SBCLの場合ignoreをつけないと、コンパイラに文句を言われました。 ;;; Exercise 3.9 [m] White a version of length using the function ;;; reduce. (defun my-length (list) "リストの長さを求める。redu…

Ex3.11 キーと値と連想リストを取って、キーと値のペアを含む連想リストを返す関数の名前は?

3.10に続き、調べ物問題。答えは、acons。 ;;; Exercise 3.11 [m] There is a built-in Common Lisp function that, ;;; given a key, a value, and an association list, returns a new ;;; association list that is extended to include the key/value ;;…

Ex3.8 もしrightmostのkeywordパラメタの値が有効だったら

Ex3.8のちょっと前で定義したfind-allをKCLのパラメタバグに対応する問題。前で行ったfind-allの定義は以下の通り。 (defun find-all (item sequence &rest keyword-args &key (test #'eql) test-not &allow-other-keys) "Find all those elements of sequen…

Ex3.7 キーワードパラメタの優先順位

;;; Exercise 3.7 [s] Why do you think the leftmost of two keys is the ;;; one that counts, rather than the rightmost.同じキーのキーワードパラメタが複数個指定された場合、左端のキーの値が有効となる理由を問う問題。rest パラメタの値に、キーワ…

Ex3.6 スペシャル変数と通常の変数

;;; Exercise 3.6 [s] Given the following initialization for the ;;; lexical variable a and the special variable *b*, what will be the ;;; value of the let form? (setf a 'global-a) (defvar *b* 'global-b) (defun fn () *b*) (let ((a 'local-a)…

Ex3.5 20の質問

データ構造をプログラムから書き換える問題。 推論に失敗する度に、ツリーが拡張されていく。 ;;; Exercise 3.5 [h] (Exercise in a altering structure.) Write a ;;; program that will play the role of the guesser in the game Twenty ;;; Questions. T…

Ex3.4 リストを表示

printの様な関数を作成する。ドット対表記が必要ないところは通常のリスト表記で表示する。結構力業。 ;;; Exercise 3.4 [m] Write a function that, like the regular print ;;; function, will print an expression in dotted pair notation when ;;; nece…

Ex3.3 ドット対でリストを表示

問題ではprincを使えと書いてあるけど、writeで実装した。 せっかくなので作成する関数も元のリストをconsし直して返す様にしてみた。 ;;; Exercise 3.3 [m] Write a function that will print an expression in ;;; dotted pair noteation. Use the bulit-i…

Ex3.1 let*をlambda式に書き換える。Ex3.2 cons関数を特別なケースと見なせる関数は?

;;; Exercise 3.1 [m] Show a lambda expression that is equivalent to ;;; the above let* expression. You may need more than one lambda. (let* ((x 10) (y (+ x 20))) (list x y)) (funcall #'(lambda (x) (funcall #'(lambda (y) (list x y)) (+ x 20…

Ex2.4 combine-allをcross-productで再定義

引数に指定された複数のリストから要素の全ての組み合わせを選択し、各組み合わせに対し、関数fnを呼び出した結果をリストとして返す関数cross-productを定義する。その結果を使ってcombine-allを再定義する。 ;;; Exercise 2.4 [m] One way of describing c…

Ex2.3 英語以外の文法でgenerate

r5rsの内容をちょっとコピーしてみる。 ただし、実行するとスタックオーバーフローする。 ;;; Exercise 2.3 [h] Write a trivial grammar for some other language. This can be a ;;; natural language other than English, or perhaps a subset of compute…

Ex2.1 Condを使い、かつ、Rewritesを2度呼ばない方法

未定義の関数は、本家サイトのソース公開ページから探せるはず。章の中でcondを使って定義したgenerateがある。 (defun generate (phrase) "Generate a random sentence or phrase" (cond ((listp phrase) (mappend #'generate phrase)) ((rewrites phrase) …

Ex2.2 終端記号と非終端記号を明に区別するgenerate

以前のgenerateは、rewriteの戻り値を分岐条件としていたけど、それだと分かりにくいよね、ちゃんと述語を作ろう。という問題。PAIPの答えでは、non-terminal-pを定義していたので、自分はterminal-symbol-pを定義して解いてみる。当然、あまり変わらない。 …

Ex1.5 ベクトルの内積

ベクトルの内積を求める問題。 applyとmapcarですっきり定義できた。 ;;; exercise 1.5 [m] Write a function to compute the dot product of two sequences ;;; of numbers, represented as lists. The dot product is computed by multiplying ;;; corresp…

Ex1.4 atomの出現回数

PAIPの解答そのまま。 PAIPの解答を見てcondのt節の後の式は改行しないで書く慣習であることに気づく。 ;;; exercise 1.4 [m] Write a function that counts the number of times an expression ;;; occurs anywarere whthin another expression. Example: (…

EX1.3 リスト中のatomの数を数える

car部のnilは空リストではなく、atom扱いで実装。 ;;; Exercise 1.3 [m] Write a function that counts the number of atoms in an expression. ;;; For example: (count-atoms '(a (b) c)) =>3. Notice that there is something of an ;;; ambiguity in thi…

Ex1.2 Powerを定義

PAIPのAnswerではexprを使っていたけど、powerの間違いかな。 ここは、O(n)の解答で。負の数にも対応したけど、小数を指定されると無限ループになる。 ;;; Exercise 1.2 [m] Write a function to exponentiate, or raise a number to an integer ;;; power. …