小编Nic*_*ard的帖子

如何在 GHCi 中配置 Vi 模式?

我刚刚了解了 GHCi 中的 Vi 模式。我已经添加editMode: Vi到我的 ~/.haskeline 文件中。我希望完成两项任务。

  1. 我想将我的转义键重新映射到jj.

  2. 我想配置 GHCi 以显示我所处的模式类似于我如何添加set show-mode-in-prompt on到我的~/.inputrc文件以进行 readline。

我已经阅读了一些haskeline 文档,但无法找到答案。

haskell

5
推荐指数
0
解决办法
107
查看次数

当谓词回溯到相同的解决方案时,使用切割是否有意义?

我正在编写一个简单的矩阵库,我偶然发现了一个解决方案,它提供了一个了解切割目的的机会。

boolean_mult(1,1,1).
boolean_mult(1,0,0).
boolean_mult(0,1,0).
boolean_mult(0,0,0).

binary_dot_product([B1],[B2],Solution) :-
    boolean_mult(B1,B2,Solution).
binary_dot_product([1|_],[1|_],1). % computation can end here.
binary_dot_product([_|B1s],[_|B2s],Solution) :-
    binary_dot_product(B1s,B2s,Solution).
Run Code Online (Sandbox Code Playgroud)

从声明性的角度来看,这个解决方案是有意义的。给定两个 1 和 0 的列表,B1 和 B2,如果它们头部的布尔积是 1,那么列表的点积就是 1。否则取它们尾部的点积。单例列表的点积是两个元素的布尔积。

我遇到的问题显示在这个查询中:

?- binary_dot_product([1,0,1],[1,1,1],X).
X = 1 ;
X = 1 ;
X = 1.
Run Code Online (Sandbox Code Playgroud)

我能够回溯三次,从声明的角度来看这是没有意义的。只能有一个点积!不是3!

我可以通过使用 cut轻松解决这个问题

binary_dot_product([B1],[B2],Solution) :-
    boolean_mult(B1,B2,Solution).
binary_dot_product([1|_],[1|_],1) :- !. 
binary_dot_product([_|B1s],[_|B2s],Solution) :-
    binary_dot_product(B1s,B2s,Solution).
Run Code Online (Sandbox Code Playgroud)

现在:

?- binary_dot_product([1,0,1],[1,1,1],X).
X = 1.
Run Code Online (Sandbox Code Playgroud)

到目前为止,在我的 Prolog 职业生涯中,我已经避免了像瘟疫一样的削减,因为我已经被警告过它们的危险。在这种情况下,虽然我觉得裁员很有意义。

在上面的代码中使用 cut 会得到什么和失去什么?

prolog

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

努力理解“无法构造无限类型”错误

我正在尝试解决 HackerRank 问题,但遇到了一个我无法弄清楚的错误。问题出在solve1函数上。确切的错误是:cannot construct the infinite type a ~ t0 a. Expected type ([t0 a], [t0 a], [t0 a]) Actual type ([a], [a], [a]). In the second argument of `tripleMap`, namely '(tripList xs)'

我一直看着这些类型,它们在我眼中继续显得正确。tripList获取数字列表并返回数字列表的三元组。tripleMap将一组数字列表作为其第二个参数。

tripList我的 REPL 中进行测试时,我得到了想要的结果:

> tripList [1,0,-1,0,1] 
([1,1],[0,0],[-1])
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

length' :: (Foldable t, Num b, Fractional b, Ord b) => t a -> b
length' = foldr (\_ acc -> 1 + acc) 0

tripList :: (Num …
Run Code Online (Sandbox Code Playgroud)

haskell

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

标签 统计

haskell ×2

prolog ×1