突発性CL勉強会でSLIMEについて勉強。 id:g000001さん、id:mokeheheさん、NANRIさんとid:tszの4人で実施。 新宿大ガード横店は改装工事をしていたので、近くの別店に移動しました。 新宿のルノアールで7時間実施。SLIMEの話題自体は2〜3時間で終わりましたが…
4章で定義したGPSは複数の目標を指定した場合、ある目標達成によって他の目標が部分的に達成できる場合をうまく扱っていない。これを修正する問題。まずは、その様なシチュエーションをオペレータのセットで定義する。 ;; Exercise 4.3[h] GPS does not reco…
PAIPも4章からは、結構規模の大きいプログラムを扱うようになってきたのでどうしようか模索中。 日記に貼れる大きさじゃ無くなってきたなぁと。 取りあえず勉強もかねて、3章までに定義した関数と4章で定義したGPSをdefpackage化してみる。
リストを取って、その全ての順列を生成する関数を定義する。 ;; 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…
PAIPも4章に入るといよいよAIの内容に入ります。4章ではExerciseは章末にまとまっています。Ex4.1は章中で定義した、デバッグ用の表示関数dbgを formatディレクティブだけで表現してみようというモノです。章中の定義は以下のように行ってました。 (defvar…
formatを使って、単語のリストを取り文として表示する。 その際、 formatは1度だけ呼び出す 先頭の単語の、先頭の文字は大文字に、それ以外は小文字にする。 最後の単語の後ろにはピリオドを打つ を行う。リファレンスを見たけど複雑過ぎてわからない。~@( …
lcmとnreconcの機能をマニュアルを使って調べる問題。 今更ながらslimeとHyperSpecがあると便利。hyperspec-lookupをキーバインドしておくとなお便利。 (global-set-key "\C-cH" 'hyperspec-lookup) 調べた結果は、lcm: 最小公倍数を求める関数 nreconc: rev…
reduceを使って、リストの長さを求める問題です。 SBCLの場合ignoreをつけないと、コンパイラに文句を言われました。 ;;; Exercise 3.9 [m] White a version of length using the function ;;; reduce. (defun my-length (list) "リストの長さを求める。redu…
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のちょっと前で定義した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…
;;; Exercise 3.7 [s] Why do you think the leftmost of two keys is the ;;; one that counts, rather than the rightmost.同じキーのキーワードパラメタが複数個指定された場合、左端のキーの値が有効となる理由を問う問題。rest パラメタの値に、キーワ…
;;; 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)…
データ構造をプログラムから書き換える問題。 推論に失敗する度に、ツリーが拡張されていく。 ;;; 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…
printの様な関数を作成する。ドット対表記が必要ないところは通常のリスト表記で表示する。結構力業。 ;;; Exercise 3.4 [m] Write a function that, like the regular print ;;; function, will print an expression in dotted pair notation when ;;; nece…
問題ではprincを使えと書いてあるけど、writeで実装した。 せっかくなので作成する関数も元のリストをconsし直して返す様にしてみた。 ;;; Exercise 3.3 [m] Write a function that will print an expression in ;;; dotted pair noteation. Use the bulit-i…
;;; 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…
引数に指定された複数のリストから要素の全ての組み合わせを選択し、各組み合わせに対し、関数fnを呼び出した結果をリストとして返す関数cross-productを定義する。その結果を使ってcombine-allを再定義する。 ;;; Exercise 2.4 [m] One way of describing c…
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…
未定義の関数は、本家サイトのソース公開ページから探せるはず。章の中でcondを使って定義したgenerateがある。 (defun generate (phrase) "Generate a random sentence or phrase" (cond ((listp phrase) (mappend #'generate phrase)) ((rewrites phrase) …
以前のgenerateは、rewriteの戻り値を分岐条件としていたけど、それだと分かりにくいよね、ちゃんと述語を作ろう。という問題。PAIPの答えでは、non-terminal-pを定義していたので、自分はterminal-symbol-pを定義して解いてみる。当然、あまり変わらない。 …
ベクトルの内積を求める問題。 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…
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: (…
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…
PAIPのAnswerではexprを使っていたけど、powerの間違いかな。 ここは、O(n)の解答で。負の数にも対応したけど、小数を指定されると無限ループになる。 ;;; Exercise 1.2 [m] Write a function to exponentiate, or raise a number to an integer ;;; power. …
章の途中で定義したlast-nameを改造して、タイトル(っていうんか?)を取り扱えるようにする。 考えられるケースは取り扱えって言っても、どんな慣習があるんでしょう? 取りあえず例題のケースだけに対応。 ;;; Exercise 1.1 [m] Define a version of last…
L-99もP46以降からPrologの問題そのままになってきた。 取りあえず、この辺りでいったん終了。PAIP本でPrologの実装を調べてくるか。 ;; P54A (defun istree (tree) (cond ((null tree) t) ((not (consp tree)) nil) ((not (= (length tree) 3)) nil) (t (an…
指定した範囲に対するgoldbach conjectureの結果を整形して表示。goldbach-rangeは後の、条件を超える素数の組の数を求める問題のために分離しておいた。 ;; P41 (defun goldbach-range (start end &optional (pred #'identity)) (remove-if #'(lambda (n) (…
P22で定義したrangeと、P31で定義したprime-pを使う。 ;; P39 (defun prime-list (m n) (remove-if-not #'prime-p (range m n))) (prime-list 50 100) ;; => (53 59 61 67 71 73 79 83 89 97)
g000001さんの解法を参考にさせてもらいました。まずは、次の素数を見つける関数next-primeを定義。 例によって、P31のprime-pを使います。 (defun next-prime (n) (if (prime-p (1+ n)) (1+ n) (next-prime (1+ n)))) (next-prime 40) ;; => 41 奇数と2は対…
ACLで動作確認。 P34の実装はconsしまくっている模様。 CL-USER> (time (totient-phi 65535)) ; cpu time (non-gc) 540 msec user, 10 msec system ; cpu time (gc) 130 msec user, 0 msec system ; cpu time (total) 670 msec user, 10 msec system ; real …