小编Jas*_* Tu的帖子

在Haskell中防守与if-then-else对比案例

我有三个函数可以找到列表的第n个元素:

nthElement :: [a] -> Int -> Maybe a 
nthElement [] a = Nothing
nthElement (x:xs) a | a <= 0 = Nothing
                    | a == 1 = Just x
                    | a > 1 = nthElement xs (a-1)

nthElementIf :: [a] -> Int -> Maybe a
nthElementIf [] a = Nothing
nthElementIf (x:xs) a = if a <= 1
                        then if a <= 0 
                             then Nothing
                             else Just x -- a == 1
                        else nthElementIf xs (a-1)                           

nthElementCases :: [a] …
Run Code Online (Sandbox Code Playgroud)

haskell if-statement case

98
推荐指数
3
解决办法
6万
查看次数

有没有值的Python dict?

而不是这个:

a = {"foo": None, "bar": None}
Run Code Online (Sandbox Code Playgroud)

有没有办法写这个?

b = {"foo", "bar"}
Run Code Online (Sandbox Code Playgroud)

并且仍然允许b持续时间访问(即不是Python集,无法键入)?

python dictionary

19
推荐指数
2
解决办法
2万
查看次数

为什么c_str()为两个不同的字符串返回相同的值?

给定一个简单的文件加载功能,

std::string load_file(const std::string &filename) {
    std::ifstream     file(filename);
    std::string       line;
    std::stringstream stream;
    while (std::getline(file, line)) {
        stream << line << "\n";
    }
    return stream.str();
}
Run Code Online (Sandbox Code Playgroud)

为什么以下打印another_file两次内容?

const char *some_file = load_file("some_file").c_str();
const char *another_file = load_file("another_file").c_str();
printf("%s", some_file);
printf("%s", another_file);
Run Code Online (Sandbox Code Playgroud)

c++ string

4
推荐指数
1
解决办法
495
查看次数

在Go中获取函数名称时,为什么会有“ -fm”后缀?

对于以下代码段(可通过Go Playground运行),

package main

import (
  "fmt"
  "net/http"
  "reflect"
  "runtime"
)

type User struct{}

var u = &User{}

func (_ User) DummyHandler(w http.ResponseWriter, r *http.Request) {}

func funcName(i interface{}) {
  p := reflect.ValueOf(i).Pointer()
  n := runtime.FuncForPC(p).Name()
  fmt.Println(n)
}

func main() {
  funcName(u.DummyHandler)
}
Run Code Online (Sandbox Code Playgroud)

输出为main.(User).DummyHandler-fm

为什么-fm函数名称的末尾有一个?

reflection go

4
推荐指数
1
解决办法
292
查看次数

找不到glXCreateContextAttribsARB

我正在学习现代3D图形编程,我正在尝试运行"你好,世界!" 三角计划.但是,我收到此错误:

jason@ubuntu:~/Desktop/Tutorial 0.3.8/Tut 01 Hello Triangle$ ./Tut\ 01\ Main
freeglut (./Tut 01 Main): glXCreateContextAttribsARB not found
Run Code Online (Sandbox Code Playgroud)

运行'glxinfo'给了我这个:

jason@ubuntu:~/Desktop/Tutorial 0.3.8/Tut 01 Hello Triangle$ glxinfo | grep OpenGL
OpenGL vendor string: Tungsten Graphics, Inc
OpenGL renderer string: Mesa DRI Intel(R) Ironlake Mobile 
OpenGL version string: 2.1 Mesa 7.11
OpenGL shading language version string: 1.20
OpenGL extensions:
Run Code Online (Sandbox Code Playgroud)

'lspci'命令给了我这个:

jason@ubuntu:~/Desktop/Tutorial 0.3.8/Tut 01 Hello Triangle$ lspci | grep "VGA"
00:02.0 VGA compatible controller: Intel Corporation Core Processor Integrated Graphics Controller (rev 02) …
Run Code Online (Sandbox Code Playgroud)

opengl freeglut mesa

2
推荐指数
1
解决办法
2880
查看次数

是否有get()实例变量?

对于Python dicts,可以做到

a = {'b': 5}
c = a.get('c', 0)
Run Code Online (Sandbox Code Playgroud)

以便在密钥不存在时具有默认值.Python实例变量是否有类似的"默认值"功能?

python

0
推荐指数
1
解决办法
55
查看次数

标签 统计

python ×2

c++ ×1

case ×1

dictionary ×1

freeglut ×1

go ×1

haskell ×1

if-statement ×1

mesa ×1

opengl ×1

reflection ×1

string ×1