在某些情况下,我不明白在函数中如何使用foldr和foldl使用。
这是几个例子,然后我解释为什么我不理解它们:
-- Two implementation of filter and map
map' f = foldr (\x acc -> (f x):acc) []
map'' f xs = foldl (\acc x -> acc ++ [(f x)]) [] xs
filter' f xs = foldr(\x acc -> if(f x) then x:acc else acc) [] xs
filter'' f = foldl(\acc x -> if(f x) then acc++[x] else acc) []
Run Code Online (Sandbox Code Playgroud)
为什么map''使用xsbut non map'?map'列表理解公式也不需要列表吗?
filter'vs 的情况相同filter''。 …
这个程序叫做program.c. 当我运行时./program echo test,我希望程序打印test,即使命令是在子shell中运行的。为什么输出是空行?它与文件路径有关吗?当我尝试时,./program /bin/echo test我仍然得到一个空行输出。
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int function(char **argv) {
execl("/bin/bash", "sh", "-c", *argv, argv, (char*)NULL);
}
int main(int argc, char **argv) {
int return2;
function(argv + 1);
}
Run Code Online (Sandbox Code Playgroud)