考虑下一个代码GHCi:
*> data R = R { s :: Text }
*> instance Show R where show x = unpack $ s x
*> let r = R $ pack "¶"
*> r
¶
*> s r
"\182"
Run Code Online (Sandbox Code Playgroud)
为什么它显示没有引号的段落符号以及显示R数据类型时.在显示时显示[Char](实际上:t s r是Text)s r?
我有一个选项记录,我需要转换成命令行参数.
例如:
data Options = Options { optFoo :: Maybe Int
, optYes :: Bool
, optBar :: Maybe String
}
options = Options { optFoo = Just 3
, optYes = True
, optBar = Nothing
}
callThing :: Options -> IO ()
callThing opts = do
callProcess "/usr/bin/thing" $ optsToArgs opts
-- Output should be: ["--foo", "3", "-y"]
optsToArgs :: Options -> [String]
optsToArgs opts = ???
Run Code Online (Sandbox Code Playgroud)
我想象能够使用List Monad,但我无法弄清楚如何让它工作.
在我的特定情况下,Options中有大约20种不同的东西,因此使用嵌套的if/case语句的解决方案并不理想.
有没有解决这类问题的共同模式?
我有一个功能ip,我无法改变:
ip = show a ++ show b ++ show c ++ show d
Run Code Online (Sandbox Code Playgroud)
我必须定义a,b,c并d因此:
GHCi> ip
"127.224.120.12"
Run Code Online (Sandbox Code Playgroud)
成立.我怎么做?
假设我写了一个纯函数f,其签名是
f :: Int -> Int
Run Code Online (Sandbox Code Playgroud)
如何编写main函数以显示1000个第一个值,如下所示:
(1,f(1)), (2,f(2)), ..., (1000,f(1000))?
Run Code Online (Sandbox Code Playgroud) 如何在python3中计算列表中每个元素的总和?虽然我能做到,有什么聪明的方法吗?
data = [[1,2],[1], [3,4,2]]
sum_length = 0
for d in data:
sum_length += len(d)
print(sum_length) # 6
Run Code Online (Sandbox Code Playgroud) 因此,我有一个由我的搜寻器制作的疯狂的长文本文件,由于某种原因,它在链接之间添加了一些空格,如下所示:
https://example.com/asdf.html (note the spaces)
https://example.com/johndoe.php (again)
Run Code Online (Sandbox Code Playgroud)
我想摆脱这一点,但要保留新行。请记住,文本文件的长度为4.000+行。我尝试自己做,但是发现我不知道如何在文件中的新行之间循环。
您好我想定义plusTree.
对此的定义.

data Tree = Null | Node Int Tree Tree deriving Show
plusTree :: Tree -> Tree -> Tree
plusTree Null ys = Null
plusTree xs Null = Null
plusTree (Node x xs) (Node y ys) = Node (x+y) (plusTree xs ys)
Run Code Online (Sandbox Code Playgroud)
我创建了上面的代码.
构造函数"Node"应该有3个参数,但是已经给出了2个参数
在模式中:节点x xs
在`plusTree'的等式中:plusTree(Node x xs)(Node y ys)= Node(x + y)(交互式:IHaskell544.plusTree xs ys)
我也遇到了上述错误.所以我尝试了各种方法.我最后虽然在第四个参数中添加一些内容时它会起作用.
plusTree :: Tree -> Tree -> Tree
plusTree Null ys = Null
plusTree xs Null = Null
plusTree (Node x …Run Code Online (Sandbox Code Playgroud) 标题说明了一切。我正在尝试修改文件夹的文件修改日期。touch t- YYYYMMDDhhmm来自终端的命令可以做到这一点,但它也会更改文件创建日期,我不想更改。
这个问题有解决办法吗?
为什么这段代码:
sapply(c(1, 3, 4, 0), print)
Run Code Online (Sandbox Code Playgroud)
返回:
[1] 1
[1] 3
[1] 4
[1] 0
[1] 1 3 4 0
Run Code Online (Sandbox Code Playgroud)
为什么它也会返回输入?
我有以下内容enum:
typedef NS_ENUM(NSUInteger, GraphType) {
GraphTypeRawData,
GraphTypeFilteredData
};
Run Code Online (Sandbox Code Playgroud)
编译器在没有警告的情况下接受我将其声明为属性原语或指针:
@property (nonatomic, assign) GraphType graphType;
Run Code Online (Sandbox Code Playgroud)
VS
@property (nonatomic, assign) GraphType *graphType;
Run Code Online (Sandbox Code Playgroud)
哪个是正确的?(为什么?)
#include <stddef.h>
template<size_t N = sizeof(void*)> struct a;
template<> struct a<4> {
enum { b };
};
template<> struct a<8> {
template<int> struct b {};
};
enum { c, d };
int main() {
a<>::b<c>d;
d;
}
Run Code Online (Sandbox Code Playgroud)
我有一段代码可以通过gcc成功编译x64架构.但是-m32错误地失败了:
$ g++ -m32 test.cpp
test.cpp: In function ‘int main()’:
test.cpp:16:12: warning: comparison between ‘enum a<4u>::<anonymous>’ and ‘enum<anonymous>’ [-Wenum-compare]
a<>::b<c>d;
^
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
这段代码出了什么问题?
我正在尝试使用C#在Visual Studio中构建计算器,我想使用这些重要的数学常量.我宣布他们:
public constant double PI;
public constant double e;
Run Code Online (Sandbox Code Playgroud)
但是它显示了这些声明的错误:
const字段需要提供值.
伙计,我该怎么办?
from random import uniform
def e(x):
n=len(x)
return(sum(x)/n)
def dmean(e,x,new):
n=len(x)
return((e*n+new)/(n+1))
l=[1,2,3,4,5,6,78]
e(l)
for i in range(0,5):
l.append(uniform(0,10))
e=e(l)
d=dmean(e,l,uniform(0,10))
Run Code Online (Sandbox Code Playgroud) haskell ×5
python ×4
32bit-64bit ×1
c# ×1
c++ ×1
calculator ×1
enums ×1
g++ ×1
ios ×1
list ×1
math ×1
metadata ×1
objective-c ×1
pointers ×1
r ×1
templates ×1
text ×1
web-crawler ×1