haskell - 如何在haskell中调用函数?
问题描述
我有一个创建列表然后随机播放的代码。但我无法执行,因为=
该main = do
部分中的问题。错误是"parse error on input"
。
这是代码:
import System.IO
import System.Random
shuffle :: [a] -> [a]
shuffle list = if length list < 2 then return list else do
i <- randomRIO (0, length list-1)
r <- shuffle (take i list ++ drop (i+1) list)
return (list!!i : r)
main = do --the problem is in this line
putStrLn "Enter the number:"
number <- getLine
let n = (read number :: Int)
let list = [1..n]
print list
shuffle list
解决方案
调用函数不是问题。您的定义有缩进问题,这对解析器shuffle
来说不是问题,直到它到达该行。main = do
import System.IO
import System.Random
shuffle :: [a] -> IO [a]
shuffle list = if length list < 2 then return list else do
i <- randomRIO (0, length list-1)
r <- shuffle (take i list ++ drop (i+1) list)
return (list!!i : r)
main = do --the problem is in this line
putStrLn "Enter the number:"
number <- getLine
let n = (read number :: Int)
let list = [1..n]
print list
shuffled <- shuffle list
print shuffled
请注意正确使用的其他更改IO
。
推荐阅读
- c++ - 有没有办法降低 c++ 中循环函数的速率/时间?
- go - Golang 调试显示一个 ~r2 变量,我没有在我的代码中定义
- macos - Apache2设置macOS Monterey错误禁止
- c++ - 将字符存储数组转换为哈希图时遇到错误
然后堆。然后打印其频率的字符和数字 - apache-spark - Spark:在 CASE WHEN 表达式中使用空检查来防止类型错误
- unit-testing - 如何从 DD 路径测试中找到独立路径?
- angular - 使用 ngFor 时在迭代中同时设置类和数据
- c++ - SetWindowRgn 将窗口主题更改为 Classic
- c++ - 如何在给定位置反转 std::list?
- jquery - ASP.NET MVV Core 应用程序:加载资源失败:服务器响应状态为 404(未找到)bootstrap.css:1