我想创建一个函数apply,它接受一个具有任意数量参数的函数以及一个整数列表,并返回函数的结果(其中列表中的每个整数按顺序是一个参数.
我想的是:
apply :: ([Int] -> Int) -> [Int] -> Int
apply f x:xs = apply (f x) xs
apply f [] = f
Run Code Online (Sandbox Code Playgroud)
但我知道这不会起作用,因为类型签名是错误的 - 函数不会获取整数列表,它只需要一些int参数.
另外,当我到达基本情况时,apply的f参数实际上应该是一个整数,无论如何都违反了类型签名.
有谁知道如何处理这类问题?
public class TwoBridge implements Piece{
private HashSet<Hexagon>[] permutations;
public TwoBridge(){
permutations = new HashSet<Hexagon>[6];
Run Code Online (Sandbox Code Playgroud)
嗨,我正在尝试创建一组六边形集合(六边形是我创建的一个类).
但是,当我尝试编译时,我收到此错误
oliver@oliver-desktop:~/uni/16/partB$ javac oadams_atroche/TwoBridge.java
oadams_atroche/TwoBridge.java:10: generic array creation
permutations = new HashSet<Hexagon>[6];
^
1 error
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
我正在使用re库来规范化一些文本.我想要做的一件事是将字符串中的所有大写字母替换为它们的小写等价物.最简单的方法是什么?
我目前正在尝试编写一个python脚本,在许多方面,它调用可执行文件并存储可执行文件在变量中发送给stdout的内容.这是我有的:
1 #!/usr/bin/python
2 import subprocess
3
4 subprocess.call("./pmm", shell=True)
Run Code Online (Sandbox Code Playgroud)
如何将pmm的输出存储在变量中?
我正在使用累加器创建一个连接列表的三元树的函数.
19 data Ttree t = Nil | Node3 t (Ttree t) (Ttree t) (Ttree t)
20
21 acc :: Ttree [a] -> [a]
22 acc tree = acc' tree []
23
24 acc' :: Ttree [a] -> [a] -> [a]
25 acc' Nil rest = rest
26 acc' (Node3 xs l m r) rest =
27 xs $ acc' l $ acc' m $ acc' r rest
Run Code Online (Sandbox Code Playgroud)
我使用ghc得到了这个错误.但我不知道xs的预期类型是如何 - > b:
e6.hs:26:4:
Couldn't match expected type `a -> b' …Run Code Online (Sandbox Code Playgroud) 1 public class TestWin{
2 public static void main(String[] args){
3 int n;
4 hexagon[][] board;
5
6 n = 4;
7 board = new hexagon[n][n];
8 board[0][0].value = 'R';
Run Code Online (Sandbox Code Playgroud)
你好.javac不喜欢我在第8行所做的.有谁知道为什么?
这是我的代码,用于在低于10,000,000的fibonnacci序列中生成值.
3 fibs = [1,1]
4 while((x = fibs[-1] + fibs[-2]) <= 10000000):
5 fibs.append(x)
Run Code Online (Sandbox Code Playgroud)
我尝试在while循环的条件下进行x样式的C赋值.不幸的是,python告诉我这是一个语法错误.什么是最简单的解决方案?
我目前正在实现一个代表 52 张纸牌的 Deck 类。它使用 boost Random 库来洗牌代表卡片的整数。
#include <iostream>
#include <fstream>
#include "constants.hpp"
#include <boost/program_options.hpp>
#include <vector>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/random/variate_generator.hpp>
boost::mt19937 gen(std::time(0));
class Deck{
private:
std::vector<int> cards;
int cardpointer;
static ptrdiff_t choosecard(ptrdiff_t i);
ptrdiff_t (*pchoosecard)(ptrdiff_t);
public:
Deck();
void shuffle();
int pop();
};
Deck::Deck(){
for(int i=1; i<=52; i++){
cards.push_back(i);
}
cardpointer = -1;
pchoosecard = &choosecard;
}
ptrdiff_t Deck::choosecard(ptrdiff_t i){
boost::uniform_int<> dist(0,i);
boost::variate_generator< boost::mt19937&, boost::uniform_int<> > cardchoice(gen, dist);
return cardchoice();
}
void Deck::shuffle(){
std::random_shuffle(cards.begin(), cards.end(), pchoosecard);
} …Run Code Online (Sandbox Code Playgroud) std::vector< boost::variant<std::string, int> > vec;
std::string s1("abacus");
int i1 = 42;
vec.push_back(s1);
vec.push_back(i1);
std::cout << vec.at(0).size() << "\n";
Run Code Online (Sandbox Code Playgroud)
当我尝试运行此代码时,我收到以下错误:
main.cpp:68: error: ‘class boost::variant<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_>’ has no member named ‘size’
make: *** [main.o] Error 1
Run Code Online (Sandbox Code Playgroud)
但是,作为一个字符串,它应该有一个size()方法.我不确定出了什么问题.注意用最后一行替换:
std::cout << vec.at(0) << "\n";
Run Code Online (Sandbox Code Playgroud)
将按预期打印"算盘".
我正在和合作伙伴一起编写代码.我们的make文件略有不同,不同的构建设置.因此,到目前为止我们还没有跟踪这个文件.但是至少有一个跟踪我们会很高兴.问题是,当完成并且另一个人运行时hg update,他们的副本会更新并且代码将无法编译.
有没有办法跟踪文件,但是有这样的方法可以选择性地更新工作目录吗?还是有其他方法我应该处理这个问题?
class Node:
children = {}
sequence = [1,2,3,4,5]
tree = Node()
node = tree
for item in sequence:
if item not in node.children:
node.children[item] = Node()
node = node.children[item]
print tree.children.keys()
Run Code Online (Sandbox Code Playgroud)
我希望上面的代码输出[1],但它输出[1, 2, 3, 4, 5].这是为什么,我将如何解决它?