繁体   English   中英

Rust Vectors 中的 Haskell 风格链表添加

[英]Haskell-style linked list addition in Rust Vectors

考虑一个(哑)函数在 Haskell 中合并两个排序的整数列表:

merge :: [Int] -> [Int] -> [Int]
merge xs ys = reverse $ merge' xs ys []

merge' :: [Int] -> [Int] -> [Int] -> [Int]
merge' [] ys acc = (reverse ys) ++ acc
merge' xs [] acc = (reverse xs) ++ acc
merge' (x:xs) (y:ys) acc
    | x < y     = merge' xs (y:ys) (x:acc)
    | otherwise = merge' (x:xs) ys (y:acc)

我们如何在 Rust 中复制这种(x:acc)行为,而无需遍历整个向量?

Linked Lists的文档告诉我们不要使用它,而是使用Vec 如果我们使用Vec ,有没有办法让它

  1. 纯功能
  2. 没有任何可变参数
  3. 不依赖于复制整个向量并使用add (所有权问题)?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM