是的,我發佈了另一個答案。它仍然可能不是你正在尋找的。但我認爲它可能是有用的。它在Haskell中。
data LExpr = Lambda Char LExpr
| Atom Char
| App LExpr LExpr
instance Show LExpr where
show (Atom c) = [c]
show (App l r) = "(" ++ show l ++ " " ++ show r ++ ")"
show (Lambda c expr) = "(λ" ++ [c] ++ ". " ++ show expr ++ ")"
所以在這裏,我編寫了一個基本的代數數據類型來表達lambda微積分。我添加了一個簡單但有效的自定義Show實例。
ghci> App (Lambda 'a' (Atom 'a')) (Atom 'b')
((λa. a) b)
爲了好玩,我扔在一個簡單的方法reduce
,與助手replace
。警告:未經仔細考慮或測試。請勿用於工業用途。無法處理某些討厭的表情。:P
reduce (App (Lambda c target) expr) = reduce $ replace c (reduce expr) target
reduce v = v
replace c expr [email protected](Atom v)
| v == c = expr
| otherwise = av
replace c expr [email protected](App l r)
= App (replace c expr l) (replace c expr r)
replace c expr [email protected](Lambda v e)
| v == c = lv
| otherwise = (Lambda v (replace c expr e))
它似乎工作,雖然這真的只是我越來越側面。 (it
在ghci中是指在提示符評估的最後一個值)
ghci> reduce it
b
所以現在最有趣的部分,rotate
。所以我想我可以剝掉第一層,如果它是Lambda,那麼很好,我會保存標識符並繼續向下鑽,直到遇到非Lambda。然後,我將把Lambda和標識符放回到「最後」位置。如果首先不是Lambda,那就什麼也不做。
rotate (Lambda c e) = drill e
where drill (Lambda c' e') = Lambda c' (drill e') -- keep drilling
drill e' = Lambda c e' -- hit a non-Lambda, put c back
rotate e = e
原諒缺乏想象力的變量名稱。通過ghci中發送此表現出良好的跡象:
ghci> Lambda 'a' (Atom 'a')
(λa. a)
ghci> rotate it
(λa. a)
ghci> Lambda 'a' (Lambda 'b' (App (Atom 'a') (Atom 'b')))
(λa. (λb. (a b)))
ghci> rotate it
(λb. (λa. (a b)))
ghci> Lambda 'a' (Lambda 'b' (Lambda 'c' (App (App (Atom 'a') (Atom 'b')) (Atom 'c'))))
(λa. (λb. (λc. ((a b) c))))
ghci> rotate it
(λb. (λc. (λa. ((a b) c))))
我們可以使用zygohistomorphic prepromorphisms? – Alvivi 2011-03-04 01:55:34
@alvivi:該死的!現在我對那些不知道它們的顴骨睡覺感到好奇。 – 2011-03-04 01:59:27
@alvivi - 以爲你想知道:我的拼寫檢查器提出* * zygohistomorphic prepromorphisms *的symplesiomorphic多態*。 – Malvolio 2011-03-04 04:31:26