How to write pseudocode for functional programming languages like Scheme or Haskell? All that I searched for showed C style or Python style pseudocode.
-
5I think this is highly dependent on which functional language you want to write pseudocode for. Haskell, ocaml and fsharp have a completely different approach than erlang, clojure/lisp. Thus the pseudocode will look different. For haskell I'd write mostly type signatures and explain the algorithm underneath in prose. For the dynamic languages i have not enough hands on experience to give you any hints – epsilonhalbe Sep 02 '16 at 15:02
-
4Why do you want to write it? Is someone saying "show me the pseudo-code", or are you trying to understand how to design code in a functional style? If the latter then pseudo-code is probably the wrong way to go about it. A bit more background to your question would be useful. – Paul Johnson Sep 02 '16 at 15:43
-
2Often (albeit not always), functional code is very compact and high-level, so in a small space you can fit the whole algorithm, making pseudocode unneeded. IMO, pseudocode is more useful when you have a too verbose syntax (which harms understanding), or when you want to skip some boring portions (stubs would work fine here). – chi Sep 02 '16 at 16:52
-
1If I'm sketching out some Haskell, I typically leave out obvious function and method definitions (e.g., `instance Sequence Seq` with no details), or functions with type signatures and no code. I also write it without using GHC, so some of the types can be a bit off if I'm sketching something fancy. – dfeuer Sep 02 '16 at 17:23
-
@PaulJohnson I am a university student and I am asked to explain everything using pseudocode be it in my assignments or my exams. – Vasantha Ganesh Sep 03 '16 at 18:19
-
Do you know that they would accept functional pseudocode (however that might work)? Some teachers are actually pretty particular with the format they want the pseudocode in. You should find out what they're looking for. – David Young Sep 03 '16 at 22:38
-
The term "pseudocode" is an implicit admission that your programming language is too low-level to express yourself clearly. – John Clements Sep 06 '16 at 20:53
2 Answers
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.

- 47,942
- 4
- 47
- 79
If I'm writing an algorithm in pseudo code in a functional style then I might mix and match languages but will consistently use:
- let-bound variables
- function application
- maps and folds
Take, for example, a hash function:
hash data =
let blocks = chunksOf blockSize (preprocess data)
foldr updateContext initialContext blocks

- 64,245
- 7
- 109
- 166