简体   繁体   English

Haskell中的Lambda抽象模式匹配

[英]Lambda abstraction pattern matching in haskell

I am trying to write a function which outputs all variable names.我正在尝试编写一个输出所有变量名称的函数。 When used on the example below, the output should be在下面的示例中使用时,输出应为

*Main> used example
     ["a","b","x","y"]

This is what I have written so far...这是我到目前为止所写的......

import Data.Char
import Data.List

type Var = String

data Term =
    Variable Var
  | Lambda   Var  Term
  | Apply    Term Term
--  deriving Show

instance Show Term where
  show = pretty

example :: Term
example = Lambda "a" (Lambda "x" (Apply (Apply (Lambda "y" (Variable "a")) (Variable "x")) (Variable "b")))


pretty :: Term -> String
pretty = f 0
    where
      f i (Variable x) = x
      f i (Lambda x m) = if i /= 0 then "(" ++ s ++ ")" else s where s = "\\" ++ x ++ ". " ++ f 0 m 
      f i (Apply  n m) = if i == 2 then "(" ++ s ++ ")" else s where s = f 1 n ++ " " ++ f 2 m

used :: Term -> [Var]
used (Variable n) = [n]
used (Lambda n t) = "\\" ++ n ++ ". " ++ used t
used (Apply t1 t2) = used t1 ++ used t2

The problem lies in the line used (Lambda nt) = "\\\\" ++ n ++ ". " ++ used t , I get this error message:问题在于使用的行used (Lambda nt) = "\\\\" ++ n ++ ". " ++ used t ,我收到此错误消息:

list.hs:28:47: error:
    lexical error in string/character literal at character '\n'
   |
28 | used (Lambda n t) = "\" ++ n ++ ". " ++ used t

Why am I getting this complaint?为什么我会收到此投诉?

You are copying and pasting from examples too much.您过多地从示例中复制和粘贴。 Keep thinking about what the function is supposed to mean as you are writing it.在编写函数时,请不断思考该函数的含义。

used (Variable n) = [n]

This is correct: the set of variables used in a term which is just a variable is the variable used.这是正确的:在一个术语中使用的变量集只是一个变量是使用的变量。 For example, the set of variables used in the term x is just [x] .例如,术语x使用的变量集只是[x]

used (Apply t1 t2) = used t1 ++ used t2

This is also correct 1 : the set of variables used in say (xyz) (wxy) is the union of the variables used in xyz and in wxy : that is [x,y,z,w,x,y] .这也是正确的1 : say (xyz) (wxy)中使用的变量xyzwxy使用的变量的并集:即[x,y,z,w,x,y]

So now let's consider the case you are struggling with.所以现在让我们考虑一下您正在努力解决的情况。

used (Lambda n t) = ...

The code you have seems to be attempting to print the the term into a string, which is not what we are trying to do here -- we are trying to find the set of used variables.您拥有的代码似乎试图将术语打印成一个字符串,这不是我们在这里尝试做的——我们试图找到一组使用的变量。

Let's consider an example: we have a term like (\\x. xyz) , and we want to find out what its used variables are.让我们考虑一个例子:我们有一个像(\\x. xyz)这样的术语,我们想找出它使用的变量是什么。 Before trying to come up with the general solution, ask yourself what the result should be in this example.在尝试提出通用解决方案之前,先问问自己在此示例中的结果应该是什么。

If this function is going to be nicely recursive, can we express your expected result in terms of the used variables of xyz ?如果此函数将很好地递归,我们可以根据xyz的使用变量来表达您的预期结果吗? How do we transform the set of used variables of xyz into the set of used variables of \\x. xyz我们如何将xyz的已用变量集转换为\\x. xyz的已用变量集\\x. xyz \\x. xyz ? \\x. xyz ?


1 Though you might get duplicates. 1虽然你可能会得到重复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM