我不了解Haskell的monad背后的确切代数和理论.但是,当我考虑函数式编程时,我会得到这样的印象:状态将通过获取初始状态并生成它的副本来表示下一个状态.这就像将一个列表附加到另一个列表时; 两个列表都没有被修改,但是创建并返回了第三个列表.
因此,将monadic操作视为隐式地将初始状态对象作为参数并隐式返回最终状态对象是否有效?这些状态对象将被隐藏,以便程序员不必担心它们并控制它们的访问方式.因此,程序员不会像十分钟前那样尝试复制代表IO流的对象.
换句话说,如果我们有这个代码:
main = do
putStrLn "Enter your name:"
name <- getLine
putStrLn ( "Hello " ++ name )
Run Code Online (Sandbox Code Playgroud)
...可以将IO monad和"do"语法看作代表这种代码风格吗?
putStrLn :: IOState -> String -> IOState
getLine :: IOState -> (IOState, String)
main :: IOState -> IOState
-- main returns an IOState we can call "state3"
main state0 = putStrLn state2 ("Hello " ++ name)
where (state2, name) = getLine state1
state1 = putStrLn state0 "Enter your name:"
Run Code Online (Sandbox Code Playgroud) 我有以下Ruby代码:
# func1 generates a sequence of items derived from x
# func2 does something with the items generated by func1
def test(x, func1, func2)
func1.call(x) do | y |
func2.call(y)
end
end
func1 = lambda do | x |
for i in 1 .. 5
yield x * i
end
end
func2 = lambda do | y |
puts y
end
test(2, func1, func2) # Should print '2', '4', '6', '8', and '10'
Run Code Online (Sandbox Code Playgroud)
当然,这不起作用.
test.rb:11: no block given (LocalJumpError) …Run Code Online (Sandbox Code Playgroud) 我正在编写一个OpenGL程序,我计算自己的矩阵并将它们传递给着色器.我想将Boost的uBLAS库用于矩阵,但我不知道如何将uBLAS矩阵转换为OpenGL的着色器统一函数.
matrix<GLfloat, column_major> projection(4, 4);
// Fill matrix
...
GLuint projectionU = glGetUniformLocation(shaderProgram, "projection");
glUniformMatrix4fv(projectionU, 1, 0, (GLfloat *)... Um ...);
Run Code Online (Sandbox Code Playgroud)
尝试将矩阵转换为GLfloat指针会导致编译时出现无效的强制转换错误.
我正在写一个网页,我在其中显示一些文字的标题和日期.
博客文章标题http://filesmelt.com/dl/head00.png
我的HTML:
<div class="post">
<h2>Post 1</h2>
<span class="date">February 28, 2011</span>
<div class="post-content">
...
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我的css:
.post h2
{
float: left;
}
.date
{
float: right;
}
.post-content
{
clear: both;
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是垂直对齐标题和日期,使其底部匹配.现在他们没有:
带有对齐线的博客文章标题http://filesmelt.com/dl/head01.png
我尝试将div中的两个文本元素包装起来,将div设置position为relative,并在两个文本元素上使用绝对定位(并取出浮动声明).这不起作用,因为由于包装器div崩溃而没有保留上边距,即使我给它一个clearfix类.
我正在编写一个Boost Spirit语法来将文本解析为这些结构的向量:
struct Pair
{
double a;
double b;
};
BOOST_FUSION_ADAPT_STRUCT(
Pair,
(double, a)
(double, a)
)
Run Code Online (Sandbox Code Playgroud)
这个语法有这样的规则:
qi::rule<Iterator, Pair()> pairSequence;
Run Code Online (Sandbox Code Playgroud)
但是,实际的语法pairSequence是这样的:
double_ % separator
Run Code Online (Sandbox Code Playgroud)
我想这个语法产生一个Pair具有a等于双层并b等于某个常数.我想做这样的事情:
pairSequence = double_[_val = Pair(_1, DEFAULT_B)] % separator;
Run Code Online (Sandbox Code Playgroud)
当然,上面没有编译.我尝试添加一个构造函数Pair,但我仍然得到编译错误(没有匹配函数调用'Pair :: Pair(const boost :: phoenix :: actor>&,double)').
这很长,有很多代码,所以我希望Stack Overflow能够应对它.:P
我正在尝试用Boost Spirit编写一个SVG解析器.我有一个语法,用"Contours"填充一个矢量,它是"BezierPoints"的矢量,可以用bezier控件表示常规点或点.
到目前为止我有这个(还没有处理相对绘图命令):
#ifndef SVG_PARSER_HPP
#define SVG_PARSER_HPP
#include <vector>
#include "boost/spirit/include/qi.hpp"
#include "boost/spirit/include/phoenix.hpp"
#include "boost/fusion/include/adapt_struct.hpp"
#include "boost/fusion/include/std_pair.hpp"
namespace qi = boost::spirit::qi;
namespace phoenix = boost::phoenix;
namespace ascii = boost::spirit::ascii;
struct Point
{
Point(const double nx = 0.0, const double ny = 0.0) : x(nx), y(ny)
{}
double x;
double y;
};
BOOST_FUSION_ADAPT_STRUCT(
Point,
(double, x)
(double, y)
)
struct BezierPoint
{
BezierPoint(const double x = 0.0, const double y = 0.0) :
point(x, y), control1(0.0, 0.0), control2(0.0, 0.0) …Run Code Online (Sandbox Code Playgroud) 我在HTML中有以下列表:
<ul>
<li><span class="button">A</span></li>
<li><span class="button">B</span></li>
<li><span class="button">C</span></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
附带的CSS.了解我想为不同的列表使用不同大小的按钮.
.button
{
background: blue;
padding: 0.5em;
}
Run Code Online (Sandbox Code Playgroud)
li不会扩展以适应它们内部的跨度.这具有抛弃整个列表边距的效果,这在我将列表转换为水平菜单时尤为重要.
另外,我在li里面设计的样式而不是自己设计样式的原因是因为其中一些跨度实际上是链接.