User talk:Dcoetzee/Wikicode
This pseudocode is no longer a proposed standard. See User:Dcoetzee/Wikicode.
Prologue
[edit]See also User talk:Dcoetzee/Wikicode/Specification for discussions. -- Kaihsu 15:54, 2004 Sep 27 (UTC)
For background see also Talk:Pseudocode and Talk:Algorithms on Wikipedia.
Discussion begins
[edit]Um, the bit in the opening of User:Dcoetzee/Wikicode where it says Wikicode is a new standard and Whenever you write pseudocode in an article, this style should be used -- that's just wishful thinking, right? Did I miss a vote making this an official policy, or is someone jumping the gun here? -- See also my comments at User talk:Dcoetzee/Wikicode/Specification for some context. Wile E. Heresiarch 06:21, 24 Sep 2004 (UTC)
- I agree with Wile E -- I don't intend to use this should it ever come up unless we have a serious proposal and voting, and am forced to by consensus. I feel it's a bad move to invent a language for pseudocode for an encyclopedia. --Improv 04:31, 30 Sep 2004 (UTC)
I do not know the reasons of writing a mixture of C and Pascal: in my opinion it's better to move to only one side. Since I have written in both languages, I feel I can suggest to move towards C because it is well known, has more syntax capabilities, and (for what it may weight for) I do prefer it over Pascal. --Enrico. 13:38, 05 Oct 2004 (UTC)
- Again, Wikicode is not a mixture of C and Pascal, and I don't understand why it continues to be characterized in this way. It does not use machine integers, the braces are optional almost everywhere, and at one time it used to use parenthesized comments and ML-style type declarations (I still would prefer the latter). This is not a C/Pascal pseudocode, although it is an imperative pseudocode. Derrick Coetzee 16:09, 5 Oct 2004 (UTC)
My attempt to fix the first example uses unsigned types, which it turns out we don't have. I'm trying to think of a way to fix that issue without over-complexifying things too much, I'd appreciate help. Pakaran. 13:02, 24 Sep 2004 (UTC)
- This simple example points up a serious flaw in the whole notion of Wikicode: User:Pakaran wants to invoke unsigned integers but he feels prevented by "the standard" -- it turns out we don't have [that type]. Even in this trivial example, Wikicode is keeping someone from expressing what they want and should express. In pseudocode, people customarily make up stuff as they go along, as demanded by the problem at hand. Limiting Wikipedia writers to the subset of concepts embodied in Wikicode is unnecessary and counterproductive. Wile E. Heresiarch 14:58, 24 Sep 2004 (UTC)
- The standard specifically says that you can invent new types wherever they are needed, or any particular other construct that is handy for the situation. If a real language fits particularly well, this can be used too. This will be clarified. Again, the point of the standard is not to restrict but to avoid unnecessary inconsistency. Derrick Coetzee 18:05, 1 Oct 2004 (UTC)
I suppose that some of the purposes of Wikicode were
- to remove dependancies on existing languages and thus stand the test of time
- to encourage programmers who use language-X to understand the algorithm rather than the implementation
- to highlight the algorithm rather than the language syntax and features
- to help non-programmers approach a (semi?)-formal definition of an algorithm
If these are right, how close would Wikicode come to meeting them? DerekP 01:29, 14 December 2005 (UTC)
- Well, one way that it does so is by assuming by default high-level abstractions such as arbitrary-precision real numbers and arbitrary-size integers. Many operations like memory allocations that require careful but uninteresting handling in real code never fail. Another is to use verbose, clear names instead of cryptic shortened names, like "bool" for "boolean" or "strcpy" for "stringCopy". I found these sorts of things weren't typically a problem in the ad hoc pseudocode I've seen though — the primary purpose of Wikicode was to create a consistent style of pseudocode with consistent notation and formatting for use in many articles, as opposed to a jumble of different styles. Deco 01:39, 14 December 2005 (UTC)
Language Enhancements
[edit]Right now Wikicode looks like some strange mixture of Pascal and C. Not that I mind, but it seems better to make it somewhat closer resemble one of those rather than hang in the middle, so as to prevent confusion. For instance, type declarations are probably better put after the variable, as in:
var x: int := 2
- Originally the definitions were exactly like this. This was violently opposed by User: Wrp103, so was changed. Derrick Coetzee 18:02, 1 Oct 2004 (UTC)
I don't understand why comments on separate lines use brackets instead of double-slash. The latter is so common nowadays that pretty much anyone will instantly recognize it as a comment; besides, a comment on a separate line can (and usually is) treated as a comment at the end of a blank line, so why add more special cases?
- Sounds good to me. This was a result of a strange evolution of compromise. Changed. Derrick Coetzee 18:19, 1 Oct 2004 (UTC)
I'm not sure if I like the idea of implicit line continuation in absence of explicit statement separator. Relying on indentation for that would be nice if the language as a whole used the same concept. But the way it is now, it is possibly confusing. How about using backslash instead?
- The language as a whole does use this concept. Braces are optional almost everywhere. Maybe the examples should emphasize this more. Derrick Coetzee 18:19, 1 Oct 2004 (UTC)
Data type names. Again, a strange mixture. On one hand, shortened versions such as bool and int. On other hand, character rather than char. Perhaps better to make them all long? Would probably make it somewhat more readable, especially for someone with no background in C-derived languages.
- Good point. I'm going to leave int alone, just because that's so common, but boolean is good. Changed. Derrick Coetzee 18:19, 1 Oct 2004 (UTC)
Operators. First, in begs for not in (as in Python). Logical implication operator, e.g. implies, would be nice for some cases. Also, what about bitwise operators? We might need them. Perhaps C-style? But ^ is already used for exponentiation... How about replacing it with **? Oh yes, and what are the precedence rules?
- Operators can be invented wherever necessary. A lot of code that does a lot of bit manipulation should just be written in C. Derrick Coetzee 18:19, 1 Oct 2004 (UTC)
Description of control flow constructs is somewhat ambiguous. For example, it is not clear whether if-else requires then in it or not - some samples are with it, some are without. I'd suggest to leave it there, if only just for readability: if cond then action
is easier to read than if cond action
. In similar vein, while should perhaps have do after condition. do-while can be replaced with repeat-while. Oh, and while we're at it, how about repeat-until? for desperately needs a step keyword, and perhaps also do before the code block.
- Actually the spec says that then is only required where the condition and action are on the same line, specifically to clear up the potential confusion you demonstrate. Derrick Coetzee 18:19, 1 Oct 2004 (UTC)
Syntax for list and sets is strangely different. For list, an insert-at-of statement is used to add elements to the list. For sets, an insert() function is used. They should probably be made similar. Also, syntax for declaring variables of set, list, and map types is not defined. I suggest set, list-of and map-of-to as in the following example:
var aSet: set var aList: list of string var aMap: map of string to integer
--Int19h 04:49, 25 Sep 2004 (UTC)
- Wikicode is pseudocode and not intended to be a machine-runable language. As stated in the article, If any particular article requires you to make up functionality or change syntax, you're free to do so. Perhaps that statement needs more emphasis though.
- Obviously. However, such made-up functionality and additional syntax are best be organized. Otherwise we'll eventually end up with articles written in several, subtly different, dialects of WikiCode. --Int19h 08:31, 25 Sep 2004 (UTC)
- Also note that being pseudocode I'd prefer to follow mathematical conventions more than traditional programming languages. For instance rather than "unsigned int", why not natural number or non-negative. Also I'd prefer to avoid unnecessary shortening of keywords; why not variable rather than var, just like function was chosen rather than fun. int is perhaps so well known as to not matter, but still, perhaps the fully spelled-out integer would be better for pseudocode. Keep in mind that pseudocode may be used in articles other than those familiar to programmers, such as in mathematics. Complete words make it more readable. -- Dmeranda 04:57, 25 Sep 2004 (UTC)
- Agreed. Especially since we can use formatting, so identifiers (and thus data types) can consist of several tokens. --Int19h 08:31, 25 Sep 2004 (UTC)
This is a terrible idea. One of the things which makes the code examples useful is that they are real languages illustrating a variety of concepts of both the article topic and the language used. Jamesday 17:16, 1 Oct 2004 (UTC)
How about % for modulus? Or is that not clear enough to everyone? Jimbobbob 03:52, 2004 Nov 20 (UTC)
I feel that an advantage of C over Pascal is shorter and less English dependent syntax. For example, { } instead of the English words begin end, ? : in addition to if then else (a pity that unlike the if keyword, the question mark isn't before the condition), and no need for a function or procedure keyword. As to mathematical conventions, why not N instead of natural number, and the use of Set theory notation, such as instead of unsigned int i;
As to the idea of using then and long words to make code more readable, see Cobol. Instead, borrow an old idea from program editors: syntax highlighting.
Types
[edit]Types are confusing to those who do not need them, and those who do can figure them out.
- Luckily, types are entirely optional. They're only placed where the author judges the type to be unclear and/or important. If the guide does not make this clear, I'll update it. Your statement is also not always true; types are sometimes an effective form of compiler-checked documentation, particularly in module interfaces. Derrick Coetzee 02:24, 13 Oct 2004 (UTC)
Formalizing
[edit]We need a standard for Wikicode that is sufficently detailed to allow construction of a syntax check function in the editor. Perl 6 could be used as a start. We also need objects for articles which require them. Again, Perl 6 is a good start. Watsonladd
Only use wikicode where a real language isn't available
[edit]If there's no code in a real language available, I guess it's ok to use wikicode if there's nothing better, but if you do come across wikicode, try to convert it to more readable (pseudo-)english, java or python (though if you insist on pascal or scheme, I'm not stopping you :-) ). It's much better to have actual readable text or runnable code usually, rather than having a fixed syntax pseudopseudocode (there's a *reason* why pseudocode has no fixed syntax, that's why it's called pseudocode. :-) ). Kim Bruning 15:16, 16 Dec 2004 (UTC)
- Heh, pseudopseudocode. But yeah, this idea is in many ways the worst of both worlds.
Turing
[edit]I made this proposal on the specification talk page, but it seems like few people read that page...
A different proposal
[edit]I agree with all who have said that inventing a Wikipedia-specific language is a bad idea. I also believe that it is senseless for articles to contain 10 different implementations of everyone's favorite language and that programming articles should have one consistent programming language. Imagine an introduction to CS textbook that implemented every other example in a new language... not good for learning.
If you look on SourceForge by programming language, the languages there have these project counts:
- 15177 C++
- 14638 C
- 14578 Java
- 10767 PHP
- 5788 Perl
- 3911 Python
- 2320 C#
- 2263 JavaScript
- 2066 Visual Basic
- 1759 Delphi/Kylix
Which is interesting, but I don't think any of those languages are suitable example languages for many reasons, not the least of which will be endless battles between advocates of different languages, as well as unfairly making editing harder for people not as skilled in that language.
My proposal is simple and is based on these assumptions and premises:
- use an existing language
- use a language designed to have simple clear syntax (read: teaching language)
- do not pick a top 10 language, choose neutral ground
- do not pick a language because computer experts like it, but because it works best in an encyclopedia
- readability is more important than runability on your computer (runability is going to be different for most of the reader base)
- designing our own language is silly (and the straw poll backs this up)
The proposal:
- In each programming article, use the Turing programming language in the article text.
- Below the example, put a list of links in short form like C, Pascal, Python, Perl, ... to sub-articles with additional examples in more useful languages.
Both parts of the proposal are critical.
Daniel Quinlan 10:05, Apr 11, 2005 (UTC)
More on the proposal:
This paper contains an interesting survey of computer programming teachers and their preferred language choice. Daniel Quinlan 10:23, Apr 11, 2005 (UTC)
- I'm glad you support the idea of a consistent code. I abstain from any further comment - I really want to forget this episode of my life. Deco 08:41, 22 Apr 2005 (UTC)
Let the reader choose
[edit]I can choose the size of the fonts in my browser. I would like to choose my favorite programming language too. Is this completely impossible?
Every user might sort the available programming languages in order of precedence in his profile. Or he might ommit this step and then the default order would be set in a most-voted fashion. The list might include Wikicode!
The code of a page would be displayed in the first available programming language of the user's list.
This behaviour might be activated or deactivated in the pages containing source code. (It would be somewhat confusing to see C code in a specific Pascal programming page)
I'm just thinking loud, but... ?? --Comocomocomocomo 15:13, 22 Apr 2005 (UTC)
- MSDN entries have a feature very much like this. It displays code samples in 3 or 4 languages, and there is a filter available to limit it to one. It does make some sense that people would learn best from whatever language they're most familiar with. Deco 16:40, 22 Apr 2005 (UTC)
However, MSDN entries simply have a variety of samples hard-coded in, and display only one. Transforming code from one language to another is not feasible. Radiant_* 14:53, Jun 3, 2005 (UTC)
- Of course this is true. I was under the impression that Como was suggesting a mechanism similar to MSDN whereby editors could include samples in many languages and the viewer chooses one (or some). If you don't believe editors will write the same sample in many languages, you haven't seen Quicksort implementations (which used to be part of Quicksort). Deco 21:02, 3 Jun 2005 (UTC)
- Yes, I meant samples included by the editors. I have not seen the MSDN case, but it sounds like what I was thinking. Those ase bad news: I'd bet it has been patented. --Comocomocomocomo 11:07, 6 Jun 2005 (UTC)
- Of course this is true. I was under the impression that Como was suggesting a mechanism similar to MSDN whereby editors could include samples in many languages and the viewer chooses one (or some). If you don't believe editors will write the same sample in many languages, you haven't seen Quicksort implementations (which used to be part of Quicksort). Deco 21:02, 3 Jun 2005 (UTC)
TfD nomination of Template:Wikicode
[edit]Template:Wikicode has been nominated for deletion. You are invited to comment on the discussion at Wikipedia:Templates for deletion#Wikicode. Thank you.
Huh?
[edit]Since Wikicode is apparently used in a number of pages, this page may still be useful to readers. I suggest you to move it back to Wikipedia nameplace. I personally think the standartised pseudocode is a great idea, and as no reasonable alternatives had been proposed we should stick with your variant. I see no indication that everyone dislikes it, and if someone does they can change it, it's a wiki after all. Grue 18:03, 16 Jun 2005 (UTC)
- I think it's a great idea, but really, it was shot down months ago, and even recently there were people going around deleting uses of the template, leaving disparaging messages in various places, and so on. Not one person voted in favour of it in the "straw poll". Hardly anyone but me ever edited this page or the specification. I don't want to make a war out of this, I just want it to rest in peace and for these antagonists to leave me alone, and that's why I am withdrawing it. I don't discourage anyone from using it; I like the design and it should still be useful even if it's not standardized and the template is not used (I think the template and introduction page were useful, but they upset a lot of editors, so that's why I'm asking the not be used). Deco 00:50, 19 Jun 2005 (UTC)
- Like Grue, I am of the meaning that such a standard pseudocode is a Good Thing™, and am honestly not able to grasp the fact that the stuff was voted into oblivion. I'm sorry to say I have to assume some of the responsibility for the demise myself---I should have voted, but were "too" preoccupied with other (wiki)things at the time.
- However, I actually think there's a chance of wikicode getting a new lease on life after a reasonable amount of time. The trick would be, IMO, to sell the wikicode standard as part of a package concept of 'pseodocode plus some real prog.lang. code' (inspired by a comment of one of the voters). The specific programming languages (taken from some agreed-upon set, or perhaps just allowed to be chosen freely by contributors) showing the algorithms would, in the beginning, vary from article to article, depending on the prog.lang. proficiency of the contributors---but given the large amount of programming-capable wikipedians, I think most algorithms would be described in many prog.lang.s before long.
- Maybe the best way to 'placate' the naysayers would be to come up with a layout somewhat like the following sketch (section header underlined by
===
, subsec headers by---
):
- Maybe the best way to 'placate' the naysayers would be to come up with a layout somewhat like the following sketch (section header underlined by
Algorithm ========= Pseudocode ----------------------------- <wikicode listing> C programming language ----------------------------- <C listing> Scheme ----------------------------- not available, please add FOO programming language ----------------------------- <FOO listing> |
- One could either have the 'real prog.lang.' subsecs displayed directly below the wikicode subsec, or place them in a separate subpage (with a link like "Algorithm, in actual programming languages")---in both cases indicating the requests for filling in missing language codestubs ("not available, please add"). Thus, those annoyed contributors (and as we know, there'll be many of those) are encouraged to add their preferred language to the article's code variants of the algorithm.
- I guess a final alternative would be to use an agreed-upon existing pseudocode 'standard', such as the one from the Dragon Book. Still, Wikipedia is now beginning to be influential to a degree suggestion that (y)our own well-thought-out pseudocode could actually become the standard within, say, a decade... :-) I base this optimistic (raving?) forecast on the fact that, up to now, no single pseudocode language has become really predominant, including those in major textbooks.
- For my own part, I remember with disgust the kind of unbalanced-looking pseudocode used in Cormen/Leiserson/Rivest's Introduction to Algorithms, which I had to trudge my way through at university back in '92. Note that, at the referenced Amazon page, Cormen himself actually comments on the pseudocode. :-) --Wernher 09:19, 19 Jun 2005 (UTC)
- There was an interesting suggestion above of having "language filters" akin to MSDN's, where you could specify the language you want to see samples in in your user preferences, and it just shows you that sample. Alternatively we could just have an implementations page and a series of anchor links to it (such as "See this sample in: C, Java, ARM assembly"). While this would be nice for readers, though, I don't honestly believe it would "placate the naysayers". Many of them are adamant that a standard pseudocode of any sort should not even exist. Deco 22:35, 19 Jun 2005 (UTC)
Comparative Samples
[edit]I thought it would be a bit of fun to see how wikicode would look against other coding styles. DerekP 07:08, 14 December 2005 (UTC)
Factorial
[edit]- With Recursion
function Factorial(int X) if X < 0 return "Invalid argument" if X = 0 return 1 return Factorial(X-1) * X
Define the function called 'Factorial' as one which receives a single integer argument, X. If the value of X is less than zero then tell the caller that X is invalid and stop. If the value of X is exactly zero then tell the caller the result is one (1) and stop. Otherwise tell the caller that the result is X multiplied by the result of performing this function again with X minus one as the argument.
function Factorial ( integer X ) if X < 0 then return "Invalid argument" end if if X = 0 then return 1 end if return Factorial(X-1) * X end function
int Factorial ( int X ) { if (X < 0) return 0; /* "Invalid argument" */ if (X == 0) return 1; return Factorial(X-1) * X; }
- Without Recursion
function Factorial(int X) var result if X < 0 return "Invalid argument" result := 1 for i from 1 to X result := result × i return result
Define the function called 'Factorial' as one which receives a single integer argument, X. If the value of X is less than zero then tell the caller that X is invalid and stop. Set the initial result to one (1). For every integer from 1 to X inclusively, replace the value of the result with current value of result multiplied by the integer. Tell the caller the value of the final result
function Factorial ( integer X ) integer result if X < 0 then return "Invalid argument" end if result = 1 for i = 1 to X do result *= i end for return result end function
int Factorial ( int X ) { int result; if (X < 0) return 0; /* "Invalid argument" */ result = 1; for (int i = 1; i <= X; i++) result *= i; return result }
- The wikicode isn't exactly right, but it's close enough. I think for a simple function like this just about any language is adequate as long as it doesn't use weird syntax for normal things. Although to us the C and wikicode examples look quite similar, I like that the wikicode doesn't use operators or syntax that are only idiomatic to C (or descendant) programmers, like "*=", "++", and the good old three-part for loop. In the original Wikicode proposal I used ML-style type declarations, like "var result : int", also recommended by Damian Conway in his SPECS syntax for C++, but this made some people extremely upset for some reason. Here's how I would write the two samples above with that original syntax:
These functions compute the factorial of a nonnegative integer n using recursion and iteration, respectively:
function factorial(n : int) if n = 0 return 1 return n × factorial(n-1)
function factorial(n : int) var result : int result := 1 for i from 1 to n result := result × i return result
- Notice that I assume valid input, but state assumptions about the input in the text. This is typical in mathematical writing. I also fixed several minor syntactic things that Wikicode requires for consistency like indentation and replacing * with ×. I've sometimes considered italicizing single letter variables, as in math, but I don't really like this idea. I've also considered using ← for assignment, as in:
function factorial(n : int) var result : int result ← 1 for i from 1 to n result ← result × i return result
- I had noted the original declaration idea and the '←' operator, but I was using the latest specification as recorded by yourself. And I take your point about assuming valid input. DerekP 01:33, 15 December 2005 (UTC)
- Incidentally, your English examples are entirely horrific (maybe intentionally). If you're going to write any pseudocode in English, I would make it high-level or vague stuff, never individual operations. Deco 00:39, 15 December 2005 (UTC)
- LOL ... Yes, I was being a little bit facetious. However it was pseudo-code and not a discussion. That is, it was meant to be constrained-formal-perscriptive rather than conversational. DerekP 01:33, 15 December 2005 (UTC)
- Yeah, it seems like any kind of formal writing intended to be narrowly interpreted ends up sounding terrible. Law is a case in point. This is one reason I don't like the idea of pure-prose pseudocodes. I do think that prose directions are a great way to give a high-level overview of an algorithm though, without getting into the finer details.
- One of the big conflicts I had designing wikicode, as I discussed elsewhere, was finding a balance between something that's easy to read and looks nice, and something that's easy to write and edit. The whole concept of wiki notation centres around the importance of simple editing. I think in this respect it came out pretty well – it's prettier and more readable than ASCII notation, but with little sacrifice to editability.
- It's hard for a typical mainstream programmer (including myself) to back away from the familiar assumptions of C-style syntax and semantics in search of something more neutral and approachable to novices. My two main areas that I tried to draw from are English prose and high school mathematics, because these would generally be familiar even to nonprogrammers. This is why I want "=" to mean "equals", not assignment, why I wanted to avoid the idiomatic 3-part for loop of C (while loops are much more immediately intuitive), why I prefer the more verbose "i := i + 1" to the idiomatic "i++", why I included familiar set operators not usually present in real languages, and so on. I also tried to draw from a number of real languages, such as C's familiar procedural structure and keywords, Python's grouping-by-indention (which often reduces visual clutter), ML's intuitive type declarations and optional types, Lisp's arbitrary precision integers, Pascal/Java's built-in string operations, and so on.
- All in all I think the design of wikicode for its intende purpose turned out quite well, but the opposition was so violent that I was upset about it for a long time and still feel jaded. It occurs to me though: part of the opposition was based on the idea that "real" languages can be verified by compiling and testing with real tools. Maybe I should write a real compiler and interpreter for wikicode. Admittedly, the world has enough languages, but few real languages have been designed primarily for expository purposes. Deco 04:17, 15 December 2005 (UTC)
- LOL ... Yes, I was being a little bit facetious. However it was pseudo-code and not a discussion. That is, it was meant to be constrained-formal-perscriptive rather than conversational. DerekP 01:33, 15 December 2005 (UTC)
Not pseudocode
[edit]I know this page is obsoleted, but: you don't seem to understand what pseudocode is. It's supposed to be simple English (or whatever is the reader's language) describing an algorithm without any implementation details. As soon as you start imposing your own syntax and semantics on it, it becomes an actual, more or less formally defined programming language instead of pseudocode. 86.157.63.135 (talk) 01:51, 25 March 2008 (UTC)