假设我在haskell中有一个通用的递归定义,如下所示:
foo a0 a1 ... = base_case
foo b0 b1 ...
| cond1 = recursive_case_1
| cond2 = recursive_case_2
...
Run Code Online (Sandbox Code Playgroud)
它总是可以用foldr重写吗?可以证明吗?
我正在寻找一种在 vim 中编写和编辑较长段落的技术。本质上,我想要的是 vim 表现得像我目前正在写这个问题的 html textarea。
在某种程度上,如果我set wrap和映射到并映射到set linebreak,我就会得到这种行为。然而,在视觉模式下,移动仍然是线状的,我知道没有方便的方法来选择长线的一部分。jgjkgk
此外,我知道textwidth如果光标的当前列高于某个阈值,该选项会自动换行。但是,如果我从中间编辑该行,当它变得太长时,该行不会自动断开。
我也知道我可以gq用来格式化一组选定的行,但一段时间后它会变得很烦人。
在 vim 中自动换行的技术是什么?
假设我有以下几点:
public interface Foo {
...
}
public class Gin {
...
}
public class Fizz {
...
}
public class Buzz {
public Foo getAFoo() {
...
}
public void test() {
Foo myfoo = getAFoo();
if (myFoo instanceof Bar) {
Bar myBar = (Bar) myFoo;
//do something more
} else {
//throw new something exception
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是合理的编程吗?是否有内置的异常可以抛出test()或我应该为此创建自己的异常类?
对我来说,整数集似乎是一个可折叠的数据结构.为什么Data.IntSet不是一个实例Foldable?
我的目的是用上find一个IntSet.我该如何实现find Data.IntSet?
我的代码中有以下情况(简化但语义相同)
class Graph a where
edges :: EdgeSet c => a -> c
Run Code Online (Sandbox Code Playgroud)
我有许多子类型可以实现图形界面.其中一个是树
data Tree = Tree
instance Graph Tree where
edges tree = DirectedEdgeSet
Run Code Online (Sandbox Code Playgroud)
对edge方法的调用应返回a DirectedEdgeSet.这应该没问题,因为DirectedEdgeSet实现了EdgeSet类:
type Edge = (Int, Int)
data DirectedEdgeSet = DirectedEdgeSet (Set Edge) Int
class EdgeSet c where
content :: c -> Set Edge
size :: c -> Int
instance EdgeSet DirectedEdgeSet where
content (DirectedEdgeSet es _) = es
size (DirectedEdgeSet _ x) = x
Run Code Online (Sandbox Code Playgroud)
此示例无法编译:
• Couldn't match …Run Code Online (Sandbox Code Playgroud) 我有以下数据和功能
data Foo = A | B deriving (Show)
foolist :: Maybe Foo -> [Foo]
foolist Nothing = [A]
foolist (Just x) = [x]
prop_foolist x = (length (foolist x)) == 1
Run Code Online (Sandbox Code Playgroud)
在运行时quickCheck prop_foolist,ghc告诉我Foo必须是任意实例。
No instance for (Arbitrary Foo) arising from a use of ‘quickCheck’
In the expression: quickCheck prop_foolist
In an equation for ‘it’: it = quickCheck prop_foolist
Run Code Online (Sandbox Code Playgroud)
我尝试过data Foo = A | B deriving (Show, Arbitrary),但这导致
Can't make a derived instance of ‘Arbitrary Foo’: …Run Code Online (Sandbox Code Playgroud) 如果我有一个泛型类,
public class Graph<K, V extends Comparable> {
...
}
Run Code Online (Sandbox Code Playgroud)
我的理解是,任何类型的对象V都具有可比性,因为它扩展了Comparable接口.现在我想HashMap<K, V>在课堂上使用.V我的地图中的类型对象仍应具有可比性.我声明了一个方法:
public V getMin(HashMap<K, V> map, V zero) {
V min = zero;
for (V value : map.values()) {
if (value.compareTo(min) < 0) {
min = value;
}
}
return min;
}
Run Code Online (Sandbox Code Playgroud)
编译时,我收到警告
warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type
Comparable
if (value.compareTo(min) < 0) {
where T is a type-variable:
T extends Object declared in interface …Run Code Online (Sandbox Code Playgroud) 我想s通过迭代一个简单结构的向量来构建一个字符串,acc根据结构附加不同的字符串.
#[derive(Clone, Debug)]
struct Point(Option<i32>, Option<i32>);
impl Point {
fn get_first(&self) -> Option<i32> {
self.0
}
}
fn main() {
let mut vec = vec![Point(None, None); 10];
vec[5] = Point(Some(1), Some(1));
let s: String = vec.iter().fold(
String::new(),
|acc, &ref e| acc + match e.get_first() {
None => "",
Some(ref content) => &content.to_string()
}
);
println!("{}", s);
}
Run Code Online (Sandbox Code Playgroud)
运行此代码会导致以下错误:
error: borrowed value does not live long enough
Some(ref content) => &content.to_string()
^~~~~~~~~~~~~~~~~~~
note: reference must be valid …Run Code Online (Sandbox Code Playgroud) 我正在学习Rust,并遇到类似的事情
struct A<T> {
some_vec: Vec<T>,
pub secret: &'static str
}
struct B{}
struct C{}
impl B {
fn foo<T>(&self) {
let bar: A<T> = A {
some_vec: Vec::new(),
secret: "123"
};
println!("The secret is {}", bar.secret);
}
}
impl C {
fn foo<T>(&self) {
let b = B{};
b.foo();
}
}
fn main() {
let c = C{};
c.foo();
}
Run Code Online (Sandbox Code Playgroud)
这会产生编译器错误
error: unable to infer enough type information about `_`; type annotations or generic parameter binding required [--explain E0282] …Run Code Online (Sandbox Code Playgroud) 我试图为一些C++数据结构编写一个C包装器.现在我有以下内容foo.cpp
typedef std::map<unsigned int, void *> _Map;
extern "C"{
void* map_create()
{
return reinterpret_cast<void*> (new _Map);
}
void map_put(void *map, unsigned int k, void *v)
{
Map *m = reinterpret_cast<_Map *> (map);
m->insert(std::pair<unsigned int, void *>(k, v));
}
}
Run Code Online (Sandbox Code Playgroud)
在foo.h我有
#ifdef __cplusplus
#define EXTERNC extern "C"
#else
#define EXTERNC
#endif
typedef void *Map;
EXTERNC void *map_create();
EXTERNC void map_put(void* map, unsigned int k, int v);
Run Code Online (Sandbox Code Playgroud)
我包括foo.h,我很高兴去.
现在,我想迭代一张地图并注意到C++通过迭代器来做到这一点.我没有使用C++的经验,也不知道如何实现迭代器.
我可以std::map使用C包装器迭代吗?这些函数定义将如何呈现?我将如何在我的C代码中的for循环中使用它们?