以下是memoization函数的完整可运行代码:
memo f = g
where
fz = f Z
fs = memo (f . S)
g Z = fz
g (S n) = fs n
-- It is a BAD BUG to inline 'fs' inside g
-- and that happened in 6.4.1, resulting in exponential behaviour
-- memo f = g (f Z) (memo (f . S))
-- = g (f Z) (g (f (S Z)) (memo (f . S . S)))
-- = g (f Z) (g …Run Code Online (Sandbox Code Playgroud) 例如,键入:t apGHCi会得到结果
ap :: Monad m => m (a -> b) -> m a -> m b
Run Code Online (Sandbox Code Playgroud)
如果我已经知道我要使用的Monad实例,我((->) r)该如何查询ap该特定实例的类型?
我想学习如何使用带有嵌入式代码执行的Perl regexp为嵌套元组创建抽象语法树.我可以使用Perl 6语法轻松编程,我知道使用解析模块可以简化Perl 5中的任务,但我认为对于这样简单的任务,我应该能够通过学习如何从语法定义.我找不到解除引用$ ^ R的方法,所以我尝试在TUPLE规则定义的末尾撤消非自愿的嵌套,但是输出不正确,例如一些子串出现两次.
use v5.10;
use Data::Dumper;
while (<DATA>) {
chomp;
/(?&TUPLE)(?{$a = $^R})
(?(DEFINE)
(?<TUPLE>
T \s (?&ELEM) \s (?&ELEM)
(?{ [$^R->[0][0],[$^R->[0][1],$^R[1]]] })
)
(?<ELEM>
(?: (a) (?{ [$^R,$^N] }) | \( (?&TUPLE) \) )
)
)/x;
say Dumper $a;
}
__DATA__
T a a
T (T a a) a
T a (T a a)
T (T a a) (T a a)
T (T (T a a) a) (T a (T a a))
Run Code Online (Sandbox Code Playgroud)
预期的输出数据结构是嵌套列表:
['a','a'];
['a',['a','a']]; …Run Code Online (Sandbox Code Playgroud) Edward Kmett在他的博客中写道,使用 newtype Co(来自 kan-extensions 包),可以从任何 Comonad 派生出 Monad。我想学习如何机械地为 any 执行此操作Cofree f a,对于某些 Cofree 类型,我不知道以万无一失的方式获取 monad 实例的不同方法。例如数据类型
data Tree a = Leaf a | Branch a (Tree a) (Tree a)\nRun Code Online (Sandbox Code Playgroud)\n同构于
\ntype Tree\' = Cofree (Compose Maybe V2) -- V2 from `linear` package\nRun Code Online (Sandbox Code Playgroud)\n我发现这种类型很有趣,因为它不能建模为自由单子,而且也不可表示。我的理解是它Co Tree也与 Tree 同构,并且有一个 monad 实例。Phil Freeman 在他的共生 UI 库Co中广泛使用,因此它一定很有用。在“声明式 UI 是未来 \xe2\x80\x94 未来是 Comonadic!” 中,他提到:
\n事实上,在 f 的某些条件下,Co (Cofree f) …
我试图了解正则表达式变量是如何工作的,因此我可以在嵌入式代码表达式中保存有效负载中的子匹配位置。根据perlvar,数组的正索引对应于 $1、$2、$3 等,但情况似乎并非如此?
#!/usr/bin/perl -w
use v5.28;
use Data::Dumper;
"XY" =~ / ( (.*) (.) (?{
say Dumper { match_end => \@+ };
say Dumper { capture => \@{^CAPTURE} }
}) ) (.)/x;
Run Code Online (Sandbox Code Playgroud)
输出:
$VAR1 = {
'match_end' => [
2,
undef,
1,
2,
undef
]
};
$VAR1 = {
'capture' => [
undef,
'X',
'Y'
]
};
$VAR1 = {
'match_end' => [
1,
2,
0,
1,
undef
]
};
$VAR1 = {
'capture' => [ …Run Code Online (Sandbox Code Playgroud) 我想检查一个字符串是否代表文件的完整路径,如下所示:
p = 'C:\my\custom\path.txt'
Run Code Online (Sandbox Code Playgroud)
该文件不存在,因此像 和isdir之类的命令exist向我返回 false,但字符串的格式仍然代表我的操作系统的有效路径,而以下一个则不存在,因为它的文件名字符无效:
p = 'C:\my\custom\:path.txt'
Run Code Online (Sandbox Code Playgroud)
所以我想知道如何检查字符串是否代表有效的文件路径,而不需要该文件实际存在。
我正在尝试为一个简单的DSL编写一个解析器,它在表单中有十几个语句<statementName> <param1> <param2> ... ;,其中参数的数量各不相同.由于语句的结构非常相似(所有匹配的语句名称字符串后跟一系列由名称给出的标记),并且所得结果的结构非常相似(所有存储语句名称和参数的哈希值),我想知道如何指定想要的结果结构,而不必为每个语句操作重复自己.
动作类的伪代码,可以帮助我指定这样的结果结构:
class FooActions {
method *_stmt ($/) {
@result[0] = make string of statement name $/[0];
@result[1] = make hash of $/[1..] with the keys being the name of the rule
at index (i.e. '"var"' for `<var=identifier>` and `"type"` for `<type>`, etc.) and
values being the `.made` results for the rules at index (see below);
return @result;
}
method identifier ($/) { return ~$/ }
method number ($/) { return +$/ …Run Code Online (Sandbox Code Playgroud) 我编译了文件stacktrace.d:
void main(){assert(false);}关闭了ASLR,运行时我得到:
core.exception.AssertError@stacktrace.d(2): Assertion failure
----------------
??:? _d_assertp [0x55586ed8]
??:? _Dmain [0x55586e20]
Run Code Online (Sandbox Code Playgroud)
objdump -t stacktrace|grep _Dmain 给
0000000000032e0c w F .text 0000000000000019 _Dmain
如果我跑步gdb -q -nx -ex start -ex 'disas /rs _Dmain' -ex q stacktrace:
...
Dump of assembler code for function _Dmain:
0x0000555555586e0c <+0>: 55 push %rbp
0x0000555555586e0d <+1>: 48 8b ec mov %rsp,%rbp
=> 0x0000555555586e10 <+4>: be 02 00 00 00 mov $0x2,%esi
0x0000555555586e15 <+9>: 48 8d 3d 44 c0 02 00 lea 0x2c044(%rip),%rdi …Run Code Online (Sandbox Code Playgroud) 我正在尝试熟悉触发器运算符,因此即使有教科书风格的状态机在其中很好地工作(并且冗长且变量丰富),我也可以在进行状态循环时将其作为附加的抽象思想。这样的情况。我想跟踪缩进,而且似乎仍然需要在每个if块的开始手动调整缩进,在这种情况下,我将其称为缩进触发器,对吗?这是我想出的:
程序:
use v5.20;
use strict;
use warnings;
my $shiftwidth = 3;
# block_rx: start of indented block marker, without leading spaces
# Keeps state of indentation, which is increased on encountering block marker
# and decreased on matching outdent.
# Function should always get indentation level from context it was called in.
# Returns: true if in indented block, ^ff^, else false
sub indenting_flipflop {
my $block_rx = $_[0];
$_ = $_[1];
my $level = $_[2];
my $indent …Run Code Online (Sandbox Code Playgroud) 这编译:
foo :: (Bounded a,Enum a) => a -> Int
foo x = length ([minBound .. x] ++ drop 1 [x .. maxBound])
Run Code Online (Sandbox Code Playgroud)
这不编译:
foo :: (Bounded a,Enum a) => a -> Int
foo x = length ([minBound .. maxBound] :: [a])
Run Code Online (Sandbox Code Playgroud)
我认为第二个例子没有编译,因为a类型签名中的类型与子表达式的类型签名中的类型不同.如何使子表达式的类型引用上面给出的多态类型?
我想使用gdb的记录,但是因为glibc的ld.so使用xsave指令,所以出现错误“进程记录不支持地址0x7ffff7fe883c的指令0xfae64”。
多亏了stackoverflow的回答,我才能用二进制补丁修复类似的错误。运行半小时后,使用调试符号编译glibc失败,因此,如果有更快的解决方案,我将感到高兴。我从这里获得了一个编译版本,但似乎没有提供任何早期版本(即,我现在使用的是glibc 2.28.r502.g065957a3704-1和gdb 8.2.1)。如何使gdb录制工作?
Haskell的基本库包含几个函数,它们各自的数据类型的小写版本,例如bool,maybe和either.在Data.Bool.Extra的源代码中,该bool函数清楚地表示为数据类型的catamorphism:
bool = curry cata
Run Code Online (Sandbox Code Playgroud)
现在,使用Recursion Schemes by Example中定义的catamorphism ,看起来上面提到的基本库函数都是它们数据类型的所有catamorphisms,例如对于Maybe:
-- base library definition
maybe n _ Nothing = n
maybe _ f (Just x) = f x
-- definition with cata
newtype Fix f = Fix {unFix :: f (Fix f)}
cata :: Functor f => (f a -> a) -> Fix f -> a
cata alg = alg . fmap (cata alg) . unFix
maybe n f m …Run Code Online (Sandbox Code Playgroud)