小编mcj*_*s45的帖子

Haskell:Brackets工作,但$抛出一个错误

我希望以下代码将"15"转换为整数并打印结果,但它会引发错误.

main = print $ read "15" :: Integer

Couldn't match expected type `Integer' with actual type `IO ()'
Run Code Online (Sandbox Code Playgroud)

但只是使用main = print (read "15" :: Integer)运行良好.我的印象是$有效地围绕括号中的其余部分.为什么不在这种情况下工作?

haskell

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

我的改版拉链有什么问题?

我正在尝试编写一个类似于zip但不会丢弃额外元素的函数.我觉得我在某个地方犯了一个非常愚蠢的错误.

输入示例:

zipMaybe [1,2,3] [1,2]
Run Code Online (Sandbox Code Playgroud)

期望的输出:

[(Just 1, Just 1), (Just 2, Just 2), (Just 3, Nothing)]
Run Code Online (Sandbox Code Playgroud)
zipMaybe :: [a] -> [b] -> [(Maybe a, Maybe b)]
zipMaybe (a:as) (b:bs) = (Just a, Just b) : zip as bs -- line with error
zipMaybe (a:as) [] = (Just a, Nothing) : zip as []
zipMaybe [] (b:bs) = (Nothing, Just b) : zip [] bs
zipMaybe _ _ = []
Run Code Online (Sandbox Code Playgroud)

但是,这不会编译.

Test.hs:2:49:
    Couldn't match type `a' with `Maybe a'
      `a' …
Run Code Online (Sandbox Code Playgroud)

haskell compiler-errors

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

强制ghc导入隐藏模块

我需要隐藏模块中的一些功能.如果import Graphics.UI.Gtk.Types我收到:

Could not find module `Graphics.UI.Gtk.Types'
it is a hidden module in the package `gtk-0.12.4'
Run Code Online (Sandbox Code Playgroud)

如何在不编辑和重新编译gtk的情况下导入此隐藏模块?

haskell ghc

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

如何终止管道后台进程?

会话示例:

- cat myscript.sh 
#!/bin/bash
tail -f example.log | grep "foobar" &
echo "code goes here"
# here is were I want tail and grep to die
echo "more code here"

- ./myscript.sh

- ps
  PID TTY          TIME CMD
15707 pts/8    00:00:00 bash
20700 pts/8    00:00:00 tail
20701 pts/8    00:00:00 grep
21307 pts/8    00:00:00 ps
Run Code Online (Sandbox Code Playgroud)

如您所见,tail 和 grep 仍在运行。


像下面这样的东西会很棒

#!/bin/bash
tail -f example.log | grep "foobar" &
PID=$!
echo "code goes here"
kill $PID
echo "more code here"
Run Code Online (Sandbox Code Playgroud)

但这只会杀死 …

linux bash pipe

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

Is it possible?: Behavior t [Behavior t a] -> Behavior t [a]

Is there a way to have a Behavior t [a] where the values of [a] at time t are the values contained in a Behavior t [Behavior t a] at time t? I.e, a function with the type of:

Behavior t [Behavior t a] -> Behavior t [a]
Run Code Online (Sandbox Code Playgroud)

If this is not possible, is that because of a logical impossibility or a limitation in reactive-banana?

haskell frp reactive-banana

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

ADT的集合,没有重复的构造函数

如果我有数据类型:

data Component = PositionC Double Double | HealthC Double | NameC String
Run Code Online (Sandbox Code Playgroud)
  • 我想要一个Components 的集合
  • 我不会存储任何重复项(即:它不会有两个HealthC)
  • 我不想手动复制代码

我可以使用什么技术,数据结构,TemplateHaskell功能来满足上述标准?

我已经尝试过Data.Set和Data.Map但没有成功.

Data.Map尝试:

import Data.Map

data Component = PositionC Double Double | HealthC Double | NameC String

-- Unacceptable code duplication
data ComponentType = PositionCT | HealthCT | NameCT
    deriving (Eq, Ord)

type ComponentMap = Map ComponentType Component

foo :: ComponentMap
-- More annoying code duplication
foo = fromList [(HealthCT, HealthC 100), (NameCT, NameC "John")] 

fooHealth = foo ! HealthCT -- …
Run Code Online (Sandbox Code Playgroud)

haskell map set

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

访问映射中元素的成员,该映射的值是C++中的抽象基类

英文解释:我要访问一个名为成员foo从关键元素"bar"从一个std::map<string, Parent *>地方Parent是一个抽象基类.

码:

#include <map>
#include <string>
#include <iostream>

using namespace std;

class Parent {
public:
    virtual ~Parent() {}
};

class Child: public Parent {
public:
    Child(): var(2) {}
    int var;
};

int main() {
    map<string, Parent *> children;
    children["bar"] = new Child;
    cout << children["bar"]->var << endl; // Erroneous line
    cout << children.find("bar")->second->var << endl; // Different method, still gives the same error
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误: ‘class Parent’ …

c++ polymorphism map

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

后台进程完成后关闭前台进程

例如,当wget完成时我将如何杀死尾巴.

#!/bin/bash
wget http://en.wikipedia.org/wiki/File:Example.jpg &
tail -f example.log
Run Code Online (Sandbox Code Playgroud)

linux bash

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

标签 统计

haskell ×5

bash ×2

linux ×2

map ×2

c++ ×1

compiler-errors ×1

frp ×1

ghc ×1

pipe ×1

polymorphism ×1

reactive-banana ×1

set ×1