0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Haskellでラムダ計算:乗算編

Posted at

加算に引き続いてラムダ計算の乗算についてやっていきます。
乗算のラムダ式は以下のようになります。

$$\lambda x y a .x(ya)$$

例えば、$3$かける$3$は
$$(\lambda x y a .x(ya))\textbf{3}\textbf{3}\rightarrow\lambda a.\textbf{3}(\textbf{3}a)$$

$$\textbf{3}a\equiv(\lambda s z.s(s(s(z))))a\rightarrow\lambda z.a(a(a(z)))$$

$$(\textbf{3}a)b\rightarrow a(a(a(b)))$$となるので、

$\textbf{3}(\textbf{3}a)b = (\lambda s z.s(s(s(z))))(\textbf{3}a)b$
$\rightarrow[\textbf{3}a]([\textbf{3}a]([\textbf{3}a]b))$
$\rightarrow[\textbf{3}a]([\textbf{3}a](a(a(a(b)))))$
$\rightarrow a(a(a(a(a(a(a(a(a(b)))))))))$

two s z     = s(s(z))     -- 2
three s z   = s(s(s(z)))  -- 3
mult x y a = x(y a)
add1 x = x + 1

main = do
    print $ mult three three add1 0     -- 3 * 3
    print $ mult two three add1 0       -- 2 * 2

ghci> main
9
6
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?