小编cha*_*oin的帖子

Haskell为什么"Num x"需要"Show x"?

最近我用LYAH看了看Haskell .

我正在搞乱类型类并编写了这个快速测试函数:

foo :: (Num x) => x -> String
foo x = show x ++ "!"
Run Code Online (Sandbox Code Playgroud)

但是这会产生这个错误:

test.hs:2:9:
    Could not deduce (Show x) arising from a use of `show'
    from the context (Num x)
    bound by the type signature for foo :: Num x => x -> String
    at test.hs:1:8-29
    Possible fix:
      add (Show x) to the context of
        the type signature for foo :: Num x => x -> String
Run Code Online (Sandbox Code Playgroud)

但据LYAH说:

要加入Num,类型必须已经是Show和Eq的朋友.

所以,如果所有的Num的一个子集 …

haskell

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

ghci自引用赋值

今天我正在学习一些新的Haskell,当时我在ghci中尝试了一些东西.它基本归结为:

Prelude> let x = 6
Prelude> x
6
Prelude> let y = show x
Prelude> y
"6"
Prelude> let x = show x
Prelude> x
"\"\\\"\\\\\\\"\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\" --(repeats)
Run Code Online (Sandbox Code Playgroud)

那么ghci在作业中不能自我引用吗?我觉得它类似于i = i++;C语言,或试图引用Scheme中let(不let*)的先前作业.无论如何要做到这一点,还是我应该更容易使用let y = show x

haskell ghci

7
推荐指数
2
解决办法
278
查看次数

表长度运算符的性能

Lua有#运算符来计算用作数组的表的"长度".在诸如C之类的语言中,在计算出某事物的长度之后,通常不再计算它.例如int len = strlen(string);

这在Lua中有什么不同吗?一个效率低于另一个吗?

(显然,对于相当小的表格,这可能不会显示出明显的差异,但知道这一点并不坏.)

c performance lua lua-table

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

Haskell部分应用函数更快?

我正在搞乱Haskell,并想知道这两个函数之间的性能是否存在差异:

count :: (Eq a) => [a] -> a -> Int
count xs e = foldr countCheck 0 xs
    where countCheck x
            | x == e = (1+)
            | otherwise = (0+)

count' :: (Eq a) => [a] -> a -> Int
count' xs e = foldr countCheck 0 xs
    where countCheck x acc
            | x == e = acc + 1
            | otherwise = acc
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下代码作为基准:main = print (count [1..10000] 1)这导致第一个(使用部分应用+)函数平均稍快一些.

我主要想知道,因为对我而言,count阅读比阅读更加困惑count' …

performance haskell

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

标签 统计

haskell ×3

performance ×2

c ×1

ghci ×1

lua ×1

lua-table ×1