📜 ⬆️ ⬇️

You can write code without variables

Very often, after phrases in the spirit: “Well, there are no variables in this language, and there are no problems with ...” or similar ones, I am surprised to notice complete surprise in the eyes of the interlocutor. This is because many programmers cannot imagine a program without such a thing as a variable. An example of such a code will be translated below to Plain C.

This code performs the following task, it receives at the input an array of 4 integers, and determines whether they can be 4 sides of a rectangular trapezium.

This code in no way claims either for efficiency or expressiveness and should be considered only as an example of the fact that this is possible. This is due to the fact that neither the language nor the task contribute to this style of programming. In spite of this, we can single out one strong side of this code, these are well-defined abstractions.

For more information about variables, and, more generally, the changing states, you can read the article “Changeable state: hazards and control of them” by Yevgeny Kirpichev in the first issue of the journal Practice of Functional Programming at this link:
fprog.ru/2009/issue1/eugene-kirpichev-fighting-mutable-state
')
#include <stdio.h>

int find_dublicate( const int *lines)
{
return find_dublicate2(lines, 0, 0);
}

int find_dublicate2( const int *lines, const int i)
{
const int result = find_dublicate3(lines, i, i + 1);
if (result == -1)
if (i < 3) return find_dublicate2(lines, i + 1);
else return -1;
else return result;
}

int find_dublicate3( const int *lines,
const int i,
const int j)
{
if (lines[i] == lines[j])
return lines[j];
else
if (j < 4) return find_dublicate3(lines, i, j + 1);
else return -1;
}

int delete_duplicate( const int *lines, const int a, const int number)
{
return delete_duplicate2(lines, a, 0, 2, number);
}

int delete_duplicate2( const int *lines, const int a,
const int i, const int j, const int number)
{
if (lines[i] == a && j != 0)
delete_duplicate2(lines, a, i + 1, j - 1, number);
else if (number != 0)
delete_duplicate2(lines, a, i + 1, j, number - 1);
else return lines[i];
}


int max_AB( const int *AB)
{
if (AB[0] > AB[1]) return 0;
else return 1;
}

int main ( int argc, char ** argv)
{
const int lines[] = {2,3,8,2};
const int a = find_dublicate(lines);
if (a == -1) goto no;
const int AB[2] = {delete_duplicate(lines, a, 0), delete_duplicate(lines, a, 1)};

const int max = max_AB(AB);

const int delta = AB[max] - AB[max ? 0 : 1];
if (delta/2 < a) goto yes;
else goto no;

yes:
printf ( "yes\n" );
return 0;
no:
printf ( "no\n" );
return 0;

}

* This source code was highlighted with Source Code Highlighter .
#include <stdio.h>

int find_dublicate( const int *lines)
{
return find_dublicate2(lines, 0, 0);
}

int find_dublicate2( const int *lines, const int i)
{
const int result = find_dublicate3(lines, i, i + 1);
if (result == -1)
if (i < 3) return find_dublicate2(lines, i + 1);
else return -1;
else return result;
}

int find_dublicate3( const int *lines,
const int i,
const int j)
{
if (lines[i] == lines[j])
return lines[j];
else
if (j < 4) return find_dublicate3(lines, i, j + 1);
else return -1;
}

int delete_duplicate( const int *lines, const int a, const int number)
{
return delete_duplicate2(lines, a, 0, 2, number);
}

int delete_duplicate2( const int *lines, const int a,
const int i, const int j, const int number)
{
if (lines[i] == a && j != 0)
delete_duplicate2(lines, a, i + 1, j - 1, number);
else if (number != 0)
delete_duplicate2(lines, a, i + 1, j, number - 1);
else return lines[i];
}


int max_AB( const int *AB)
{
if (AB[0] > AB[1]) return 0;
else return 1;
}

int main ( int argc, char ** argv)
{
const int lines[] = {2,3,8,2};
const int a = find_dublicate(lines);
if (a == -1) goto no;
const int AB[2] = {delete_duplicate(lines, a, 0), delete_duplicate(lines, a, 1)};

const int max = max_AB(AB);

const int delta = AB[max] - AB[max ? 0 : 1];
if (delta/2 < a) goto yes;
else goto no;

yes:
printf ( "yes\n" );
return 0;
no:
printf ( "no\n" );
return 0;

}

* This source code was highlighted with Source Code Highlighter .
#include <stdio.h>

int find_dublicate( const int *lines)
{
return find_dublicate2(lines, 0, 0);
}

int find_dublicate2( const int *lines, const int i)
{
const int result = find_dublicate3(lines, i, i + 1);
if (result == -1)
if (i < 3) return find_dublicate2(lines, i + 1);
else return -1;
else return result;
}

int find_dublicate3( const int *lines,
const int i,
const int j)
{
if (lines[i] == lines[j])
return lines[j];
else
if (j < 4) return find_dublicate3(lines, i, j + 1);
else return -1;
}

int delete_duplicate( const int *lines, const int a, const int number)
{
return delete_duplicate2(lines, a, 0, 2, number);
}

int delete_duplicate2( const int *lines, const int a,
const int i, const int j, const int number)
{
if (lines[i] == a && j != 0)
delete_duplicate2(lines, a, i + 1, j - 1, number);
else if (number != 0)
delete_duplicate2(lines, a, i + 1, j, number - 1);
else return lines[i];
}


int max_AB( const int *AB)
{
if (AB[0] > AB[1]) return 0;
else return 1;
}

int main ( int argc, char ** argv)
{
const int lines[] = {2,3,8,2};
const int a = find_dublicate(lines);
if (a == -1) goto no;
const int AB[2] = {delete_duplicate(lines, a, 0), delete_duplicate(lines, a, 1)};

const int max = max_AB(AB);

const int delta = AB[max] - AB[max ? 0 : 1];
if (delta/2 < a) goto yes;
else goto no;

yes:
printf ( "yes\n" );
return 0;
no:
printf ( "no\n" );
return 0;

}

* This source code was highlighted with Source Code Highlighter .


PS The code seems to work correctly, although it wasn’t normally cracked. Compile gcc-4.2.1
PPS In the lead of some misunderstanding, the variable was understood as objects with a changeable state, and not named memory sections.

Source: https://habr.com/ru/post/72063/


All Articles