小编kev*_*kev的帖子

理解F#尾递归

最近,我正在学习F#.我尝试以不同的方式解决问题.像这样:

(*

[0;1;2;3;4;5;6;7;8] -> [(0,1,2);(3,4,5);(6,7,8)]

*)

//head-recursive
let rec toTriplet_v1 list=
    match list with
    | a::b::c::t -> (a,b,c)::(toTriplet_v1 t) 
    | _ -> []

//tail-recursive
let toTriplet_v2 list=
    let rec loop lst acc=
        match lst with
        | a::b::c::t -> loop t ((a,b,c)::acc)
        | _ -> acc
    loop list []

//tail-recursive(???)
let toTriplet_v3 list=
    let rec loop lst accfun=
        match lst with
        | a::b::c::t -> loop t (fun ls -> accfun ((a,b,c)::ls))
        | _ -> accfun []
    loop list (fun x -> …
Run Code Online (Sandbox Code Playgroud)

f# tail-recursion

8
推荐指数
1
解决办法
488
查看次数

为什么ls在管道输出时会提供不同的输出

直接打印到终端:

$ ls
a.out  avg.c  avg.h
Run Code Online (Sandbox Code Playgroud)

管道到 cat

$ ls | cat
a.out
avg.c
avg.h
Run Code Online (Sandbox Code Playgroud)

为什么ls根据目的地提供不同的输出?

bash

8
推荐指数
2
解决办法
519
查看次数

如何控制包含东亚字符的Unicode字符串的填充

我有三个UTF-8蜇伤:

hello, world
hello, ??
hello, ?rld
Run Code Online (Sandbox Code Playgroud)

我只想要前10个ascii-char-width,以便括号在一列中:

[hello, wor]
[hello, ? ]
[hello, ?r]
Run Code Online (Sandbox Code Playgroud)

在控制台中:

width('??')==width('worl')
width('? ')==width('wor')  #a white space behind '?'
Run Code Online (Sandbox Code Playgroud)

一个中文字符是三个字节,但在控制台中显示时只有2个ascii字符宽度:

>>> bytes("hello, ??", encoding='utf-8')
b'hello, \xe4\xb8\x96\xe7\x95\x8c'
Run Code Online (Sandbox Code Playgroud)

format()当UTF-8字符混入时,python 没有帮助

>>> for s in ['[{0:<{1}.{1}}]'.format(s, 10) for s in ['hello, world', 'hello, ??', 'hello, ?rld']]:
...    print(s)
...
[hello, wor]
[hello, ?? ]
[hello, ?rl]
Run Code Online (Sandbox Code Playgroud)

它不漂亮:

 -----------Songs-----------
|    1: ??                  |
|    2: ???                 |
|    3: ??????              |
|    4: ?????               |
|    5: ???(CUCURRUCUCU …
Run Code Online (Sandbox Code Playgroud)

python unicode string-formatting

7
推荐指数
3
解决办法
2986
查看次数

如何在函数内定义仿函数

有时,我需要一些functor-helper来操作列表.我尽量将范围保持在本地.

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    struct Square
    {
        int operator()(int x)
        {
            return x*x;
        }
    };

    int a[5] = {0, 1, 2, 3, 4};
    int b[5];

    transform(a, a+5, b, Square());

    for(int i=0; i<5; i++)
        cout<<a[i]<<" "<<b[i]<<endl;
}
Run Code Online (Sandbox Code Playgroud)
hello.cpp: In function ‘int main()’:
hello.cpp:18:34: error: no matching function for call to ‘transform(int [5], int*, int [5], main()::Square)’
Run Code Online (Sandbox Code Playgroud)

如果我搬出Squaremain(),没关系.

c++

7
推荐指数
2
解决办法
904
查看次数

如何在vim中搜索所有CJK字符?

我可以使用unicode代码点搜索CJKchar(例如?):

/\%u5c0f
/[\u5c0f]
Run Code Online (Sandbox Code Playgroud)

我不能通过使用来搜索所有CJK字符[\u4E00-\u9FFF],因为vim手册说:

?help /[]
NOTE: The other backslash codes mentioned above do not work inside []!

这是一种做这项工作的方法吗?

regex unicode vim cjk

7
推荐指数
1
解决办法
1279
查看次数

如何格式化vim quickfix条目?

这是生成Markdown大纲的vim脚本:

fun! TOC()
    call setloclist(0, [])
    let save_cursor = getpos(".")
    call cursor(1, 1)
    while search("^#", 'W') > 0
       let msg = printf('%s:%d:%s', expand('%'), line('.'), substitute(getline('.'), '#', '»', 'g'))
       laddexpr msg
    endwhile
    call setpos('.', save_cursor)
endfun

com! -bar TOC call TOC()
Run Code Online (Sandbox Code Playgroud)

示例降价文件:https://github.com/progit/progit/raw/master/zh/01-introduction/01-chapter1.markdown


运行:TOC命令后,这是快速列表:

01-chapter1.markdown|5| »» ?????? »»
01-chapter1.markdown|11| »»» ???????? »»»
01-chapter1.markdown|22| »»» ?????????? »»»
01-chapter1.markdown|33| »»» ????????? »»»
01-chapter1.markdown|42| »» Git ?? »»
01-chapter1.markdown|56| »» Git ?? »»
01-chapter1.markdown|60| »»» ????????????? »»»
01-chapter1.markdown|74| »»» ???????????? …
Run Code Online (Sandbox Code Playgroud)

vim

7
推荐指数
1
解决办法
1764
查看次数

每个`ftplugin/name.vim`都需要定义`b:undo_ftplugin`吗?

$VIMRUNTIME/ftplugin/(例如python.vimada.vim)中的某些脚本没有定义b:undo_ftplugin.该cpo选项的默认值是aABceFs.

当我set ft=python,然后set ft=css.$VIMRUNTIME/ftplugin/css.vim立即完成.而且omnifunc=pythoncomplete#Complete一直都是.

每个人都ftplugin/name.vim需要定义b:undo_ftplugin吗?


这是 /usr/share/vim/vim73/ftplugin.vim:

" Vim support file to switch on loading plugins for file types
"
" Maintainer:   Bram Moolenaar <Bram@vim.org>
" Last change:  2006 Apr 30

if exists("did_load_ftplugin")
  finish
endif
let did_load_ftplugin = 1

augroup filetypeplugin
  au FileType * call s:LoadFTPlugin()

  func! s:LoadFTPlugin()
    if exists("b:undo_ftplugin")
      exe b:undo_ftplugin
      unlet! b:undo_ftplugin b:did_ftplugin
    endif …
Run Code Online (Sandbox Code Playgroud)

vim

7
推荐指数
1
解决办法
713
查看次数

python中类块和功能块之间的差异

代码1

x = 0

class Foo:
    print(x)
    x = 1
    print(x)

print(x)
Run Code Online (Sandbox Code Playgroud)

结果1

0
1
0
Run Code Online (Sandbox Code Playgroud)

代码2

x = 0

def foo():
    print(x)
    x = 1
    print(x)

foo()
Run Code Online (Sandbox Code Playgroud)

结果2

UnboundLocalError: local variable 'x' referenced before assignment.
Run Code Online (Sandbox Code Playgroud)

为什么x可以引用两个名称空间中的对象class block
我不明白为什么Code 1不扔一个UnboundLocalError.
功能和课堂之间的不一致让我烦恼.


更新:

在阅读了几次Python文档之后,我仍然无法理解范围规则.

以下是块:模块,函数体和类定义....[跳跃]...

如果名称绑定在块中,则它是该块的局部变量,除非声明为非本地.如果名称在模块级别绑定,则它是全局变量.(模块代码块的变量是局部的和全局的.)如果在代码块中使用了变量但在那里没有定义,则它是一个自由变量.

如果名称绑定操作发生在代码块中的任何位置,则块中名称的所有使用都将被视为对当前块的引用.在绑定之前在块中使用名称时,这可能会导致错误.这条规则很微妙.Python缺少声明,并允许在代码块中的任何位置进行名称绑定操作.可以通过扫描块的整个文本以确定名称绑定操作来确定代码块的局部变量.

python

7
推荐指数
1
解决办法
2173
查看次数

istringstream运算符>>返回值如何工作?

此示例使用整数,运算符和另一个整数读取行.例如,

25*3

4/2

// sstream-line-input.cpp - Example of input string stream.
//          This accepts only lines with an int, a char, and an int.
// Fred Swartz 11 Aug 2003

#include <iostream>
#include <sstream>
#include <string>
using namespace std;
//================================================================ main
int main() {
    string s;                 // Where to store each line.
    int    a, b;              // Somewhere to put the ints.
    char   op;                // Where to save the char (an operator)
    istringstream instream;   // Declare an input string stream

    while …
Run Code Online (Sandbox Code Playgroud)

c++

6
推荐指数
1
解决办法
2832
查看次数

如何在bash中将二进制数据插入sqlite3数据库?

我想在bash脚本中将二进制数据(png,jpg,gif等)插入到sqlite3数据库中.
我使用独立的二进制文件sqlite3.我该如何编写SQL语句?
谢谢你的帮助.

sql sqlite binary bash

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

标签 统计

vim ×3

bash ×2

c++ ×2

python ×2

unicode ×2

binary ×1

cjk ×1

f# ×1

regex ×1

sql ×1

sqlite ×1

string-formatting ×1

tail-recursion ×1