Mergesort Getting an Error in F#

后端 未结 1 1826
你的背包
你的背包 2021-01-26 03:03
let rec merge = function
| ([], ys) -> ys
| (xs, []) -> xs
| (x::xs, y::ys) -> if x < y then x :: merge (xs, y::ys)
                    else y :: merge (x::x         


        
相关标签:
1条回答
  • 2021-01-26 04:06

    Your mergesort function is missing a case causing the signature to be inferred by the compiler to be 'a list -> 'b list instead of 'a list -> 'a list which it should be. The reason it should be 'a list -> 'a list is that you're not looking to changing the type of the list in mergesort.

    Try changing your mergesort function to this, that should fix the problem:

    let rec mergesort = function
     | [] -> []
     | [a] -> [a]
     | L -> let (M, N) = split L
            merge (mergesort M, mergesort N)
    

    Another problem with your code however is that neither merge nor split is tail recursive and you will therefore get stack overflow exceptions on large lists (try to call the corrected mergesort like this mergesort [for i in 1000000..-1..1 -> i]).

    You can make your split and merge functions tail recursive by using the accumulator pattern

    let split list =
      let rec aux l acc1 acc2 =
        match l with
            | [] -> (acc1,acc2)
            | [x] -> (x::acc1,acc2)
            | x::y::tail ->
                aux tail (x::acc1) (y::acc2)
      aux list [] []
    
    let merge l1 l2 = 
      let rec aux l1 l2 result =
        match l1, l2 with
        | [], [] -> result
        | [], h :: t | h :: t, [] -> aux [] t (h :: result)
        | h1 :: t1, h2 :: t2 ->
            if h1 < h2 then aux t1 l2 (h1 :: result)
            else            aux l1 t2 (h2 :: result)
      List.rev (aux l1 l2 [])
    

    You can read more about the accumulator pattern here; the examples are in lisp but it's a general pattern that works in any language that provides tail call optimization.

    0 讨论(0)
提交回复
热议问题