我正在编写一个使用UTF-16字符串的应用程序,并且为了利用重载的字符串扩展,我试图IsString为它创建一个实例:
import Data.Word ( Word16 )
import Data.String ( IsString(fromString) )
type String16 = [Word16]
instance IsString [Word16] where
fromString = encodeUTF16
encodeUTF16 :: String -> String16
Run Code Online (Sandbox Code Playgroud)
问题是,当我尝试编译模块时,GHC 7.0.3抱怨:
Data/String16.hs:35:10:
Illegal instance declaration for `IsString [Word16]'
(All instance types must be of the form (T a1 ... an)
where a1 ... an are *distinct type variables*,
and each type variable appears at most once in the instance head.
Use -XFlexibleInstances if you want to disable this.)
In the …Run Code Online (Sandbox Code Playgroud) 我知道我可以在Git中删除远程分支git push.(请参阅如何在Git中删除远程分支?).但我不能在Mercurial做同样的事情.
我试过了hg bookmark -d something,但是当我使用hg-git推送到Git存储库时,它不会删除远程存储库上的书签.
当我尝试时hg bookmark -d origin/something,它抱怨它不存在.
考虑以下枚举器:
var items = (new int[] { 1, 2, 3, 4, 5 }).Select(x =>
{
Console.WriteLine($"inspect {x}");
return x;
});
Run Code Online (Sandbox Code Playgroud)
这会产生元素[1, 2, 3, 4, 5],在消耗时打印它们.
当我Last在这个枚举器上调用该方法时,它会触发一个只访问单个元素的快速路径:
items.Last();
Run Code Online (Sandbox Code Playgroud)
inspect 5
Run Code Online (Sandbox Code Playgroud)
但是当我将回调传递给Last它时,它从头开始遍历整个列表:
items.Last(x => true);
Run Code Online (Sandbox Code Playgroud)
inspect 1
inspect 2
inspect 3
inspect 4
inspect 5
Run Code Online (Sandbox Code Playgroud)
浏览.NET Core源代码,我发现:
Last(IEnumerable<T>)转发TryGetLast(IEnumerable<T>, out bool) ;TryGetLast(IEnumerable<T>, out bool)有一条快速的道路IPartition<T> ;ArraySelectIterator<T>工具IPartition<T>,这个快速路径被触发,一切都很顺利.另一方面:
在查看变压器包时,我发现了这个名为IdentityT的 monad变换器.
虽然我理解如何使用Identity monad(例如State只是别名StateT Identity)以及monad变换器如何工作,但我不知道它是如何相关的IdentityT.
因为它不在MTL中,所以我猜它是为了完整而添加在那里并没有实际用途.那是对的吗?
我在一个应用程序中使用optparse-applicative库,该应用程序在命令行上接受多个字符串并对每个字符串执行操作.我的第一次尝试是这样的:
arguments Just
( metavar "EXPR"
& help "Expressions to render, in zero-based De Bruijn index notation" )
Run Code Online (Sandbox Code Playgroud)
不幸的是,这允许在没有参数的情况下运行程序,即使它没有多大意义.
我的第二次尝试涉及分别解析第一个参数,然后将其转移到列表的其余部分:
(:) <$> argument Just ( metavar "EXPR" )
<*> arguments Just ( metavar "EXPR" )
Run Code Online (Sandbox Code Playgroud)
这应该有效,但它没有:当调用时--help,解析器吞噬它并处理它而不是显示帮助文本.
所以我的问题是:如何配置optparse以至少需要一个参数?
当我尝试在GHC 7.4.1下加载以下代码时:
{-# LANGUAGE RankNTypes #-}
import Control.Monad.ST
newtype M s a = M { unM :: ST s a }
runM :: (forall s. M s a) -> a
runM (M m) = runST m
Run Code Online (Sandbox Code Playgroud)
它失败并显示以下消息:
test.hs:9:14:
Couldn't match type `s0' with `s'
because type variable `s' would escape its scope
This (rigid, skolem) type variable is bound by
a type expected by the context: ST s a
The following variables have types that mention s0
m :: ST …Run Code Online (Sandbox Code Playgroud) Stringimplements Deref<Target = str>,表示以下代码编译:
fn save(who: &str) {
println!("I'll save you, {}!", who);
}
save(&String::from("Madoka"));
Run Code Online (Sandbox Code Playgroud)
如果我创建一个也实现的自定义类型Deref<Target = str>,那么它也适用:
struct Madoka;
impl Deref for Madoka {
type Target = str;
fn deref(&self) -> &Self::Target {
"Madoka"
}
}
save(&Madoka);
Run Code Online (Sandbox Code Playgroud)
现在,让我们尝试强制转换到另一种类型 - 比如说u32.它似乎也有效:
fn reset(how: &u32) {
println!("Reset {} times", how);
}
struct Homura;
impl Deref for Homura {
type Target = u32;
fn deref(&self) -> &Self::Target {
&42
}
}
reset(&Homura);
Run Code Online (Sandbox Code Playgroud)
但是当我将表达式包装在一个块中时,它不再编译:
reset(&{ Homura }); …Run Code Online (Sandbox Code Playgroud) 我已经对QName做了相当多的阅读,但我找不到任何关于如何使用它的好例子.有人能给我一个简单的例子来说明如何使用QName并解释它将用于什么上下文?
我正在编写一个简单的全文本搜索库,并且需要折叠大小写以检查两个单词是否相等。对于此用例,现有的.to_lowercase()和.to_uppercase()方法还不够。
从crates.io的快速搜索中,我可以找到用于规范化和分词的库,但不能区分大小写。regex-syntax确实具有大小写折叠代码,但是未在其API中公开。
如果没有现有的解决方案,那我可能不得不自己动手
section\nopen classical\nexample : \xc2\xac (A \xe2\x86\x94 \xc2\xac A) :=\nhave hn : (A \xe2\x88\xa8 \xc2\xac A), from sorry,\n assume h : (A \xe2\x86\x94 \xc2\xac A),\n show false, from or.elim hn\n (assume h1 : A, h.mp h1 h1)\n (assume h2 : \xc2\xac A, h2 (h.mpr h2))\nend\nRun Code Online (Sandbox Code Playgroud)\n我正在阅读 Jeremy Avigad 的《逻辑与证明》一书来学习精益。问题是我只能用by_contradiction战术来应对排中法则。有人可以帮我吗?
换句话说,可以优化以下内容Just [1..]吗?
> sequence (map Just [1..])
*** Exception: stack overflow
Run Code Online (Sandbox Code Playgroud)
还有一个更具体的例子,data61/fp-course如果Empty存在一个值,则预计会提前终止。
seqOptional ::
List (Optional a)
-> Optional (List a)
seqOptional =
foldRight f (Full Nil)
where
f Empty _ = Empty
f _ Empty = Empty
f (Full a) (Full as) = Full (a :. as)
Run Code Online (Sandbox Code Playgroud)
为什么改变前两种模式的顺序会使函数永远循环,好像永远Empty无法匹配一样?我模糊地理解这样的定义会f在无限列表中变得严格,但我看不出是什么导致了这种情况。
还是这些不相关的问题?
附带问题:堆栈用尽而不是堆用尽是否重要?