How to write pseudocode for functional programming languages like Scheme or Haskell? All that I searched for showed C style or Python style pseudocode.
In SICP and perhaps other tutorials you have something calle optimistic programming. Instead of pseudocode you just name things and supply the arguments they might take. So imagine you want to make a huffman tree out of a list of sorted nodes from lowest to highest frequency:
(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))))))
This is the complete algorithm and it even will become a part of the resulting actual code, so it's not actual pseudo code either. single-node?, make-node, insert-sorted isn't defined and in Scheme you'll get an error but in CL you could actually use this and it would jump into the debugger where you are asked if you would like to define some of these so you basically would then implement the missing parts as you go and continue the execution until everything is finished.
I guess in Haskell or any other programming language, not only functional ones, you can do this kind of optimistic programming - in the language you are implementing of course. There might be small changes in the end result, but it's not bigger than one would do in other refactorings.
If I'm writing an algorithm in pseudo code in a functional style then I might mix and match languages but will consistently use:
Take, for example, a hash function:
hash data =
let blocks = chunksOf blockSize (preprocess data)
foldr updateContext initialContext blocks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With