Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<--- and <-> in pseudocode

Tags:

pseudocode

I am not from cs background and I am trying to make sense of what is used for what. In pseudocode I see a lot of this:

for i <---  1 to n-1 do
j <--- find-Min(A,i,n)
A[j] <-> A[i]
end for 

What are <--- and <-> used to refer to?

like image 732
Asim Zaidi Avatar asked May 25 '12 16:05

Asim Zaidi


3 Answers

<--- means "assign the right-hand side to the left-hand side" (it is somewhat strange to see this used in the for case, as it might easily have been omitted there).

<-> means "swap". A[j] value is swapped with A[i].

EDIT:

It just occurred to me that the first line might be missing i and should instead read:

for i <---  1 to n-1 do

This becomes a legitimate use case of <--- described above: i is assigned values from 1 to n-1 sequentially, and the loop body (down to end for, which denotes the end of loop) is executed for each of these i values.

like image 145
Alexander Pavlov Avatar answered Oct 30 '22 16:10

Alexander Pavlov


There's nowhere close to universal agreement about the notation used in pseudocode.

In this case I'd guess that <--- means "assign the right side to the left side", and <-> means 'swap the right and left sides."

In the first case, however, I think you're missing a character. It's probably supposed to be:

for i <--- 1 to n-1 do

So it's a normal for loop that would be written as:

for i = 1 to n-1

in BASIC, or:

for (i=1; i<n; i++)

in a C-like language.

like image 41
Jerry Coffin Avatar answered Oct 30 '22 17:10

Jerry Coffin


Left arrow for assignment

is used to make it obvious that the variable receives a new value, e.g.

for i <- 1 to n-1 do 

I have seen this in:

  • mainly pseudo code;
  • R, S, Scala and OCaml;
  • with an own left arrow symbol in APL.

Left-right-arrow for swapping elements

is also used to make the operation obvious in a symbolic way.

I can't think of anything but pseudo code right now.

like image 32
DaveFar Avatar answered Oct 30 '22 16:10

DaveFar