Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write pseudocode for functional programming languages? [closed]

How to write pseudocode for functional programming languages like Scheme or Haskell? All that I searched for showed C style or Python style pseudocode.

like image 281
Vasantha Ganesh Avatar asked Jul 09 '26 23:07

Vasantha Ganesh


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.

like image 149
Sylwester Avatar answered Jul 11 '26 18:07

Sylwester


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
like image 30
Thomas M. DuBuisson Avatar answered Jul 11 '26 17:07

Thomas M. DuBuisson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!