Haskell Lambda 帮助 - 创建应用程序函数
Haskell Lambda help - Creating a applications function
我正在尝试创建一个函数 applications,当给定一个术语 N 和一个术语列表 (X1,...,Xn) return N(X1,...,Xn)
但是当我 运行 我的代码时,它显示以下错误:
* Couldn't match type `Term' with `[Char]'
Expected type: Var
Actual type: Term
* In the first argument of `Lambda', namely `x'
In the expression: Lambda x (applications v xs)
In an equation for `applications':
applications v (x : xs)
| x == [] = Lambda x v
| otherwise = Lambda x (applications v xs)
|
147 | |otherwise = Lambda x (applications v xs)
但由于某种原因,我的 applications 函数导致了这个错误,尽管它看起来与我的工作 abstractions.
applications :: Term -> [Term] -> Term
applications v [] = v
applications v (x:xs)
|x == [] = Lambda x v
|otherwise = Lambda x (applications v xs)
我很感激任何帮助,因为我是新手!
我假设您的类型 Term
定义如下:
type Var = String
data Term = Var Var | Lambda Var Term | App Term Term deriving Show
也就是说,该应用程序由二进制数据构造函数捕获App
。
现在,请注意,您对 abstractions
的定义可以通过从第二个子句中删除大小写区分来简化(第一个子句已经捕获了空尾的情况):
abstractions :: Term -> [Var] -> Term
abstractions t [] = t
abstractions t (x : xs) = Lambda x (abstractions t xs)
其中 lambda 抽象通常尽可能向右延伸,即 lambda x1。拉姆达x2。 t = λ x1。 (lambda x2.t),函数应用通常关联到左边,即t u1 u2 = (t u1) u2。因此,函数 applications
的直接应用可以是:
applications :: Term -> [Term] -> Term
applications t [] = t
applications t (u : us) = applications (App t u) us
例如:
> applications (Var "x") [Var "y", Var "z"]
App (App (Var "x") (Var "y")) (Var "z")
现在,abstractions
和 applications
终于遵循了熟悉的模式。 abstractions
可以写成对要抽象的变量列表的右折,applications
可以写成对要应用的术语列表的左折:
abstractions = foldr Lambda
applications = foldl App
我正在尝试创建一个函数 applications,当给定一个术语 N 和一个术语列表 (X1,...,Xn) return N(X1,...,Xn)
但是当我 运行 我的代码时,它显示以下错误:
* Couldn't match type `Term' with `[Char]'
Expected type: Var
Actual type: Term
* In the first argument of `Lambda', namely `x'
In the expression: Lambda x (applications v xs)
In an equation for `applications':
applications v (x : xs)
| x == [] = Lambda x v
| otherwise = Lambda x (applications v xs)
|
147 | |otherwise = Lambda x (applications v xs)
但由于某种原因,我的 applications 函数导致了这个错误,尽管它看起来与我的工作 abstractions.
applications :: Term -> [Term] -> Term
applications v [] = v
applications v (x:xs)
|x == [] = Lambda x v
|otherwise = Lambda x (applications v xs)
我很感激任何帮助,因为我是新手!
我假设您的类型 Term
定义如下:
type Var = String
data Term = Var Var | Lambda Var Term | App Term Term deriving Show
也就是说,该应用程序由二进制数据构造函数捕获App
。
现在,请注意,您对 abstractions
的定义可以通过从第二个子句中删除大小写区分来简化(第一个子句已经捕获了空尾的情况):
abstractions :: Term -> [Var] -> Term
abstractions t [] = t
abstractions t (x : xs) = Lambda x (abstractions t xs)
其中 lambda 抽象通常尽可能向右延伸,即 lambda x1。拉姆达x2。 t = λ x1。 (lambda x2.t),函数应用通常关联到左边,即t u1 u2 = (t u1) u2。因此,函数 applications
的直接应用可以是:
applications :: Term -> [Term] -> Term
applications t [] = t
applications t (u : us) = applications (App t u) us
例如:
> applications (Var "x") [Var "y", Var "z"]
App (App (Var "x") (Var "y")) (Var "z")
现在,abstractions
和 applications
终于遵循了熟悉的模式。 abstractions
可以写成对要抽象的变量列表的右折,applications
可以写成对要应用的术语列表的左折:
abstractions = foldr Lambda
applications = foldl App