小编Tib*_*doš的帖子

过滤Nothing并解压缩Just

我在使用这个程序时遇到了麻烦.

filterJust :: [Maybe a] -> [a]

filterJust [] = []
filterJust x = map fromJust (filter (isJust) x)
Run Code Online (Sandbox Code Playgroud)

但是ghci不断报道这个

但是ghci不断报道这个

编辑:

我不想使用任何额外的模块,所以我这样做:

filterJust :: [Maybe a] -> [a]

filterJust x = map unpack (filter (Nothing /=) x)

unpack (Just a) = a
Run Code Online (Sandbox Code Playgroud)

我收到了这条消息

我收到了这条消息

而且我不明白为什么.我应该可以使用Eq函数而无需导入anthing吗?

haskell

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

Haskell代码zipWith使用Maybe

我在使用此代码时遇到问题.我正在尝试编写一个简单的函数,它接受两个列表并尝试通过列表B的相应元素划分列表A的每个元素.如果列表B中的元素为0,则应返回Nothing,否则应返回Just (a / b).

这是代码:

divlist :: Integral a => [a] -> [a] -> [Maybe a]
divlist = zipWith (\x y -> if (y /= 0) then Just (x / y) else Nothing) 
Run Code Online (Sandbox Code Playgroud)

这可能是愚蠢的,但我根本找不到它.

编辑:这是ghci报道的:

C:\Users\spravce\Desktop\Haskell\6.hs:16:51: error:
    • Could not deduce (Fractional a) arising from a use of ‘/’
    from the context: Integral a
        bound by the type signature for:
                divlist :: Integral a => [a] -> [a] -> [Maybe a]
        at C:\Users\spravce\Desktop\Haskell\6.hs:14:1-48
    Possible fix:
        add …
Run Code Online (Sandbox Code Playgroud)

haskell

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

构造函数使指针类型的对象

我上了这堂课

class Person {

public:

    Person(const std::string& name, const std::string& email, const std::string& city) 
    : name(name), email(email), city(city) {
    }

    bool hasCity() const {
        return city.compare("") == 0;
    }

    void print() const {
        std::cout << name + " <" + email + ">";
        if(hasCity()){
            std::cout << ", " + city;
        }
        std::cout << std::endl;
    }

    bool equalTo(const Person& comparedPerson) const {
        return email.compare(comparedPerson.email) != 0;
    }

    bool equalId(std::string comparedId){
        return email.compare(comparedId) != 0;
    }

    const std::string name;
    const std::string email; …
Run Code Online (Sandbox Code Playgroud)

c++

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

标签 统计

haskell ×2

c++ ×1