Types F#如何构造匹配和满足类型

Types F#如何构造匹配和满足类型,types,f#,match,Types,F#,Match,我有一个函数archive_lb_files,我想使用一个测试函数剔除除一个之外的所有匹配项。我不确定如何更正存根匹配,因此无法获得 这个表达式应该有string->bool类型,但这里有unit类型 错误 这是代码片段,FileMaint.fm.archive\u lb\u file lb\u dir archive\u dir fn返回布尔值 (* Subroutine to process files we fetch from IC, and then need to be upload

我有一个函数
archive_lb_files
,我想使用一个测试函数剔除除一个之外的所有匹配项。我不确定如何更正存根匹配,因此无法获得

这个表达式应该有string->bool类型,但这里有unit类型

错误

这是代码片段,
FileMaint.fm.archive\u lb\u file lb\u dir archive\u dir fn
返回布尔值

(* Subroutine to process files we fetch from IC, and then need to be uploaded to Munis.  *)
let process_payment_processor_files spool_dir unload_dir paymt_dir report_date =
    use fHLog = FileMaint.fm.open_log_file_out (FileMaint.fm.generate_log_file ())
    // We'll flesh this in when Stratus is installed, and we know the directory names. cmn 5/26/2017
    ignore

let test_print fn =
    printfn "%A" fn

let archive_lb_files lb_dir archive_dir report_date =
    use fHLog = FileMaint.fm.open_log_file_out(FileMaint.fm.generate_log_file () )
    let lb_fnam_template = "*_lockbox.txt"
    let dir_list = Directory.GetFiles(lb_dir, lb_fnam_template)

    for pfn in dir_list do
        let fn = Path.GetFileName(pfn)
        match fn with
        | "cb_lockbox.txt" -> FileMaint.fm.archive_lb_file lb_dir archive_dir fn
        | "ics_lockbox.txt" -> (test_print fn) 
        | "ic_lockbox.txt" -> test_print fn
        | _ -> test_print "does not exist"
在回答答案和注释时,
如何归档lb_文件
返回函数

(* Moves a file to a unique file name but not including a central directory in between a source and destination. *)
let archive_lb_file lb_path archive_path lockbox_file_name copy_only =
    use fH = new StreamWriter(base_log_file, true)
    let rc =
        try
            if (0 = String.Compare(copy_only, "copy_only")) then
                File.Move((lb_path + lockbox_file_name), archive_path) |> ignore
                fH.WriteLine(generate_time_stamp + ": " + lb_path + lockbox_file_name + " moved to " + archive_path)
            else
                File.Copy((lb_path + lockbox_file_name), archive_path) |> ignore
                fH.WriteLine(generate_time_stamp + ": " + lb_path + lockbox_file_name + " copied to " + archive_path)
            true 
        with ex -> 
            fH.WriteLine(generate_time_stamp + ": " + "An error occrred trying to move or copy " + 
                         lb_path + lockbox_file_name + " to " + archive_path)
            printfn "%A" ex
            false
    rc

匹配表达式的所有分支必须具有相同的类型。根据您发布的错误消息判断,匹配表达式的第一个分支的类型为
string->bool
,这意味着您对
FileMaint.fm.archive\u lb_file
的调用可能缺少一个参数:它需要另一个
string
,得到一个后,它承诺返回一个
bool

由此,解决方案将分为两部分:

  • 将缺少的参数传递到
    FileMaint.fm.archive\u lb\u file
    (我不能再帮你了,因为我看不到该函数的定义,因此不知道缺少的参数应该是什么)
  • 使
    test\u print
    函数返回相同的类型,即
    bool
    。为此,只需添加一个返回值:

  • 也就是说,我怀疑您的程序实际上有一个逻辑错误:
    FileMaint.fm.archive\u lb\u file
    返回bool,但您忽略了它。你应该吗?如果返回值对您来说无关紧要,那么函数应该首先返回它吗?

    匹配表达式的所有分支都必须具有相同的类型。根据您发布的错误消息判断,匹配表达式的第一个分支的类型为
    string->bool
    ,这意味着您对
    FileMaint.fm.archive\u lb_file
    的调用可能缺少一个参数:它需要另一个
    string
    ,得到一个后,它承诺返回一个
    bool

    由此,解决方案将分为两部分:

  • 将缺少的参数传递到
    FileMaint.fm.archive\u lb\u file
    (我不能再帮你了,因为我看不到该函数的定义,因此不知道缺少的参数应该是什么)
  • 使
    test\u print
    函数返回相同的类型,即
    bool
    。为此,只需添加一个返回值:

  • 也就是说,我怀疑您的程序实际上有一个逻辑错误:
    FileMaint.fm.archive\u lb\u file
    返回bool,但您忽略了它。你应该吗?如果返回值对您来说无关紧要,那么函数应该首先返回它吗?

    匹配语句中的所有模式都必须返回相同的类型。您的
    test\u print
    返回单位,但您的
    FileMaint.fm.archive\u lb\u文件
    返回函数
    string->bool
    。如果您将方法更改为

    let test_print fn =
        printfn "%A" fn
        true
    
    将修复您遇到的错误,即使这看起来很奇怪

    编辑: 由于您发布了缺少的函数,因此您看到的错误是由于没有在调用中传递
    copy\u only
    参数造成的

        match fn with
        | "cb_lockbox.txt" -> FileMaint.fm.archive_lb_file lb_dir archive_dir fn true
        | "ics_lockbox.txt" -> (test_print fn) 
        | "ic_lockbox.txt" -> test_print fn
        | _ -> test_print "does not exist"
    
    添加
    true
    将调用该函数。调用中没有出现错误的原因是因为curry函数


    匹配语句中的所有模式都必须返回相同的类型。您的
    test\u print
    返回单位,但您的
    FileMaint.fm.archive\u lb\u文件
    返回函数
    string->bool
    。如果您将方法更改为

    let test_print fn =
        printfn "%A" fn
        true
    
    将修复您遇到的错误,即使这看起来很奇怪

    编辑: 由于您发布了缺少的函数,因此您看到的错误是由于没有在调用中传递
    copy\u only
    参数造成的

        match fn with
        | "cb_lockbox.txt" -> FileMaint.fm.archive_lb_file lb_dir archive_dir fn true
        | "ics_lockbox.txt" -> (test_print fn) 
        | "ic_lockbox.txt" -> test_print fn
        | _ -> test_print "does not exist"
    
    添加
    true
    将调用该函数。调用中没有出现错误的原因是因为curry函数


    不,不会:第一个分支的类型是
    string->bool
    ,而不仅仅是
    bool
    。你说得对。编辑注释以添加奇怪的返回语句。我已经发布了调用的函数。我认为它返回的是bool,而不是函数。@octopusgrabbus在调用
    archive\u lb\u file
    时错过了传递
    copy\u only
    参数,这使得它返回的是函数值。这是由于f#中的currying函数造成的。将
    |“cb_lockbox.txt”->FileMaint.fm.archive\u lb_file lb_dir archive\u dir fn true
    更改为在末尾添加参数,将修复此问题否:第一个分支具有type
    string->bool
    ,而不仅仅是
    bool
    。你说得对。编辑注释以添加奇怪的返回语句。我已经发布了调用的函数。我认为它返回的是bool,而不是函数。@octopusgrabbus在调用
    archive\u lb\u file
    时错过了传递
    copy\u only
    参数,这使得它返回的是函数值。这是由于f#中的currying函数造成的。将
    |“cb_lockbox.txt”->FileMaint.fm.archive\u lb_file lb_dir archive\u dir fn true更改为在末尾添加参数将修复此问题