因为foldr我们有融合法:如果f是严格的f a = b,和
f (g x y) = h x (f y)对所有人来说x, y,那么f . foldr g a = foldr h b.
如何发现/得出类似的法律foldr1?(它显然甚至不能采取相同的形式 - 考虑双方采取行动的情况[x].)
我的问题涉及将树转换为功能图库表示的明显尴尬,该表示需要显式引用节点名称以插入更多节点/边.
具体来说,我递归构建玫瑰树(目前Data.Tree.Tree a从containers),并希望将其转换为一个Gr a ()调用各种功能反对.为此,可以首先遍历树(使用供应monad或其中一个FGL的状态monad类型,如NodeMapM),并使用标识符标记每个节点:
{-# LANGUAGE TupleSections #-}
import Control.Monad.Supply
import Data.Graph.Inductive
import Data.Tree
tagTree :: Tree a -> Supply Int (Tree (a,Int))
tagTree (Node n xs) = do
xs' <- sequence (map tagTree xs)
s <- supply
return $ Node (n,s) xs'
Run Code Online (Sandbox Code Playgroud)
然后evalSupply (tagTree tree) [1..],然后使用类似的东西
taggedTreeToGraph :: Tree (a,Int) -> Gr a ()
taggedTreeToGraph (Node (n,i) xs) =
([],i,n,map (((),) . snd . rootLabel) xs)
& foldr …Run Code Online (Sandbox Code Playgroud) (问题的简化形式。)我正在编写一个涉及一些 Python 组件的 API。这些可能是函数,但为了具体起见,我们假设它们是对象。我希望能够从命令行解析各种组件的选项。
from argparse import ArgumentParser
class Foo(object):
def __init__(self, foo_options):
"""do stuff with options"""
"""..."""
class Bar(object):
def __init__(sef, bar_options):
"""..."""
def foo_parser():
"""(could also be a Foo method)"""
p = ArgumentParser()
p.add_argument('--option1')
#...
return p
def bar_parser(): "..."
Run Code Online (Sandbox Code Playgroud)
但现在我希望能够构建更大的组件:
def larger_component(options):
f1 = Foo(options.foo1)
f2 = Foo(options.foo2)
b = Bar(options.bar)
# ... do stuff with these pieces
Run Code Online (Sandbox Code Playgroud)
美好的。但是如何编写合适的解析器呢?我们可能希望像这样:
def larger_parser(): # probably need to take some prefix/ns arguments
# general options to be overridden by p1, …Run Code Online (Sandbox Code Playgroud) 我已经安装了'guard'和'guard-rspec',我还配置了Guardfile(用于观察'app/views'中的更改)但是当我运行'bundle exec guard'时,我总是得到这个:
vagrant@vagrant-debian-squeeze:/vagrant/sample_app$ bundle exec guard
Guard could not detect any of the supported notification libraries.
Guard is now watching at '/vagrant/sample_app'
Guard::RSpec is running, with RSpec 2!
Running all specs
........
Finished in 0.97359 seconds
8 examples, 0 failures
>
Run Code Online (Sandbox Code Playgroud)
它已经完成了与后卫控制台提示符下,如果我从编辑"应用程序/视图/"(例如应用程序/视图/ static_pages/home.html.erb)的一些文件并保存,后卫没有表现出任何规格的输出,刚刚还在等待对于一些控制台命令.
我想它应该在保存监视文件后显示一些rspec输出.
的Gemfile:
source 'https://rubygems.org'
gem 'rails', '3.2.3'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.9.0'
gem 'guard-rspec', '0.5.5'
gem 'guard'
end
# Gems …Run Code Online (Sandbox Code Playgroud) 该Ocsigen/Eliom教程开始与提供了一个应用实例"你好,世界!" 作为HTML:
open Eliom_content.Html5.D
let main_service =
Eliom_registration.Html5.register_service
~path:["graff"]
~get_params:Eliom_parameter.unit
(fun () () ->
Lwt.return
(html
(head (title (pcdata "Page title")) [])
(body [h1 [pcdata "Graffiti"]])))
Run Code Online (Sandbox Code Playgroud)
如何将其作为JSON服务呢?具体来说,如何注册JSON服务,以及应该使用哪些库/组合器来生成/序列化JSON(js_of_ocaml?)?
所以我一直在努力解决过去几个小时打印数独游戏板的问题,我差不多完成了但是我已经陷入了最后一步.所以我所拥有的是一个数据板,表示为"列表列表"(即板= [[1,3,5,7,0,2,0,0,0],[3,4,5,... .],...]
我已经能够使用以下功能打印出具有格式的行:
line i s_board = intercalate " | " . map unwords . chunksOf 3 $ map show a
where
a = s_board!!i
Run Code Online (Sandbox Code Playgroud)
所以通过像"0行板"那样打电话,我会得到"1 3 5 | 7 0 2 | 0 0 0"这部分是我需要的.接下来我尝试使用"do block"输出我需要的板,看起来像这样:
print = do line 0 board
line 1 board
...
Run Code Online (Sandbox Code Playgroud)
这甚至不会编译,当我做这样的事情时:
print = do
line 0 board
line 1 board
Run Code Online (Sandbox Code Playgroud)
适当的列表被多次打印,这是相当混乱的.我想继续努力,包括额外的格式化,比如在每三行之后打印一个字符串,例如"----------",以完成电路板,但我甚至无法让其他东西工作对了.我很感激对这些问题的任何帮助.