gpt4 book ai didi

haskell - GHC 拆分性能

转载 作者:行者123 更新时间:2023-12-03 14:54:40 24 4
gpt4 key购买 nike

splitAt 在 GHC 中是这样实现的:

splitAt n xs  =  (take n xs, drop n xs)
  • 那么,splitAt 是否将工作加倍,或者是否有一些幕后优化?
  • 此外, take 和 drop 生成一个递归过程。这是为什么。毕竟它们是库函数,美观并不重要。为什么不实现它们以创建迭代过程?
  • 最佳答案

    您正在查看的定义是 Haskell Report前奏定义。

    从该页面引用(强调我的)

    In this chapter the entire Haskell Prelude is given. It constitutes a specification for the Prelude. Many of the definitions are written with clarity rather than efficiency in mind, and it is not required that the specification be implemented as shown here.



    所以在 GHC 源码中,当你看到
    #ifdef USE_REPORT_PRELUDE
    // Haskell report prelude definition here (inefficient)
    #else
    // Efficient definition here
    #endif

    你应该阅读 #else如果您想查看通常使用的定义,则分支 - 除非您特别要求 Haskell 报告定义。

    关于haskell - GHC 拆分性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20420986/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com