English 中文(简体)
未定义与发展(第2部分)
原标题:Undefined and Developing (Part 2)

我已经问了< a href=> “https://stackoverflow.com/ questions/10738806/a-show-instance-for-unflemed” 有关未定义 的问题。由于无法做到这一点,我将描述这个情况,如果这有用的话。 但我认为这个问题很常见,在其他情况下也可以有用。

让我们想象一个情形, 当我们为某语法写一个解析器时。 我们定义了由许多数据类型执行的 AST, 每一个数据类型都有一组构建器, 每个构建器都有一组参数。 每个类型都来自 Show 。 然后我们开始写入解析器的实现, 我们用循环依赖来定义许多函数 。

由于我们有许多功能, 它很难在不检查的情况下同时写入整个模块 。 但函数循环引用将无法编集未完成模块 。 在这种情况下, 我定义钩子功能, 这些功能不会指望返回所需的类型的值 。

这是一个问题! 函数必须返回不是简单的类型, 而是在构造器中带有构造器的数据构建器 。 这对钩子功能来说太难了 。 因此我这样做:

parseHook _ = undefined

在结果数据树上没有“未定义”之前,一切都很简单。我想指出,这种情况发生在发展阶段,我唯一想要的是看到树的结构。当然,我可以把每一种类型都用“也许”来包起来,但我不想改变已经存在的类型。

英语不是我的母语,所以我不确定我是否正确地表达了我的想法。 这就是为什么我要补充一个例子说明这种情况:

data StructParam = StructParam Int Int
                 deriving Show 

data Struct1 = Struct1 Int Int StructParam Int Int Int Int StructParam
             deriving Show 

data Struct2 = Struct2 Int Int StructParam Int Int Int Int StructParam  
             deriving Show 

data Struct3 = Struct3_var1 Struct1 
             | Struct3_var2 Struct2
             deriving Show 

-- this hook is ok, but too long
parse1 _ = Struct1 1 2 (StructParam 3 4) 5 6 7 8 (StructParam 9 0)

-- this hook is short, but undefined
parse2 _ = undefined

-- complete function
rootParse 1 = Struct3_var1(parse1 1)    
rootParse 2 = Struct3_var2(parse2 2)

>print $ rootParse 1
Struct3_var1 (Struct1 1 2 (StructParam 3 4) 5 6 7 8 (StructParam 9 0))

>print $ rootParse 2
Prelude.undefined

-- but I want something like this
>print $ rootParse 2
Struct3_var2("undefined")
最佳回答

您可以在您的数据类型中添加“ 未执行” 作为构建器, 并对所有切片设置默认大小写, 以定义 < code> > 未执行 < /code > 结尾, 然后显示实例会稍有效果 。

data Exp = Literal Int
         | Variable String
         | Operator Op Exp Exp
         | Unimplemented

    deriving Show
问题回答

实现这一点的一个丑陋的黑客是在本类的显示中使用 unsafePerformIO captain ,以发现未定义的例外。 不过,我建议不采用这一方法。





相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?

热门标签