Shop OBEX P1 Docs P2 Docs Learn Events
What language could this be? — Parallax Forums

What language could this be?

DavidZemonDavidZemon Posts: 2,973
edited 2015-03-31 11:59 in General Discussion
Has anyone seen a language similar to the psuedo code at the top of this wiki page? It's very intriguing and I've never seen one like it.

http://en.wikipedia.org/wiki/State_pattern
class AbstractTool is
     function moveTo(point) is
         input:  the location point the mouse moved to
         (this function must be implemented by subclasses)
 
     function mouseDown(point) is
         input:  the location point the mouse is at
         (this function must be implemented by subclasses)
 
     function mouseUp(point) is
         input:  the location point the mouse is at
         (this function must be implemented by subclasses)

Comments

  • Bob Lawrence (VE1RLL)Bob Lawrence (VE1RLL) Posts: 1,720
    edited 2015-03-30 06:40
    The state pattern, which closely resembles Strategy Pattern, is a behavioral software design pattern, also known as the objects for states pattern. This pattern is used in computer programming to encapsulate varying behavior for the same routine based on an object's state object. This can be a cleaner way for an object to change its behavior at runtime without resorting to large monolithic conditional statements.[1]:395

    http://en.wikipedia.org/wiki/State_pattern

    Pseudocode

    Take, for example, a drawing program. The program has a mouse cursor, which at any point in time can act as one of several tools. Instead of switching between multiple cursor objects, the cursor maintains an internal state representing the tool currently in use. When a tool-dependent method is called (say, as a result of a mouse click), the method call is passed on to the cursor's state.

    Each tool corresponds to a state. The shared abstract state class is AbstractTool:

    class AbstractTool is
    function moveTo(point) is
    input: the location point the mouse moved to
    (this function must be implemented by subclasses)

    function mouseDown(point) is
    input: the location point the mouse is at
    (this function must be implemented by subclasses)

    function mouseUp(point) is
    input: the location point the mouse is at
    (this function must be implemented by subclasses)
  • Heater.Heater. Posts: 21,230
    edited 2015-03-30 06:41
    Well, as the article says it's "Pseudocode". That is to say not any particular programming language for which there is a real compiler.

    Looking at that code and the comments within it one can easily see how this might be written in an actual language like C++, Java, C#, JavaScript, Python, etc etc etc.

    Aside, back in the day we used to write out the algorithms we wanted in pseudo code and then manually "compile" or translate that to assembler. That made the high level operation of the code easier to think about and discuss before getting down to the details of actual machine instructions. It also provided documentation on how that assembler code was suppose to work. Our pseudo code was an actual high level language "ALGOL". We had no ALGOL compiler so we could be a bit sloppy in the use of this pseudo code syntax.
  • DavidZemonDavidZemon Posts: 2,973
    edited 2015-03-30 06:51
    Sure, I get psuedo code. But every time I've seen someone write psuedo code before, the syntax was heavily inspired by a real language, if not a direct copy. So, I'm going off the assumption that the same is true for this snippet as well.
  • Heater.Heater. Posts: 21,230
    edited 2015-03-30 07:15
    That is true I guess. Like I said we used a close approximation to ALGOL.

    I don't see it in this case. Maybe someone does.

    The idea is to avoid all the syntactic junk of the mechanics of making a real language work. Like types, declarations, whatever, and just use what is necessary to illustrate the idea.
  • mindrobotsmindrobots Posts: 6,506
    edited 2015-03-30 07:42
    Someone that grew up in a Pascal/Modula 2 envronment:

    assigment -> :=
    equality -> =

    :D

    It's hand formatted if you look at the input to the wiki article. It's not like the Java code (ahhhh, my eyes!!!!) which wiki was told about and recognized.

    I think it may just be pseudo-code that someone came up with. It is certainly more formal than mine!

    (yes, my pseudo-code has a garbage collector too!!! :D )
  • GadgetmanGadgetman Posts: 2,436
    edited 2015-03-30 13:52
    The only language-dependent element of that pseudocode is the mention of subclasses, which heavily implies an object-oriented language is supposed to be the target.

    Other than that, that is some of the cleanest declarations I've seen.
    My guess is that the author ordinarily uses some sort of tool that auto-documents projects by lifting definitions and comment blocks out of the source files, and therefore uses a very strict coding regime.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2015-03-30 20:00
    Okay, the snippet in question is pseudocode.

    Problems arrise when one tries to get a definition of pseudocode... just because it is a quick exemplar sketch, not a formal computer language of any sort.

    Trying to assign pseduocode to a particular language is dubious -- sometimes it is obvious, other times it is not.

    Pseudocode is for people to review; other code is for the machine to compile or interpret. The idea is to avoid a presentation to people without a concern for bugs and nuances that distract from grasping the concept and the goals that one desires to present.

    A. There is no pseudocode specifcation that is universally accepted in any formal sense.
    B. Different authors of pseudocode will 'sketch' differently due to differences in knowledge, personal preferences, and education.
    C. A formalization of pseudocode into a standard would like require as much effort to maintain as a true computer language -- a duplication of effort for very dubious reasons.

    http://en.wikipedia.org/wiki/Pseudocode
  • SRLMSRLM Posts: 5,045
    edited 2015-03-30 20:36
    I once had a professor who stressed that psuedocode had a formal syntax, but a quick Google search doesn't turn up anything to back him up.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2015-03-31 08:04
    I am not surprised that there are some professors that claim there is a formal syntax for pseudocode.

    But the simple reality is pseudocode is teaching tool. Would you use the same pseudocode for introductory programing and advanced algorhytms. The beginners wouldn't appreciate a lot of formality, the advanced students would appreciate it.

    The confusion may lie in the fact that some authors formalize a pseudocode style, just for the book they author and for the level audience it is expected to reach. Other authors might try to adopt a popular style and make their psuedo code look similar to C or something else.

    And of course, the real problem is when you use a search engine and you get all sorts of pseudocode for all sorts of audiences and levels.
  • Heater.Heater. Posts: 21,230
    edited 2015-03-31 08:26
    SRLM,
    I once had a professor who stressed that pseudocode had a formal syntax...
    A professor who missed the obvious paradox that if the language in question were codified into a formal syntax it would no longer be pseudo - code now would it?

    On the other hand when Donald Knuth started his life times work "The Art of Computer Programming" he realized he needed a rigorous language with which to discuss algorithms. So he defined the MIX pseudocode very precisely. (I'm not sure if he ever referred to it as pseudo code though).

    MIX is so well defined that readers of his books have built MIX virtual machines to run MIX code. I think even real hardware MIX machines have been built.

    Which, by the way Loopy, again reminds me why programming languages are "real" languages. They are created by humans and used by humans to communicate with each other. Even if there is no computer that can make use of them!
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2015-03-31 09:45
    Heater. wrote: »
    Which, by the way Loopy, again reminds me why programming languages are "real" languages. They are created by humans and used by humans to communicate with each other. Even if there is no computer that can make use of them!

    Gawd, Heater. Computer languages (aka, programing languages) are primarily for machines to interpret or compile. Pseudocode is for humans to primarily communicate programing concepts with each other. Just because two people may discuss a program (in their native 'real' language perhaps), doesn't make the topic of their discussion a real language in any sense. At best, you are referring to a programming language being a semiotic system.

    I don't see how you can assert that a language that directs a computer to operate in a certain manner is a 'real' language. Try some formal study in linguistics. It is all about human-to-human communication. Semiotic is all about symbolic representations.
  • TorTor Posts: 2,010
    edited 2015-03-31 11:59
    Programming languages describe algorithms, human languages are used for communication. Granted, a) the mechanisms of languages in general (syntax, semantics) are re-used for this, because we know it, and b) describing an algorithm is also at the same time 'communicating' it because if you and others can write it then you and others can read it. But programming languages are still covering only a tiny tiny part of all communication space, unlike human languages. And as I mentioned the last time this came up, learning a programming language is a process totally unlike learning a human language - I know many programming languages and speak a few human ones and I'm still learning new languages (programming and human). Totally different. Except for the funny fact that if you are a long-time programmer then the grammar and logic of Japanese makes complete and utter sense.. but that's still not the same.
Sign In or Register to comment.