简单的haskell代码无法编译 [英] Simple haskell code can't compile

查看:124
本文介绍了简单的haskell代码无法编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习Haskell,只是尝试了一个简单的代码,当我运行

  doubleMe :: Int  - > Int 
doubleMe x = x + x

main = do
doubleMe 2




ghc -c first.hs


错误是:


$ ghc -c first.hs



first.hs:4:1:
无法匹配预期类型 IO t0'与实际类型 Int'
在表达式中:main
检查函数的类型`main'


当我使用GCHi进行调试时,没有问题需要加载 doubleMe 函数,稍后调用它。任何帮助将不胜感激。

解决方案

Haskell中的每个函数都返回一个特定类型的值。 b

在这种情况下, doubleMe 函数返回 Int 类型的值。当你从ghci解释器加载一个文件时,它会从文件中加载所有的函数,并使它们可用。
$ b main 函数是一个稍微特殊的函数,非常类似于其他语言。它定义了一个Haskell程序的入口点。但是,由于它只是另一个Haskell函数,它也必须返回一个特定类型的值。 main 函数被限制为返回 IO 类型的值,最常见的是 IO( )

IO 本身也很特别,属于一类称为monads的类型。您可以在这里阅读 IO monad



现在让我们回到您的代码:

  main = do 
doubleMe 2

忽略 do 语法一会儿。你基本上告诉编译器: main 函数返回类型为 doubleMe 2 INT 。这不会飞。 main 返回值 IO 类型。在这种情况下,您可以使用 return 函数将 Int 值转换为 IO Int 值。 ( return )函数是所有一元类型必须具有的函数,非常简单地说,它将任何类型的值转换为一元值。)



所以这变成:

pre $ main $ $
return(doubleMe 2)

这是完全有效的代码,并且会被编译。但是,一旦你运行该程序,你会注意到它没有做任何事情。这是因为程序返回值。4.
实际上,您可以在不使用do的情况下编写它,它会变为:

  main = return(doubleMe 2)

这也可以。

然而让我们假设您想要打印出价值。这是 IO 确实进入的地方。打印到屏幕上是 IO 操作。

  main = do 
print(doubleMe 2)
return(doubleMe 2)



do 表达式允许您 chain 一组 IO 操作。所以你的程序仍然会返回值 4 ,但它会首先评估表达式 print(doubleMe 2)。正如所料,这实际上导致打印值 doubleMe 2
检查ghci中的 print 函数。

 > :t print 
print :: Show a => a - > IO()

print函数可以处理任何类型的值,可以显示 / em>,它会产生 IO 动作(打印到屏幕),但不会返回任何()

所有这些例子都可以工作,并且希望能够让事情变得清晰一些。 (查看 main 的类型签名)。

   -  main :: IO Int 
main = return(doubleMe 2)

- main :: IO Int
main = do
print(doubleMe 2)
return(doubleMe 2)

- main :: IO()
main = do
print(doubleMe 2)
return()

- main :: IO()
main = do
print(doubleMe 2)

- main :: IO()
main = print( doubleMe 2)

- main :: IO()
main = do
printHello
print(doubleMe 2)
return()

- main :: IO String
main = do
printHello
print(doubleMe 2)
returnHello


I start learning Haskell, and just tried a simple code, and it shows me some errors when I run

doubleMe :: Int -> Int
doubleMe x = x + x

main = do
    doubleMe 2

ghc -c first.hs

The errors are:

$ ghc -c first.hs

first.hs:4:1: Couldn't match expected type IO t0' with actual typeInt' In the expression: main When checking the type of the function `main'

While I use GCHi to debug, there were no issues to load doubleMe function first, and call it later. Any help would be appreciated.

解决方案

Every function in Haskell returns a value of a certain type.

In this case, the doubleMe function returns a value of the Int type. When you load a file from the ghci interpreter, it loads all the functions from the file and makes them available to you.

The main function is a slightly special function, much like in other languages. It defines the entry point of a Haskell program. However, since it is just another Haskell function it too must return a value of a particular type. The main function is constrained to return a value of the IO type, most commonly IO ().

IO itself is quite special too, belonging to a class of types called monads. You can read up on the IO monad here.

Let us now return to your code:

main = do
    doubleMe 2

Ignore the do syntax for a moment. You're basically telling the compiler that the main function returns doubleMe 2 which is of type Int. This won't fly. main has to return a value of IO type. In which case, you can use the return function to convert an Int value into an IO Int value. (return functions are functions that all monadic types must have. Very simply put, it converts a value of any type, into a monadic value.)

So this becomes:

main = do
    return (doubleMe 2)

This is perfectly valid code, and will compile. However, once you run the program, you'll notice that it doesn't do anything. That's because the program returns the value 4. In fact, you can write it without the do, and it becomes:

main = return (doubleMe 2)

This will work too.

Let us assume however, that you want to print out the value. This is where IO really comes in. Printing to a screen is an IO action.

main = do
          print (doubleMe 2)
          return (doubleMe 2)

A do expression allows you to chain a set of IO actions. So your program will still return the value 4 but it will first evaluate the expression print (doubleMe 2). As expected, that actually results in printing the value doubleMe 2. Examine the print function in ghci.

> :t print
print :: Show a => a -> IO ()

The print function works on a value of any type that can be shown and it results in an IO action (printing to the screen) but returns nothing ().

All of these examples work, and will hopefully make things a little clear. (Look at the type signatures for main).

-- main :: IO Int
main = return (doubleMe 2)

-- main :: IO Int
main = do 
          print (doubleMe 2)
          return (doubleMe 2)

-- main :: IO ()
main = do
          print (doubleMe 2)
          return ()

-- main :: IO ()
main = do
          print (doubleMe 2)

-- main :: IO ()
main = print (doubleMe 2)

-- main :: IO ()
main = do
         print "Hello"
         print (doubleMe 2)
         return ()

-- main :: IO String
main = do
         print "Hello"
         print (doubleMe 2)
         return "Hello"

这篇关于简单的haskell代码无法编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆