は、私たちはそれを通常のLispの方法をやってみましょう
:-)楽しみのようですね。
問題は次のとおりです。文字列をディクショナリと一致させて、せいぜい1つのスペルミスが許されるようにします。ここで
は辞書です:
(defparameter *dictionary*
(loop for i from 1 to 9 collect (cons (format nil "~R" i) i)))
2つの文字列が一致することは何を意味するのでしょうか?
(defun diff (s1 s2)
"Count the number of differences in the shortest common start."
(loop for c1 across s1
for c2 across s2
sum (if (char= c1 c2) 0 1)))
(diff "abc" "abcde")
==> 0
(diff "abc" "aba")
==> 1
今マッチング:
(defun parse-string (s)
(loop for (name . number) in *dictionary*
if (matchp name s) return number))
(parse-string "one")
==> NIL ; not found
(parse-string "onn")
==> 1
(parse-string "too")
==> 2
(parse-string "thre3")
==> 3
(parse-string "foor")
==> 4
(parse-string "fivv")
==> 5
(parse-string "sis")
==> 6
(parse-string "sever")
==> 7
(parse-string "aight")
==> 8
(parse-string "nane")
==> 9
PS:最後に
(defun matchp (s1 s2)
"Two strings match iff they have the same length and 1 different character."
(and (= (length s1)
(length s2))
(= 1 (diff s1 s2))))
(matchp "a" "b")
==> T
(matchp "too" "two")
==> T
(matchp "one" "one")
==> NIL
は、辞書内の文字列を検索。私はかなり先進的なloop
ファシリティを使いました。これが宿題であれば、おそらくそれを使うことは許されないので、より単純なイディオムを使用してコードを書き直す必要があります。
PPS。あなたはおそらく本を読むべきです、aclとpclの両方が良いです。
出典
2017-04-06 18:54:30
sds
良い入門書と無料のPDFダウンロード:https://www.cs.cmu.edu/~dst/LispBook/ –
この本を後で調べます。お返事ありがとう – plank223