在ghci,
:t ((+).(+))
> ((+).(+)) :: (Num (a -> a), Num a) => a -> (a -> a) -> a -> a
Run Code Online (Sandbox Code Playgroud)
但这是什么东西?请问有谁给我一个使用这个的例子吗?
一个复合2的功能如何能够分别采用2个参数?例如,如何(map.map) :: (a -> b) -> [[a]] -> [[b]]工作?
(^.^)
(-.-)
(+.+) (不禁制作出有趣的面孔.PS:我认为这意味着要告诉编译器你今天的感受)
相关文章:如何消除选择器功能的歧义?
https://ghc.haskell.org/trac/ghc/wiki/Records/OverloadedRecordFields/DuplicateRecordFields
但是,我们不会推断参数的类型来确定数据类型,或者有任何方法可以将选择推迟到约束求解器.
实际上很烦人,这个功能没有实现.我试图查找多个来源,但我找不到他们决定不推断类型的原因.
有谁知道这个的好理由?是因为当前类型系统的限制吗?
如果这是一个简单的问题,我深表歉意。我已经指定了一个输入文件,该文件位于代码源文件的同一目录中。
isprime :: Int -> [Int] -> Bool
isprime ...
main = do
handle <- openFile "primes-to-100k.txt" ReadMode
contents <- hGetContents handle
i <- getLine
print $ isprime (read i::Int) $ map (\x->read x::Int) $ lines contents
hClose handle
Run Code Online (Sandbox Code Playgroud)
当我在 shell 中使用“runhaskell Q111.hs”时,代码运行良好,但是当我使用“ghc --make Q111.hs”编译并运行时,出现错误消息
Q111: primes-to-100k.txt: openFile: 不存在(没有这样的文件或目录) 注销
关键是代码在 ghci 和 runhaskell 上运行良好,但可执行文件找不到输入 txt。
我是否必须使用某种方式向编译器提供输入 txt?
很抱歉打扰你这个简单的问题.我正在尝试学习类型系列扩展的工作原理.当我愚弄它时,我遇到了一个错误,我无法弄清楚为什么.
class Foo a b c where
data T a b c :: *
f :: a -> T a b c
g :: T a b c -> b
h :: c -> a -> b
h c a = g $ f a
Run Code Online (Sandbox Code Playgroud)
错误信息:
Could not deduce (Foo a b c0) arising from a use of ‘g’
from the context (Foo a b c)
bound by the class declaration for ‘Foo’
at DB/Internal/Typecast.hs:(17,1)-(25,19)
The type variable ‘c0’ is …Run Code Online (Sandbox Code Playgroud) 我关注Haskell懒惰评估的效率.考虑以下代码
main = print $ x + x
where x = head [1..]
Run Code Online (Sandbox Code Playgroud)
在这里,由于懒惰,x首先保持表达head [1..]而不是结果1,但是当我调用时x + x,表达式head [1..]会被执行两次吗?
我在haskell.org上找到了以下描述
另一方面,懒惰评估意味着仅在需要其结果时评估表达式(注意从"缩减"到"评估"的转变).因此,当评估引擎看到一个表达式时,它会构建一个thunk数据结构,其中包含评估表达式所需的任何值,以及指向表达式本身的指针.当实际需要结果时,评估引擎调用表达式,然后将thunk替换为结果以供将来参考.
那么这是否意味着,在x + x调用第一个时x,head [1..]执行并x重新分配1,第二个x只是调用它的引用?
我明白了吗?
对不起,这似乎是一个初学者的问题.如何在其扩展类中访问父类对象变量?
class FOO {
public $foo;
function __construct() {
$this->foo = 'string';
}
}
class Parent {
public $x;
function __construct() {
$this->x = new FOO();
var_dump($x); // this works
}
}
class Child extends Parent {
public $y;
function __construct() {
var_dump($this->x); // appears to be NULL all the time
}
}
Run Code Online (Sandbox Code Playgroud)
如何正确传递$ x的值或引用?
我的代码中有一个非常令人困惑的错误.我正在使用Data.Aeson包.我不认为这是包的错误.
class ToArrayFormat a where
getObjects :: (ToJSON b) => a -> b
toArrayFormat :: a -> Value
toArrayFormat a = toJSON $ getObjects a
Run Code Online (Sandbox Code Playgroud)
这段代码将无法使用错误消息进行编译:
Could not deduce (ToJSON s0) arising from a use of ‘toJSON’
from the context (ToArrayFormat a)
bound by the class declaration for ‘ToArrayFormat’
at <interactive>:(103,1)-(108,43)
The type variable ‘s0’ is ambiguous
In the expression: toJSON
In the expression: toJSON $ getObjects a
In an equation for ‘toArrayFormat’:
toArrayFormat a = toJSON $ getObjects a …Run Code Online (Sandbox Code Playgroud)