我在这里有一个函数,用于查看元组列表,并通过获取第一个值来查找元组中的第二个值.这是迄今为止的功能:
lookup :: String -> [(String,String)] -> String
lookup _ _ [] = "Not found"
lookup x y zs = if (notFound x zs)
then "Not found"
else (head [b | (a,b) <- zs, (a==x)])
Run Code Online (Sandbox Code Playgroud)
如果没有包含给定第一个字符串的元组,则notFound函数只返回一个Bool为true.问题是,我在Hugs中遇到这种类型的错误:
ERROR "find.hs" (line 22): Type error in explicitly typed binding
*** Term : lookup
*** Type : String -> [(String,String)] -> [a] -> String
*** Does not match : String -> [(String,String)] -> String
Run Code Online (Sandbox Code Playgroud)
我认为它与虚拟的"未找到"值有关,它与生成列表中的字符串具有不同的类型,但我不确定.
我有一个可能是多种类型的变量 - 有时它是一个字符串,有时是数字,表格或bool.我试图每次打印出变量的值,如下所示:
print("v: "..v)
Run Code Online (Sandbox Code Playgroud)
与v
是我的变量.问题是,当我得到一个无法连接的值时,我收到此错误:
myscript.lua:79:尝试连接表值
我已经尝试将其更改为此,以防它设法检测是否可以打印变量:
print("v: "..(v or "<can't be printed>"))
Run Code Online (Sandbox Code Playgroud)
但那里我遇到了同样的问题.是否有某种函数可用于确定变量是否可以连接到字符串,还是更好的打印变量的方法?
我正在为我在Haskell中编写的基本编译器制作一个扫描程序.其中一个要求是用单引号(')括起来的任何字符都被翻译成字符文字标记(类型为T_Char),这包括转义序列,如'\n'和'\ t'.我已经定义了扫描仪功能的这一部分,在大多数情况下都可以正常工作:
scanner ('\'':cs) | (length cs) == 0 = error "Illegal character!"
| head cs == '\\' = mkEscape (head (drop 1 cs)) : scanner (drop 3 cs)
| head (drop 1 cs) == '\'' = T_Char (head cs) : scanner (drop 2 cs)
where
mkEscape :: Char -> Token
mkEscape 'n' = T_Char '\n'
mkEscape 'r' = T_Char '\r'
mkEscape 't' = T_Char '\t'
mkEscape '\\' = T_Char '\\'
mkEscape '\'' = T_Char '\''
Run Code Online (Sandbox Code Playgroud)
但是,当我在GHCi中运行它时会出现这种情况:
Main> scanner "abc …
Run Code Online (Sandbox Code Playgroud) 我有一些代码涉及迭代这样的std::hash_set
事情:
typedef std::hash_set< VEdge, VEdge > MyHashSet;
MyHashSet hs;
for( int i=0; i < numFaces; ++i )
{
VEdge myEdge( someValue, someOtherValue );
MyHashSet::iterator it = hs.find(myEdge);
if ( it->face[0] == -1 )
it->face[0] = i; // Error: "Read-only variable is not assignable"
}
Run Code Online (Sandbox Code Playgroud)
我已经改变了代码来简化它,但你可以看看它在做什么.VEdge
在具有int face[2]
其成员之一的结构中定义.我遇到的问题是,Clang告诉我这it->face[0]
是只读的,即使这个相同的代码在MSVC上运行(或者我被告知).有没有关于Clang的事情意味着我必须以face
某种方式将变量定义为可写的?
我有以下代码,有人在MSVC上工作给我:
#define MAP1(x, y) map[#x] = #@y;
Run Code Online (Sandbox Code Playgroud)
我在Xcode上,使用Clang,从各种谷歌搜索中我发现这被称为"charizing运算符",并且特定于MSVC的预处理器.有没有办法在使用Clang时模拟此运算符的功能?我尝试删除@
但收到以下错误消息:
Assigning to 'int' from incompatible type 'const char[2]'
Run Code Online (Sandbox Code Playgroud)
是否会对'int'进行明确的演绎或者是一个不同的操作员?
我有一些代码可以在MSVC下编译好(或者说发送给我的Windows开发人员),但是在CLang下会出错.环顾四周后,我发现CLang确实对解决模板专业化问题更加严格,但我不确定在我的案例中应该把这些专业化放在哪里.基本上我的一个文件有这样的结构:
template<>
struct iterator_traits< char * > // error is here
{
typedef random_access_iterator_tag iterator_category;
typedef char value_type;
typedef ptrdiff_t difference_type;
typedef difference_type distance_type;
typedef char * pointer;
typedef char & reference;
};
Run Code Online (Sandbox Code Playgroud)
这是一个namespace std
街区.错误消息是:
Explicit specialization of 'std::iterator_traits<char *>' after instantiation
Run Code Online (Sandbox Code Playgroud)
同一错误消息的另一部分(通过'扩展'Xcode中的错误消息查看)说Implicit instantiation first required here
,并点击它带我到stl_iterator.h
,特别是这一行(第642行):
typedef typename iterator_traits<_Iterator>::iterator_category
iterator_category;
Run Code Online (Sandbox Code Playgroud)
在这种情况下,有谁知道正确的做法是什么?我见过涉及类的例子,但从来没有涉及结构的例子.
我在Xcode(4.5.2)中有一个使用Debug配置构建的项目.但是,现在我已经切换到构建Release配置,我遇到了一个问题:我的一个内联汇编函数正在收到错误Invalid symbol redefinition
.谷歌搜索该错误消息发现我有一些人有编译器错误,但没有关于它意味着什么的信息.这是函数,带有注释的错误行:
inline int MulDivAdd(int nNumber,
int nNumerator,
int nDenominator,
int nToAdd)
{
int nRet;
__asm__ __volatile__ (
"mov %4, %%ecx \n"
"mov %1, %%eax \n"
"mull %2 \n"
"cmp $0, %%ecx \n"
"jl __sub \n"
"addl %%ecx, %%eax \n"
"adc $0, %%edx \n"
"jmp __div \n"
"__sub: \n" // "Invalid symbol redefinition"
"neg %%ecx \n"
"subl %%ecx, %%eax \n"
"sbb $0, %%edx \n"
"__div: \n" // "Invalid symbol redefinition"
"divl %3 \n"
"mov %%eax, %0 \n" …
Run Code Online (Sandbox Code Playgroud) 我必须创建一个函数,它给出一个键(作为a String
),一个值(作为a String
)和一个键和值的关联列表(as [(String, String)]
).该函数用于将键/值对添加到列表的末尾,如果键已经存在于具有关联值的列表中,则删除旧值.
我已经尝试使用lookup
键和关联列表,但我不知道如何处理输出 - lookup
函数的输出类型是Maybe String
,我似乎无法对其执行列表函数(如删除元素) .有什么方法可以查看列表并删除任何具有给定键的列表元素,而不知道与之关联的值?
我正在尝试使用表单和PHP函数将值输入到数据库表中.PHP似乎很好,因为它创建的SQL语句看起来没问题,但数据库总是会抛出错误.这是我的代码生成的SQL语句(具有任意值):
INSERT INTO Iteminfo ('itemName', 'itemSeller', 'itemCategory', 'itemDescription', 'itemPrice', 'itemPostage', 'itemBegin', 'itemEnd', 'buynow', 'itemPicture')
values ('gorillaz album', 'ben', 'music', 'new one ', '5.00', '1.00', '2010-03-15 14:59:51', '2010-03-16 14:59:51', '0', 'http://www.thefader.com/wp-content/uploads/2010/01/gorillaz-plastic-beach.jpg')
Run Code Online (Sandbox Code Playgroud)
当我使用PHP函数来评估查询时以及当我使用phpMyAdmin手动输入查询时,这会引发错误.但是,我看不出它有什么问题.任何人都可以对此有所了解吗?除了itemPrice和itemPostage(存储为DECIMAL(4,2))以及itemBegin和itemEnd之外,所有字段都是VARCHAR值,它们存储为DATETIME.
我刚刚部署了我的应用程序,一旦进入主页,我就会收到"500内部服务器错误"页面.查看日志后,我收到以下错误:
类型'exceptions.SyntaxError'>:第465行的文件/base/data/home/apps/spare-wheels/1.348259065130939449/sparewheels.py中的非ASCII字符'\ xc2',但未声明编码; 有关详细信息,请访问http://www.python.org/peps/pep-0263.html(sparewheels.py,第465行)
有问题的行看起来像这样:
self.template_values['price_pounds'] = "£%.2f" % (float(self.event.price_pence)/100)
Run Code Online (Sandbox Code Playgroud)
这在localhost上运行时工作正常:Google Apps版本的Python不支持数字格式吗?
我目前有这个代码用于填充要返回的注释列表:
foreach ($comments as $comment)
{
$f_comments[] = array (
'comment_id' => $comment['comment_id'],
'comment_body' => $comment['comment_body'],
'user_id' => $comment['user_id'],
'user_avatar' => $comment['user_avatar'],
'user_username' => $comment['user_username'],
'timeago' => formatter_common_format_time_string($comment['added']),
);
if (!empty($comment['user_picture']))
{
$f_comments[]['user_picture'] = $comment['user_picture'];
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,这是返回的内容:
"comments":[{"comment_id":9386,"comment_body":"Comment","user_id":46542,"user_avatar":"9","user_username":"TestUser","timeago":"about 2 hours ago"},{"user_picture":"ec237f517bc26b27d8e790c3a5d125841321552075"}]
Run Code Online (Sandbox Code Playgroud)
...而我希望user_picture与其他结果一起使用,如下所示:
[{"comment_id":9386,"comment_body":"Comment","user_id":46542,"user_avatar":"9","user_username":"TestUser","timeago":"about 2 hours ago", "user_picture":"ec237f517bc26b27d8e790c3a5d125841321552075"}]
Run Code Online (Sandbox Code Playgroud)
......但是我看不到这样做的方法.我对PHP很陌生,所以我可能会遗漏一些明显的东西.任何人都可以看到什么是错的?
我正在尝试使用C++为Mac制作SDL游戏.我已经完成了与SDL相关的所有工作,但是当我尝试使用sqrt函数时,我得到了错误no matching function for call to 'sqrt'
.这是我的包含列表和我的第一个函数(main()函数现在没有真正做任何事情):
#include <iostream>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <math.h>
#include "geometry.h"
using namespace std;
void drawLine(vector2 start, vector2 end)
{
double x = end.x - start.x;
double y = end.y - start.y;
double length = sqrt(x*x, y*y); // I get the error here
}
Run Code Online (Sandbox Code Playgroud)
我也试过导入相同的结果.有什么特别的我必须在Mac(Lion,XCode 4.3)上做这个工作吗?
编辑:为了完整性,geometry.h包含vector2结构的定义
我有以下功能,涉及GAS语法中的i386程序集片段:
inline int MulDivRound(
int nNumber,
int nNumerator,
int nDenominator )
{
int nRet, nMod;
__asm__ __volatile__ (
"mov %2, %%eax \n"
"mull %3 \n"
"divl %4 \n"
"mov %%eax, %0 \n"
"mov %%edx, %1 \n"
: "=m" (nRet),
"=m" (nMod)
: "m" (nNumber),
"m" (nNumerator),
"m" (nDenominator)
: "eax", "edx"
);
return nRet + nMod*2 / nDenominator;
}
Run Code Online (Sandbox Code Playgroud)
我注意到,在一些情况下,我正在EXC_I386_DIV
使用此功能崩溃.以下调用会产生这样的崩溃:
int res = MulDivRound( 4096, -566, 400 );
Run Code Online (Sandbox Code Playgroud)
我无法清楚地看到正在发生的事情导致此函数除以0:当然它只是移动4096 eax
,然后乘以-566,然后将其除以400,返回除法运算结果的两个分量.任何人都可以对此有所了解吗?