小编Jos*_*ica的帖子

如何以完整而优雅的方式使用固定最小长度的列表?

我目前正在处理这样的功能:

foo = (\(a:b:c:d:e:f:_) -> foobar a b c d e f) . (++ repeat def)
Run Code Online (Sandbox Code Playgroud)

换句话说,给定一个列表,它将使用前六个元素来表示某项内容;如果列表的长度少于六个元素,则将其def用作所缺少列表的替身。这是总数,但其中的各个部分却并非如此(就像map fromJust . filter isJust),所以我不喜欢它。我试图重写它,以便它不需要使用任何局部性,并得到了:

foo [] = foobar def def def def def def
foo [a] = foobar a def def def def def
foo [a,b] = foobar a b def def def def
foo [a,b,c] = foobar a b c def def def
foo [a,b,c,d] = foobar a b c d def def
foo [a,b,c,d,e] = foobar a …
Run Code Online (Sandbox Code Playgroud)

haskell pattern-matching

10
推荐指数
2
解决办法
145
查看次数

当 POSIX 说 stderr 应该可以读写时是什么意思?

POSIX 的stderr、stdin、stdout页面- 标准 I/O 流是这样说的:

标准错误流预计将开放供读取和写入。

“预期”有多强?违反它是未定义的行为吗?它是谁的责任,系统的还是应用程序的?

考虑这个程序:

#include <stdio.h>

int main(void) {
    printf("feof is %d and ferror is %d\n", feof(stderr), ferror(stderr));
    printf("fgetc is %d\n", fgetc(stderr));
    printf("feof is %d and ferror is %d\n", feof(stderr), ferror(stderr));
}
Run Code Online (Sandbox Code Playgroud)

当我在不重定向 stderr 的情况下运行它时(所以它就像 stdin 一样指向我的终端),它会立即输出它而不等待任何输入:

feof is 0 and ferror is 0
fgetc is -1
feof is 0 and ferror is 1
Run Code Online (Sandbox Code Playgroud)

这是否意味着我的系统不符合 POSIX 标准?

另外,如果这是我的责任,那么假设我有一个具有权限的文件620,并且我在组中但不是所有者。这是否意味着这someprogram 2>saidfile是未定义的行为,因为在这种情况下无论如何您都无法从 stderr 读取?

c posix file stderr language-lawyer

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

How do I make this algorithm lazier without repeating myself?

(Inspired by my answer to this question.)

Consider this code (it's supposed to find the largest element that's less than or equal to a given input):

data TreeMap v = Leaf | Node Integer v (TreeMap v) (TreeMap v) deriving (Show, Read, Eq, Ord)

closestLess :: Integer -> TreeMap v -> Maybe (Integer, v)
closestLess i = precise Nothing where
  precise :: Maybe (Integer, v) -> TreeMap v -> Maybe (Integer, v)
  precise closestSoFar Leaf = closestSoFar
  precise closestSoFar …
Run Code Online (Sandbox Code Playgroud)

haskell dry lazy-evaluation

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

我如何使用 Stack/Cabal 构建早期部分的程序输出作为同一构建后期部分的源代码?

我有一个非常特殊的依赖情况,我想将其打包到单个 Stack/Cabal 包中:我需要构建并运行我的程序以获取代码生成器的输入,该代码生成器生成需要链接到的输出。 .. 我的程序。

好的,更具体地说,这里是手动步骤:

  1. stack build 安装所有依赖项,并构建所有非 Verilator 使用的可执行文件。
  2. stack exec phase1运行第一个阶段,其中生成一个 Verilog 文件和一个 Clash.manifest文件。
  3. 我有一个自定义源代码生成器,它.manifest使用步骤 2 中的文件,并生成 C++ 代码和Makefile可用于驱动 Verilator 的代码。
  4. 运行Makefile步骤 3 中生成的:
    1. 它在第 2 步的 Verilog 源代码上运行 Verilator,生成更多的 C++ 源代码和一个新的 Makefile
    2. 然后它运行新生成的 second Makefile,它生成一个二进制库
  5. stack build --flag phase2构建第二个可执行文件。此可执行文件包含.hsc处理步骤 2 中生成的头文件的文件,并链接到步骤 4/2 中生成的 C++ 库。

我想自动化这个,这样我就可以运行,stack build而这一切都在幕后发生。我从哪里开始?!

为了说明整个过程,这里是一个独立的模型:

package.yaml

name: clashilator-model
version: 0
category: acme

dependencies:
  - base
  - directory

source-dirs: …
Run Code Online (Sandbox Code Playgroud)

haskell build ffi cabal haskell-stack

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

为什么 Traversable 不能多次访问其元素?

我记得在某处读到过这样的类型不能是Traversable

data Bar a = Bar a deriving(Show)

instance Functor Bar where
  fmap f (Bar x) = Bar (f x)

instance Foldable Bar where
  foldMap f (Bar x) = f x <> f x
Run Code Online (Sandbox Code Playgroud)

我记得的一点解释是,为了foldMap = foldMapDefault保持,Traversable实例必须多次访问其元素,这是合法实例无法做到的。但是,我不记得为什么合法实例不能这样做。考虑这个:

instance Traversable Bar where
  sequenceA (Bar x) = Bar <$ x <*> x
Run Code Online (Sandbox Code Playgroud)

乍一看还不错。这样做有什么违法?

haskell traversable

8
推荐指数
2
解决办法
213
查看次数

Linux ldd 中的“静态链接”和“非动态可执行文件”有什么区别?

考虑这个 AMD64 汇编程序:

.globl _start
_start:
    xorl %edi, %edi
    movl $60, %eax
    syscall
Run Code Online (Sandbox Code Playgroud)

如果我编译gcc -nostdlib并运行ldd a.out,我得到这个:

        statically linked
Run Code Online (Sandbox Code Playgroud)

如果我改为使用gcc -static -nostdlib并运行编译它ldd a.out,我会得到这个:

        not a dynamic executable
Run Code Online (Sandbox Code Playgroud)

什么之间的区别statically linkednot a dynamic executable?如果我的二进制文件已经静态链接,为什么添加-static会影响任何事情?

linux gcc x86-64 static-linking ldd

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

如何确定一个 Enum 值是否是另一个值的后继者?

我正在尝试编写一个函数来告诉我一个是否Enum是另一个的继承者。这是我的第一次尝试:

isSuccessorOf x y = x == succ y
Run Code Online (Sandbox Code Playgroud)

看起来很合理。让我们试试看:

?> isSuccessorOf 3 2
True
?> isSuccessorOf 1 5
False
?> isSuccessorOf 3 (maxBound :: Int)
*** Exception: Prelude.Enum.succ{Int}: tried to take `succ' of maxBound
Run Code Online (Sandbox Code Playgroud)

哎呀。那应该是False。让我们确保我们不要尝试这样做succ maxBound

isSuccessorOf x y = y /= maxBound && x == succ y
Run Code Online (Sandbox Code Playgroud)

让我们再试一次:

?> isSuccessorOf 3 (maxBound :: Int)
False
?> isSuccessorOf 3 (2 :: Integer)
<interactive>:2:1: error:
    • No instance for (Bounded Integer)
        arising from a …
Run Code Online (Sandbox Code Playgroud)

enums haskell bounded-types

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

智能构造函数类型可以有多个有效的 Functor 实例吗?

这两条关于Functors 的规则是众所周知的:

  1. 你不能做一个 Functor如果类型参数以逆变方式出现,
  2. 任何类型最多有一个有效Functor实例

但如果你稍微作弊,你就可以打破第一条规则。以休斯列表为例:

data HList a = UnsafeHList ([a] -> [a])

pattern HList a <- UnsafeHList (($ []) -> a)
    where HList a = UnsafeHList (a ++)

instance Functor HList where
    fmap f (HList a) = HList (map f a)

-- instances necessary to make HList useful, but irrelevant to this example, have been omitted
Run Code Online (Sandbox Code Playgroud)

只要您假设所有HLists 都将通过智能构造函数生成,则该Functor实例是合法的,即使a出现逆变器也是如此。

我的问题:你能用类似的技术来打破第二条规则吗?Functor如果您假设它们总是通过智能构造函数创建,是否有任何类型具有两个不同的有效实例?

constructor haskell functor typeclass parametric-polymorphism

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

Remove all Docker images without containers or child images, even if they're tagged

I want to clean up all of my Docker images that aren't being used, directly or indirectly, by my current containers. Docker provides the docker image prune command for this, but I can't get it to remove exactly what I want. If I use that command without -a, it removes too little: it leaves behind all tagged images, even if no container uses them. If I use that command with -a, it removes too much: it removes images …

docker

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

为什么在枚举所有情况时通配符匹配不起作用?

考虑这个代码:

{-# LANGUAGE GADTs #-}

data P t where
 PA :: P Int
 PB :: P Double
 PC :: P Char

isA PA = True
isA _ = False
Run Code Online (Sandbox Code Playgroud)

它编译并运行良好。现在考虑这个代码:

{-# LANGUAGE GADTs #-}

data P t where
 PA :: P Int
 PB :: P Double
 PC :: P Char

isA PA = True
isA PB = False
isA PC = False
Run Code Online (Sandbox Code Playgroud)

它无法编译:

Main.hs:8:10: error:
    • Couldn't match expected type ‘p’ with actual type ‘Bool’
        ‘p’ is untouchable
          inside the …
Run Code Online (Sandbox Code Playgroud)

haskell type-inference pattern-matching gadt

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