代码之家  ›  专栏  ›  技术社区  ›  Maja Piechotka

在F#中修改了map2(不截断列表)-如何按照惯用的方式进行?

  •  1
  • Maja Piechotka  · 技术社区  · 16 年前

    我想把这样的函数重写成F#:

    zipWith' :: (a -> b -> c) -> (a -> c) -> (b -> c) -> [a] -> [b] -> [c]
    zipWith' _ _ h []     bs     = h `map` bs
    zipWith' _ g _ as     []     = g `map` as
    zipWith' f g h (a:as) (b:bs) = f a b:zipWith f g h as bs
    

    我的第一次尝试是:

    let inline private map2' (xs : seq<'T>) (ys : seq<'U>) (f : 'T -> 'U -> 'S) (g : 'T -> 'S) (h : 'U -> 'S) =
        let xenum = xs.GetEnumerator()
        let yenum = ys.GetEnumerator()
        seq {
            let rec rest (zenum : IEnumerator<'A>) (i : 'A -> 'S) =
                seq {
                    yield i(zenum.Current)
                    if zenum.MoveNext() then yield! (rest zenum i) else zenum.Dispose()
                }
            let rec merge () =
                seq {
                    if xenum.MoveNext()
                    then
                        if yenum.MoveNext()
                        then yield (f xenum.Current yenum.Current); yield! (merge ())
                        else yenum.Dispose(); yield! (rest xenum g)
                    else
                        xenum.Dispose()
                        if yenum.MoveNext()
                        then yield! (rest yenum h)
                        else yenum.Dispose()
                }
            yield! (merge ())
        }
    

    然而,它很难被认为是地道的。我听说了 LazyList 但我哪儿也找不到。

    3 回复  |  直到 16 年前
        1
  •  3
  •   Tomas Petricek    16 年前

    for

    但是,可以编写计算(类似于 seq { .. } IEnumerator<T> seq get it here (代码还包括问题的解决方案)。

    然后你可以写下:

    // Zip using specified functions for sequences 
    let zipWithFun f g h (a:seq<_>) (b:seq<_>) = 
      // Local helper function that works with iterators (xs and ys)
      let rec zipWithFunE xs ys = iter {
        // Try to get first element from both iterators (mutates the iterators!)
        let! x = xs
        let! y = ys
        match x, y with 
        | Some(x), Some(y) -> 
            // If both produced value, then combine values using 'f' & continue
            yield f (x, y)
            yield! zipWithFunE xs ys 
        // If only one produced value, yield the value and then return the
        // remaining values projected using one of the functions
        | Some(rest), _ ->
            yield g rest
            yield! ys |> Enumerator.map g
        | _, Some(rest) ->
            yield g rest
            yield! ys |> Enumerator.map g
        | _ -> () }
    
      // Construct a 'seq' value from a function that processes enumerators
      Enumerator.toSeq (fun () -> 
        zipE (a.GetEnumerator()) (b.GetEnumerator()))
    

    代码的核心部分几乎复制了原始Haskell解决方案的结构,这使得这种方法非常有吸引力,但是您仍然可以直接使用序列,而不必将数据复制到其他数据结构。

        2
  •  2
  •   Brian    16 年前

    懒鬼名单在F# PowerPack . 你可能需要它来写得更优雅些。考虑到您的第一次尝试代码看起来有多好,我希望您在编写LazyList版本时不会遇到任何问题。

        3
  •  1
  •   user332266    16 年前

    let forever xs =
        Seq.append (Seq.map Some xs) (Seq.initInfinite (fun _ -> None))
    
    let zipWith f g h xs ys =
        Seq.zip (forever xs) (forever ys)
        |> Seq.takeWhile (fun (x, y) -> Option.isSome x || Option.isSome y)
        |> Seq.map ( function
            | (Some x, Some y) -> f x y
            | (Some x, None  ) -> g x
            | (None  , Some y) -> h y
            | _ -> failwith "quite unexpected !" )