问题:处理CSV文件并在其上测试条件.当前代码只是打印而不是测试条件.
问题:类型推断失败.我不遵循它失败的原因.
这是代码,而不是导入样板.
--------------------------------------------------
has_empty_string :: [String] -> Bool
has_empty_string col =
any null col
----------------------------------------
get_hashrow :: [[String]] -> [String]
get_hashrow sheet =
-- looking at column 5
map (\row -> row !! 5) sheet
------------------------------
process_lines :: (String -> b) -> Handle -> IO ()
process_lines func inh = do
ineof <- hIsEOF inh
if ineof
then return ()
else do inpStr <- hGetLine inh
result <- func inpStr
putStrLn $ show result
process_lines func inh
------------------------------
process_lines_in_file …
Run Code Online (Sandbox Code Playgroud) 在Haskell(F#,Ocaml等)中,我可以这样做:
sign x | x > 0 = 1
| x == 0 = 0
| x < 0 = -1
Run Code Online (Sandbox Code Playgroud)
它计算给定整数的符号.
这可以简明扼要地表达某些逻辑流程; 我在Perl中遇到过其中一个流.
现在我正在做的是
sub frobnicator
{
my $frob = shift;
return "foo" if $frob eq "Foomaticator";
return "bar" if $frob eq "Barmaticator";
croak("Unable to frob legit value: $frob received");
}
Run Code Online (Sandbox Code Playgroud)
这让人感到无比和丑陋.
这段代码必须在Perl 5.8.8上运行,但我当然也对更现代的技术感兴趣.
我遇到问题让-e和-d文件测试运算符可靠地工作.
清单1和清单2位于NTFS Windows XP SP 3系统的同一目录中.但是,清单2坚持存在一个目录(它没有),清单1使它正确.清单2是主程序的一部分.
另外,有趣的是,我的记录器例程拒绝创建/写入文件,即使我使用or croak
语句进行打开和打印也是如此.我怀疑这两个是连接的.我将其列为清单3.
我的观点是Perl的全局变量在某处被取消设置/设置并且我设法炸掉它们(虽然我试图非常小心).
谢谢!
清单1:
use strict;
use warnings;
my $dir = "somedir2";
my $result= (-e $dir);
if( ! (-e $dir))
{
print "$dir doesn't exist\n";
}
else
{
print "$dir exists\n";
}
#print "$result\n";
if(! (-d $dir))
{
print "$dir isn't a dir!\n";
}
else
{
print "$dir is a dir\n";
}
Run Code Online (Sandbox Code Playgroud)
清单2:
#Does the output directory not exist?
open_logger("logfile.txt");
logger("initializing logfile.");
logger("Checking $outputdir for existence...");
if( ! ( -e $outputdir)) …
Run Code Online (Sandbox Code Playgroud) 我正在构建一个机制来获取任意CLOS对象并从中返回一个哈希(在我的调试经验中很有用).
但是,我不确定如何强制进行变量扩展.我觉得解决方案在于正确使用gensym,但我不确定如何.
;;helper macro
(defun class-slots-symbols (class-name)
"Returns a list of the symbols used in the class slots"
(mapcar 'closer-mop:slot-definition-name
(closer-mop:class-slots
(find-class class-name))))
;;macro that I am having difficulty with
(defmacro obj-to-hash (obj-inst)
"Reads an object, reflects over its slots, and returns a hash table of them"
`(let ((new-hash (make-hash-table))
(slot-list (class-slots-symbols (type-of ,obj-inst))))
;;The slot-list needs to expand out correctly in the with-slots form
(with-slots (slot-list) obj-inst
(loop for slot in slot-list do ;and also here
(format t "~a~&" …
Run Code Online (Sandbox Code Playgroud)