当我尝试编译下面的列表时
import System.Environment(getArgs)
import System.Exit
import Control.Monad(when)
main = do
args <- getArgs
when (length args /= 2) $ do
putStrLn "Syntax: passwd-al filename uid"
existFailure
Run Code Online (Sandbox Code Playgroud)
编译器投诉:函数'putStrLn'应用于两个参数.但显然它只需要一个String而existsFailure只是System.Exit的另一个IO动作.
怎么解决这个问题?
我试图通过编写一个简单的文件副本util来学习haskell:
main = do
putStr "Source: "
srcPath <- getLine
putStr "Destination: "
destPath <- getLine
putStrLn ("Copying from " ++ srcPath ++ " to " ++ destPath ++ "...")
contents <- readFile srcPath
writeFile destPath contents
putStrLn "Finished"
Run Code Online (Sandbox Code Playgroud)
这让我感到高兴
GHCi, version 6.10.4: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
[1 of 1] Compiling Main ( D:\Test.hs, interpreted )
D:\Test.hs:8:22: Not in …Run Code Online (Sandbox Code Playgroud) 我有一个类型,Foo并希望使它成为一个实例Show,以便我可以在GHCi中使用它:
data Foo = Foo
instance Show Foo where
show Foo = "Foo"
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用它时,出现了一个模糊的错误:
ghci> show Foo
<interactive>:4:1:
Ambiguous occurrence `show'
It could refer to either `Main.show', defined at Foo.hs:4:1
or `Prelude.show',
imported from `Prelude' at Foo.hs:1:1
(and originally defined in `GHC.Show')
Run Code Online (Sandbox Code Playgroud)
为什么?我刚刚定义了属于类型类的函数,不是吗?
我正在写一些Haskell代码来学习语言,我遇到了语法错误:
Vec2.hs:33:27: parse error on input '='
我在这里写的代码如下.错误指向第二个术语vec2Normalize iLength = ...我没有看到语法错误
-- Get the inverse length of v and multiply the components by it
-- Resulting in the normalized form of v
vec2Normalize :: Vec2 -> Vec2
vec2Normalize v@(x,y) = (x * iLength, y * iLength)
where length = vec2Length v
iLength = if length == 0 then 1 else (1 / length)
Run Code Online (Sandbox Code Playgroud) 我是Haskell的新手,在haskell中编写一个小代码时,我遇到了一个名为的错误
Last generator in do {...} must be an expression
Run Code Online (Sandbox Code Playgroud)
我努力删除此错误但失败了.
我访问堆栈溢出这个错误,我发现但答案是太大而复杂,所以我无法理解.
我的代码是
main = do
putStrLn "What is your name?"
name <- getLine
putStrLn ("Name of customer is "++ name)
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我,所以这个错误可以解决,你的帮助必须得到赞赏
我正在学习Haskell并且正在编写一本书中的示例程序.下面的这个模块将无法编译并在ghci和ghc中给出以下错误:
"Optimal.hs:15:23:解析错误输入'='"
任何想法,我都看不到它?
module Optimal (optimalPath) where
import RoadSystem
optimalPath :: RoadSystem -> Path
optimalPath roadSystem =
let (bestAPath, bestBPath) = foldl roadStep ([], []) roadSystem
in if sum (map snd bestAPath) <= sum (map snd bestBPath)
then reverse bestAPath
else reverse bestBPath
roadStep :: (Path, Path) -> Section -> (Path, Path)
roadStep (pathA, pathB) (Section a b c) =
let timeA = sum (map snd pathA)
timeB = sum (map snd pathB)
forwardTimeToA = timeA + a
crossTimeToA = …Run Code Online (Sandbox Code Playgroud) 我遇到了棘手的缩进造成的问题,这里的代码看起来 是VI:
1 import Data.List
2 myQuickSort [] = []
3 myQuickSort (x:xs) = myQuickSort smaller ++ [x] ++ myQuickSort bigger
4 where smaller = filter ( < x ) xs
5 bigger = filter ( >=x ) xs
Run Code Online (Sandbox Code Playgroud)
但是在./cat 3.hs之后,看起来,
root@pierr-desktop:/opt/playGround/haskell# cat 3.hs
import Data.List
myQuickSort [] = []
myQuickSort (x:xs) = myQuickSort smaller ++ [x] ++ myQuickSort bigger
where smaller = filter ( < x ) xs
bigger = filter ( >=x ) xs
Run Code Online (Sandbox Code Playgroud)
然后将其加载到ghci中
GHCi, version …Run Code Online (Sandbox Code Playgroud) 为什么以下代码不正确?我得到这个错误:do {...}中的最后一个生成器必须是一个表达式?
main = do putStrLn "What is 2 + 2?"
x <- readLn
if x == 4
then putStrLn "You're right!"
else putStrLn "You're wrong!"
Run Code Online (Sandbox Code Playgroud) 你能看到一些错误吗?我不能!GHC指向:
Err == Err = True
Run Code Online (Sandbox Code Playgroud)
但这条线路还可以(也许).
data Stone = Black | White | None | Err
instance Eq Stone where
Black == Black = True
White == White = True
None == None = True
Err == Err = True
_ == _ = False
instance Show Stone where
show Black = "B "
show White = "W "
show Err = "E "
show None = "N "
Run Code Online (Sandbox Code Playgroud)
错误信息:main.hs:9:20:输入`='解析错误
defaultFileName :: [Char]
defaultFileName = "Test.log"
defaultSearchName :: String
defaultSearchName = "xyz"
Run Code Online (Sandbox Code Playgroud)
可以编译此代码:
a3 :: Int -> [[Char]] -> IO [Char]
a3 index arg =
if null arg
then do
a <- putStrLn "No parameters have been passed."
a <- putStrLn $ "1 Default search string: " ++ defaultSearchName
a <- putStrLn ("2 Default file name: " ++ defaultFileName)
return defaultFileName
else return (arg!!index)
Run Code Online (Sandbox Code Playgroud)
一旦我添加了一个额外的 IF-THEN-ELSE,我就不能再编译它了
a3 :: Int -> [[Char]] -> IO [Char]
a3 index arg =
if null …Run Code Online (Sandbox Code Playgroud)