小编bli*_*630的帖子

不在范围内:`catch'

我正在阅读为了一个很好的东西,你学习了一个haskell,但是当我读到输入和输出章节中的异常部分时,我遇到了一个不在范围内的错误:`catch'

这是我的代码:

import System.Environment  
import System.IO  
import System.IO.Error  

main = toTry `catch` handler  

toTry :: IO ()  
toTry = do (fileName:_) <- getArgs  
           contents <- readFile fileName  
           putStrLn $ "The file has " ++ show (length (lines contents)) ++ " lines!"  

handler :: IOError -> IO ()  
handler e = putStrLn "Whoops, had some trouble!"  
Run Code Online (Sandbox Code Playgroud)

我收到此错误消息:

Not in scope: `catch'
Run Code Online (Sandbox Code Playgroud)

haskell scope

6
推荐指数
1
解决办法
889
查看次数

在Haskell中使用`print'不会产生(Show a0)实例

我是Haskell的新手.主题来自Learn your Haskell书籍"递归数据结构"

这是我的代码

data List a = Empty | Cons a (List a) deriving (Show, Read, Eq, Ord) 

main = do
    print $ Empty
    print $ 5 `Cons` Empty 
    print $ 4 (Cons 5 Empty)  
    print $ 3 `Cons` (4 `Cons` (5 `Cons` Empty))
Run Code Online (Sandbox Code Playgroud)

这是我收到的错误消息

No instance for (Show a0) arising from a use of `print'
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances: …
Run Code Online (Sandbox Code Playgroud)

haskell types

3
推荐指数
1
解决办法
702
查看次数

使用"elem"时没有(Eq TrafficLight)的实例可能的修复:为(Eq TrafficLight)添加实例声明

我有一个例子来自于了解你是一个很好的Haskell

class Eq1 a where
   (===), (=/=) :: a -> a -> Bool
   x === y = not $ x =/= y
   x =/= y = not $ x === y  

data TrafficLight = Red | Yellow | Green  

instance Eq1 TrafficLight where  
    Red === Red = True  
    Green === Green = True  
    Yellow === Yellow = True  
    _ === _ = False  


instance Show TrafficLight where  
    show Red = "Red light"  
    show Yellow = "Yellow light"  
    show Green = …
Run Code Online (Sandbox Code Playgroud)

haskell

2
推荐指数
1
解决办法
199
查看次数

无法匹配预期类型具有实际类型IO()的人员

我已经开始学习Haskell,并阅读Learn You Haskell.第8章处理"制作我们自己的类型和类型类",我有一个错误信息,这对我来说是一个问题.可能解决方案是一个小问题,但我找不到它,所以请揭示提示,或者帮我解释一下.

data Person = Person { firstName :: String  
                     , lastName :: String  
                     , age :: Int  
                     } deriving (Eq, Show, Read)  

mikeD = Person {firstName = "Michael", lastName = "Diamond", age = 43}  

main = print $ read "Person {firstName =\"Michael\", lastName =\"Diamond\", age = 43}" :: Person
Run Code Online (Sandbox Code Playgroud)

这是我收到的错误消息

Couldn't match expected type `Person' with actual type `IO ()'
In a stmt of a 'do' block:
    print
    $ read
        "Person {firstName =\"Michael\", lastName =\"Diamond\", age = 43}" …
Run Code Online (Sandbox Code Playgroud)

io haskell types

1
推荐指数
1
解决办法
211
查看次数

(Show (Int -&gt; Int)) 没有因使用‘print’而产生的实例

我尝试实现一个基本的 lambda 函数,但遇到了一些错误,并且在此处的问题之间搜索后无法找出解决方案。我的代码是:

myMap :: (a -> b) -> [a] -> [b]
myMap addSomething [] = []
myMap addSomething (x:xs) = addSomething x : myMap addSomething xs

-- instance Show (a -> b) where
--          show a = "funcion"

list = [0..4]
listTwo = [(5 :: Int)..9]

addSomething :: Int -> Int
addSomething x = x + 1

addSomethingTwo ::  Num a => a -> a-> a
addSomethingTwo x = (\x->x+1)

main = do
    print $ myMap addSomething list
    print …
Run Code Online (Sandbox Code Playgroud)

lambda haskell

1
推荐指数
1
解决办法
1776
查看次数

没有因使用'mappend'而引起的(Monoid [Char])实例

与您一起学习Haskell函子,应用函子和monoids章节,并面临一个新问题,即使我试图解决无法解决的问题,我的代码也在这里,我的代码在这里:

class Monoid m where  
    mempty :: m  
    mappend :: m -> m -> m  

instance Monoid a => Monoid (Maybe a) where  
    mempty = Nothing  
    Nothing `mappend` m = m  
    m `mappend` Nothing = m  
    Just m1 `mappend` Just m2 = Just (m1 `mappend` m2)  

main = print $ Nothing `mappend` Just "andy" 
Run Code Online (Sandbox Code Playgroud)

这是我收到的错误消息:

  No instance for (Monoid [Char]) arising from a use of `mappend'
    Possible fix: add an instance declaration for (Monoid [Char])
    In the second argument of …
Run Code Online (Sandbox Code Playgroud)

haskell

0
推荐指数
1
解决办法
308
查看次数

可惜的任务,但我无法解决它

我是一个真正的一级忍者,想要解决我的作业,但我不能这样做:

所以任务是:

说一个介于1-100之间的数字:
我说15个
Okey,这很好
说1-100之间的数字:
我说231
Grrr,不好回答,说另一个数字
我说-58
Grrr,不好回答,说另一个数字
78
Okey,那是好在
1-100之间说一个数字:

这是我的问题,因为我可以在第一行编写代码,但我无法前进

我的代码是:

x=input("Saying a number between 1-100: ")
print(x)
Run Code Online (Sandbox Code Playgroud)

python

-2
推荐指数
1
解决办法
94
查看次数

标签 统计

haskell ×6

types ×2

io ×1

lambda ×1

python ×1

scope ×1