简体   繁体   English

Haskell:使用Lambda抽象时出现GHCI错误

[英]Haskell: GHCI error when using lambda abstraction

I tried to run the following code which is taken from 'Programming in Haskell' by Graham Hutton 我试图运行以下代码,该代码摘自Graham Hutton的“在Haskell中编程”

type Parser a = [(a, String)]

return :: a -> Parser a
return v = \inp -> [(v,inp)]

when loading the module in GHCI 7.6.3 the following error occurs: 在GHCI 7.6.3中加载模块时,发生以下错误:

Couldn't match expected type `t0 -> [(a, t0)]'
    with actual type `[(a, String)]'
The lambda expression `\ inp -> ...' has one argument,
but its type `Parser a' has none
In the expression: \ inp -> [(v, inp)]
In an equation for `return': return v = \ inp -> [(v, inp)]
Failed, modules loaded: none.

I changed the sample to: 我将示例更改为:

type Parser a = [(a, String)]

return :: a -> String -> Parser a
return v inp = [(v,inp)]

which worked, I'd like to get to run the original sample however and was wondering what I've missed. 这项工作奏效了,但是我想运行原始示例,并想知道我错过了什么。

I don't have this book, but it's clearly a typo. 我没有这本书,但这显然是错字。 Should be: 应该:

type Parser a = String -> [(a, String)]

By the way, the author's website has all the code from the book, and if you have a look at Parsing.lhs , you'll see the definition 顺便说一下,作者的网站上有书中的所有代码,如果您查看Parsing.lhs ,则会看到定义

newtype Parser a  =  P (String -> [(a,String)])

That's probably a more advanced version which is obtained incrementally during the chapter, but any way, the fact that a parser has an input string is crucial. 这可能是本章中逐步获得的更高级的版本,但无论如何,解析器具有输入字符串的事实至关重要。

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

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