如何编写Scheme或Haskell等函数式编程语言的伪代码?
我搜索到的所有内容都显示了C风格或Python风格的伪代码。
(define (huffman nodes)
(if (single-node? nodes)
(first nodes)
(let ([new-node
(make-node (first nodes)
(second nodes))])
(huffman (insert-sorted new-node
(cddr nodes))))))
single-node?
、make-node
和insert-sorted
会导致错误,但在CL中,您实际上可以使用它,并且它会跳转到调试器,询问是否要定义其中一些内容,因此您可以随时实现缺失的部分,并继续执行直到完成所有操作。如果我用函数式伪代码编写算法,那么我可能会混合使用不同的语言,但始终会使用:
例如,考虑一个哈希函数:
hash data =
let blocks = chunksOf blockSize (preprocess data)
foldr updateContext initialContext blocks
instance Sequence Seq
),或者只有类型签名而没有代码的函数。我也不使用GHC编写它,因此如果我正在草拟某些复杂的东西,则某些类型可能会有点偏差。 - dfeuer