从了解到一个哈克尔:http://learnyouahaskell.com/for-a-few-monads-more
函数的Monad实例是这样的:
instance Monad ((->) r) where
return x = \_ -> x
h >>= f = \w -> f (h w) w
Run Code Online (Sandbox Code Playgroud)
我无法理解以下内容的输出:
import Control.Monad.Instances
addStuff :: Int -> Int
addStuff = do
a <- (*2)
b <- (+10)
return (a+b)
Run Code Online (Sandbox Code Playgroud)
addStuff 3
返回19.书中说3作为参数传递给两者(*2)
and (+10)
.怎么样?
从h >>= f = \w -> f (h w) w
,它似乎(h w)
被绑定到a或b.那么,为什么6不被传递(+10)?
我的理解f
这里的是,当(*2)
是h
,f
是最后2行addStuff
.如果(+10)
是 …
您不必从头开始完成完整的代码.问题出在main里面的execl(..)语句中.代码是 -
#include <cstdio>
#include <iostream>
#include <cstring>
#include <unistd.h>
#include <sys/wait.h>
#include <vector>
#define li long int
using namespace std;
char TypedCommandInTerminal[1001];
vector <string> ValidCommands,TypedCommand;
void ShowTerminal()
{
cout<<"User:$ ";
gets(TypedCommandInTerminal);
}
void PushCommands()
{
ValidCommands.push_back("mkdir");
}
void GetCommandIntoVector()
{
TypedCommand.clear();
char *p = strtok(TypedCommandInTerminal," ");
while(p)
{
TypedCommand.push_back(p);
p = strtok(NULL," ");
}
}
bool MatchCommand(string Command)
{
li i;
for(i=0;i<ValidCommands.size();i++)
{
if(ValidCommands[i].compare(Command)==0)
{
return true;
}
}
return false;
}
int main()
{
int status;
string StoredCommand; …
Run Code Online (Sandbox Code Playgroud)