小编Bak*_*riu的帖子

strcat() 实现有效,但最终会导致核心转储

我的实现strcat(char*, const char*)似乎有效,但随后会导致核心转储。

strcat() 执行:

char* strcat(char* dest, const char* src)
{
    char* tmp = dest;
    while(*tmp) ++tmp ;
    while( (*tmp++ = *src++ ) != '\0') ;
    return (dest);
}
Run Code Online (Sandbox Code Playgroud)

代码在int main()那里我打电话的strcat():

char arr3[] = "Mr. ";
char arr4[] = "Smith";
printf("Hello %s!", strcat(arr3, arr4));
Run Code Online (Sandbox Code Playgroud)

它实际上连接了两个字符串并将其打印出来,但仍然导致核心转储。

输出:你好,史密斯先生!中止(核心转储)

我做错了什么?

c coredump strcat

3
推荐指数
1
解决办法
4578
查看次数

失败时如何保存信息?

我正在编写一些代码,使用StateTmonad转换器来跟踪一些有状态信息(日志记录等).

我传给的monad StateT非常简单:

data CheckerError a = Bad {errorMessage :: Log} | Good a
    deriving (Eq, Show)


instance Monad CheckerError where
    return x = Good x

    fail msg = Bad msg

    (Bad msg) >>= f = Bad msg
    (Good x) >>= f = f x

type CheckerMonad a = StateT CheckerState CheckerError a
Run Code Online (Sandbox Code Playgroud)

这只是一个LeftRight变型.

困扰我的是它的定义fail.在我的计算中,我在这个monad中产生了很多信息,我想在失败的时候保留这些信息.目前我唯一能做的就是将所有内容转换为a StringBad使用String传递的as参数创建一个实例fail.

我想做的是:

fail msg = do
    info <- getInfoOutOfTheComputation …
Run Code Online (Sandbox Code Playgroud)

monads haskell state-monad monad-transformers

3
推荐指数
1
解决办法
135
查看次数

将谓词添加到地图功能

完全是Haskell的新手,通过Learn Haskell学习更大的好处.

我正在看地图功能

map :: (a -> b) -> [a] -> [b]  
map _ [] = []  
map f (x:xs) = f x : map f xs  
Run Code Online (Sandbox Code Playgroud)

是否可以为此添加谓词?例如,仅映射到列表中的每个其他元素?

haskell filtering map

3
推荐指数
1
解决办法
887
查看次数

变量替换(你怎么称呼/命名这个方法)?

在bash中说我有:

c=3

b3=4
Run Code Online (Sandbox Code Playgroud)

代替:

echo $b3 
Run Code Online (Sandbox Code Playgroud)

什么样的命令输出"4"?我试过下面但不能.

  1. echo $b($(c))

  2. echo $b{!c}

  3. echo $b${c}

  4. echo ${d[$c]}

variables bash

3
推荐指数
1
解决办法
45
查看次数

Python 3.4中的BeautifulSoup无效语法(在2to3.py之后)

我想在Python 3.4中安装Beautiful Soup 4.我从命令行安装它(得到了无效的语法错误,因为我没有转换它),运行2to3.py转换脚本到bs4现在我得到一个新的无效语法错误.

>>> from bs4 import BeautifulSoup
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
    from bs4 import BeautifulSoup
  File "C:\Python34\bs4\__init__.py", line 30, in <module>
    from .builder import builder_registry, ParserRejectedMarkup
  File "C:\Python34\bs4\builder\__init__.py", line 4, in <module>
    from bs4.element import (
  File "C:\Python34\bs4\element.py", line 1213
    print 'Running CSS selector "%s"' % selector
                                    ^
SyntaxError: Missing parentheses in call to 'print'
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

python beautifulsoup python-3.x python-3.4

3
推荐指数
1
解决办法
8590
查看次数

Haskell将多行代码视为单个函数调用

pure :: String -> ()
pure x = unsafePerformIO $ do
  print x
  return ()

pureCall :: String -> IO ()
pureCall x = do
  pure x
  putStrLn "inside child function"
Run Code Online (Sandbox Code Playgroud)

这会抛出编译错误,

  The function ‘pure’ is applied to three arguments,
    but its type ‘String -> ()’ has only one
Run Code Online (Sandbox Code Playgroud)

我在其他语言中使用分号作为代码行分隔符.但不确定,我怎么能在haskell中做到这一点,并将pureCall功能块作为两个单独的语句运行!!

haskell

3
推荐指数
1
解决办法
529
查看次数

输入'where'时解析错误

我正在尝试“解决”给定d: 的佩尔方程x^2 - d * y^2 = 1,或者至少我想得到x > 0可以解决方程的最小值。到现在为止还挺好。这是我的 Haskell 代码

minX :: Integer -> Integer
minX n  | isSquare n = 1
        | otherwise  = minXRec [0,1,intSqrt n] [1,0,1] 0 1 (intSqrt n) n

minXRec :: [Integer] -> [Integer] -> Integer -> Integer -> Integer -> Integer -> Integer
minXRec (p0:p1:p2:x) (q0:q1:q2:y) m d a n
    | p2*p2 - n*q2*q2 == 1 = p2
    | minXRec [p1, p2, newA*p2+p1] [q1, q2, newA*q2+q1] …
Run Code Online (Sandbox Code Playgroud)

haskell where-clause

3
推荐指数
1
解决办法
2572
查看次数

如何简化if语句中的多个或条件?

所以我想写这个:

if x % 2 == 0 or x % 3 == 0 or x % 5 == 0 or x % 7 == 0:
Run Code Online (Sandbox Code Playgroud)

但是这样:

if x % (2 or 3 or 5 or 7) == 0:
Run Code Online (Sandbox Code Playgroud)

我该如何以正确的方式写出来?

python if-statement python-3.x

3
推荐指数
1
解决办法
1631
查看次数

生成列表元素对的每个排列,而不重复或反对

我知道这与以前的问题类似,但我的请求中有足够的差异值得提出新问题.我有一个字符串元素列表.

>>> mainlist
['one', 'two', 'three', 'four', 'five']
Run Code Online (Sandbox Code Playgroud)

我想创建一个循环程序,它接受第一个元素,然后将其与其余元素配对,如下所示:

['one two', 'one three', 'one four', 'one five']
Run Code Online (Sandbox Code Playgroud)

请注意,它没有创建该对 'one one'

下一个周期应该是:

['two three', 'two, four', 'two five']
Run Code Online (Sandbox Code Playgroud)

请注意,它再次没有创建'two two',甚至'two one'因为我的目的,它等于'one two'.

等等...

我最近的是:

for primary in mainlist:
    for secondary in mainlist:
        if primary == secondary: print("skipping...")
        else: print(primary + " " + secondary)


>> skipping...
one two
one three
one four
one five
two one
skipping...
two three
two four
two five
three one …
Run Code Online (Sandbox Code Playgroud)

python

3
推荐指数
2
解决办法
1807
查看次数

获取属于其OneToOne模型的属性排序的模型

假设有一个模型命名User,另一个命名与之PetOneToOne关系User,Pet模型有一个属性age,如何获得User拥有十大oldest狗的十个?

class User(models.Model):
     name = models.CharField(max_length=50, null=False, blank=False)

class Pet(models.Model):
     name = models.CharField(max_length=50, null=False, blank=False)
     owner = models.OneToOneField(User, on_delete=models.CASCADE)
     age = models.IntegerField(null=False)
Run Code Online (Sandbox Code Playgroud)

User,有一个属性friends,有一个ManyToMany与关系User,如何让十friendsUser Tom拥有十大oldest狗?

class User(models.Model):
     name = models.CharField(max_length=50, null=False, blank=False)
     friends = models.ManyToManyField(self, ...)

class Pet(models.Model):
     name = models.CharField(max_length=50, null=False, blank=False)
     owner = models.OneToOneField(User, on_delete=models.CASCADE)
     age = models.IntegerField(null=False)
Run Code Online (Sandbox Code Playgroud)

python django python-3.x

3
推荐指数
1
解决办法
74
查看次数