8

In colleges and in algorithm textbooks, it is quite common for the teacher and author to explain control flow in pseudo-code. With the advent of more expressive languages like Python and Haskell among others, is it reasonable that colleges switch to explain algorithms via one of these languages?

The only advantage of pseudo-code I can think of is that it's purportedly language-agnostic. But it is not. Some pseudo-code uses an imperative approach, other pseudo-code looks functional. Authors just borrow semantics from whatever programming language they are comfortable using, or worse just describe the semantics in natural language. So if pseudo-code isn't actually language-agnostic, what is the advantage of using it then? Wouldn't it be better to just use an existing language?

  • 5
    "I could have chosen the language of a particular machine X, but then those people who do not possess machine X would think this book is only for X -people. Furthermore, machine X probably has a lot of idiosyncrasies that are completely irrelevant to the material in this book yet which must be explained; and in two years the manufacturer of machine X will put out machine X + 1 or machine 10X, and machine X will no longer be of interest to anyone." (Donald Knuth, TAOCP) – Kilian Foth Feb 22 '16 at 7:47
  • @KilianFoth Nice . I believe Knuth also said he foresaw a future where programs would be written in English and separated by whitespace ( though I cannot find the source ) , which is almost like saying the pseudo-code would be executable :) Though the idiosyncrasies are truly distracting , a formal subset of features would be acceptable. – Asterisk Feb 22 '16 at 7:52
  • 1
    @Asterisk you are thinking of literate programming. I would make sure one also reads EWD667: On the foolishness of "natural language programming". by Dijkstra. I would also point out the difficulty that people have formulating questions here that follow consistent English rules of punctuation, capitalization, and spelling. I shudder at the thought of these people writing code following similar styles when writing prose-programming. – user40980 Feb 22 '16 at 14:07
  • 1
    "advent"? Both of those languages are over 20 years old. And Haskell at least was preceded by Miranda, which had almost identical syntax, and is i believe 30 years old now. – Jules Feb 22 '16 at 17:53
  • @Jules Well I named these because they have the cleanest and most succinct syntax that would seem suitable for pseudo-code . Both have gained wider traction in the last 10 years ( or more like last 5 years). Python cracking the top 10 in PyPl and Haskell under 50. There are other languages that are growing ( due to whatever reasons) , but they have heavier syntax and wierded features. Industry is still 50% C++ and Java and then there's JavaScript (which seems like a language designed in reverse , first add features then define rules and constraints ). – Asterisk Feb 22 '16 at 18:09
18

No. The point of pseudo-code is that it doesn't have to compile. I can quickly gloss over irrelevant details. In contrast, even languages that look like pseudocode at the first glance can have very non-intuitive details that would just detract from the algorithm. Let's take for example Quicksort in Haskell:

qs :: Ord a => [a] -> [a]
qs [] = []
qs (pivot:xs) = (qs smaller) ++ pivot:(qs larger)
  where smaller = [x | x <- xs, x <= pivot]
        larger  = [x | x <- xs, x > pivot]

or the same in Python:

def qs(array):
  if not array:
    return []
  pivot = array[0]
  xs = array[1:]
  smaller = [x for x in xs if x <= pivot]
  larger  = [x for x in xs if x > pivot]
  return qs(smaller) + [pivot] + qs(larger)

The advantage in both cases is that this is executable code, and as such can be tested, typechecked, and toyed with by students. However, they both include syntactic details that are distracting. Students would usually be better served by pseudocode that illustrates the intention of the algorithm, not implementation details:

algorithm QUICKSORT(array)
  return [] if array is empty
  pivot ← array[0]
  xs ← array[1, ...] -- the rest of the array without the pivot
  smaller ← [x | x ∈ xs, x <= pivot] -- all smaller or equal elements
  larger ← [x | x ∈ xs, x  > pivot] -- all larger elements
  return [QUICKSORT(smaller)..., pivot, QUICKSORT(larger)...]

Notable differences:

  • I can just make up a list comprehension syntax that looks like maths rather than having to explain why Python has a for and if here.

  • I don't have to explain that language's syntax for list concatenation. Why does Python use + addition? What is : in Haskell? I can just choose a syntax that gets the point across more clearly.

  • the type signature Ord a => [a] -> [a] is just an implementation detail. While possibly helpful in this case, the type signatures sometimes required by Haskell can get absurd.

  • I don't have to explain why Python considers empty collections to be false, and what array[1:] is supposed to mean.

  • I avoid clever students pointing out that I should really use yield in the Python example.

  • Haskell sucks for explaining mutable data structures like Hash Tables, RB trees, ….

  • Things start getting very language-specific once we need complex records to express our algorithms. E.g. Python's object system has a few surprises that are just distracting.

That said, it can be very valuable to use one of these languages in addition to pseudocode, just carefully label what is what.

  • 9
    I agree with you in general, but your example contradicts some of your arguments - your pseudo code and the Python example look extremely similar. That is IMHO a consequence of the Python language containing a whole lot "common sense". – Doc Brown Feb 21 '16 at 16:11
  • 2
    If only a subset of language features / API are used it would be in sync with the essence of pseudo-code. However if we start delving into what would be the best way or most esoteric way to accomplish it in a particular language , then it would be distraction. The scientists that described algorithm came up with their own notation that heavily resembled set-theory. After Turing machine's idea was established and C , Fortran , Lisp came up pseudo-code started borrowing ideas from them – Asterisk Feb 21 '16 at 18:46
  • 2
    I would add that if code can be compiled "as is" then there's a very real risk of "student used cut&paste and learnt nothing". – Brendan Feb 21 '16 at 22:57
  • 3
    You argued I don't have to explain ... what array[1:] is supposed to mean., but then you really ended up having to explain array[1, ...] -- the rest of the array without the pivot in the comments because nobody could've figured out what array[1, ...] means (is your pseudocode array 0- or 1-based, what the heck those ellipsis means, etc). I think that really is in favour of using real language as a basis for pseudocode, as you can simply point people to a real languages' documentation/tutorial instead of needing to learn your pseudocode syntaxes. – Lie Ryan Feb 25 at 2:13
  • 1
    @amon For better or worse, math notations are not as well defined as programming languages. Also the use ellipsis in sequences doesn't have a specific meaning. Sometimes it means it's arithmetic sequence, sometimes it means geometric, sometimes it means next item in some arbitrary sequence that the author is talking about and you have to read five paragraphs away from the code to pick that up. The slice notation has one well-defined meaning, it's not ambiguous. Also, while ellipsis may have been common in sequence, you still hadn't specified how the sequence interacts with square brackets. – Lie Ryan Feb 26 at 15:23
10

No.

The entire purpose of pseudo-code is to abstract away the details and complexities of individual languages so that you focus on what the program's supposed to do, rather than how it does it. With pseudo-code you can make up arbitrary rules that do not need to conform to actual implementation requirements the way that real-world languages do, but only to the requirements of the actual topic at hand.

Furthermore, if the logic is presented in a way that you (as the student) can't just copy/paste into a file, compile it and be done, then you are forced to implement the solution yourself even when the solution itself is described for you. This encourages individual thought over copy/paste cheating.

0

What is Real?

Because Real is only in definition to an interpreter.

Is Mandarin any more or less real than English?

  • Certainly Mandarin is not particularly useful to an English speaker
  • similarly English is so much nonsense to a Mandarin speaker
  • unless they speak both.

So Real isn't even the question. Lets rephrase it:

Why is Pseudo Code used instead of a Formal Language?

A simple VENN Diagram can highlight the problem easily. The set of all humans that are English And Mandarin Speakers is the subset of English Or Mandarin Speakers. Because it takes effort to obtain proficiency in any language the intersection is generally much smaller than the union.

The textbook on programming can presume that you understand at least one natural language, the language the textbook is written in. Its generally safe to presume this, as otherwise a different more legible textbook would have been selected. After all learning one language is difficult enough - two is harder.

This gives the first reason for using a pseudo code. It maximises the audience that could easily read the book. This is done by following established language conventions already found in the natural language. Say recipes from cooking, mathematical formulas, etc... Any gap can be bridged by a quick natural language explanation, or failing that a final recourse to our visual system with pictures.

As for why the common language could not be the programming language. I leave to you to consider how much Mandarin (or any language you do not already speak) you have learned by reading a book about programming written with examples given in a familiar programming language.

What A textbook Achieves

As for the second reason, consider what a textbook must achieve:

  • explain why they would bother to learn an alien language instead of just using their natural language.
  • explain an alien language to the reader such that they might be able to speak it themselves.

Why Program

Most of the book has to convince you as to why you would want to learn and use this alien language, or any similar language. This means discussing the essence of programming itself.

  • How do you identify a problem
  • How do you break a problem apart
  • How do you architect the data
  • How do you architect the processes
  • How do you manage the dependencies
  • How do you identify faults
  • and more

Most of this has nothing to do with the machines themselves, its mostly a discussion on how meat-ware should operate in order to bring about a program. That's pretty complex because it has to show why we would link our human space goals, to program space problems and strive to solve them.

Describing a Program

The second textbook achievement is describing a Language. Now most programming languages can be described with a Grammar and a few semantic rules. On the shallow end are languages like JSON which can be defined fairly completely within three or so pages. More complex languages need a larger specification, but for the most part do not need a total understanding in order to be useful. What these descriptions are however are Pseudo Code. They specify the formal language in terms of a natural language. The difference is that these pseudo codes are specified in advance.

Now given that even Formal Languages are themselves (Executable) Pseudo Codes, the question is what is most important when describing an algorithm? The next-larger context.

  1. The algorithm has a Goal that is reasonable in that context,
  2. that context has some constraints,
  3. and the algorithm is a description of how those constraints can be handled while achieving the goal.

At no point is the language that the algorithm is written in important. If anything only a few key operations are critical for the algorithms success. So the question then becomes:

  • is it better to describe a meat-ware program capable of interpreting the full specification of a Formal Language like C++/C#/Python/etc... in order to understand the algorithm
  • or just Define the 4 or so primitives required to understand the algorithm.

Given that learning a language is Hard, and the reader must have learned/learn a language in order to understand the algorithm, as the writer of a textbook what should you ask of the reader?

0

I'd go against the grain here and argue that yes, books and tutorials should use pseudocode that's based on real high level languages instead of inventing a wholly new pseudocode syntax.

However, I'd also warn any authors and readers that when used in this context, this should be pseudo-version of the real language, and authors should be mindful not to be too strict about following the syntax and limitations of the language. Instead, authors should think carefully about what is it they're trying to communicate with the snippet, and hide irrelevant details behind function calls and/or comments rather than striving for a full, actually compilable implementation or code that's idiomatic for the chosen base language.

Authors should take some creative liberty on the language to get their point across so that even someone who knows just the basics of the language can still understand the gist of the snippet, while those seeking more exact semantic can refer to the language's documentation or fallback to their prior knowledge about the language to fill in the details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.