Safekipedia

Refal

Adapted from Wikipedia Β· Discoverer experience

Refal, short for "Recursive functions algorithmic language," is a special kind of programming language designed for working with symbols and ideas rather than just numbers. It was first thought of in 1966 and created in 1968, making it one of the oldest languages of its type. Refal was made to be both easy to understand mathematically and useful for writing big, complicated programs.

Unlike many other languages from its time, Refal uses something called pattern matching to solve problems. This means it can look for certain patterns in information and change them in specific ways. Refal works well with tree-shaped data, which is different from the straight-line lists used in other languages like Lisp and Prolog. This tree structure makes it easier to build and work with complex sets of data.

Refal also has a special feature called the freezer that helps it work faster by saving parts of calculations for later use. Because of these qualities, Refal is good at tasks like changing and organizing information in tree shapes, similar to how XSLT is used today.

Basics

A Refal Hello World example is shown below.

$ENTRY Go { = ;} Hello { = ; }

The program above includes two functions named Go and Hello. A function is written as the name of the function followed by the function body in curly braces. The Go function is marked as the entry point of the program using the $ENTRY directive.

Expressions in the function bodies work in a Lisp-like way. For example, the Hello function seems to use a built-in function called Prout with the words 'Hello world' as an input. But the way it works is different. To show this, consider a function that checks if a string is a palindrome.

Pal { = True; s.1 = True; s.1 e.2 s.1 = ; e.1 = False; }

This example shows a function with four parts, called sentences. Each sentence starts with a pattern, followed by an equal sign, and then an expression. Patterns can include special variables. Variables that start with "s" match a single symbol, and those that start with "e" match any expression. When a function runs, it checks its input against the patterns in order. When it finds a match, it changes the input using the expression next to that pattern.

The function Pal can be understood as: "If the input is empty, change it to True. If the input is one symbol, change it to True. If the input starts and ends with the same symbol, remove those symbols and check again. Otherwise, change it to False."

Other examples

Refal is a special kind of computer language that helps with solving problems by using simple rules.

Factorial

In Refal, you can write a rule to calculate the factorial of a number. For example, the rule says that the factorial of 0 is 1. For any other number, it multiplies the number by the factorial of one less than that number.

Factorial with loops

You can also use loops in Refal to calculate factorials. The number acts as a counter in the loop.

Equality

Refal can check if two things are the same. If the two things are identical, it returns "True." If they are different, it returns "False."

If

It’s easy to create rules for making choices in Refal. For example, an "If" rule can choose one result when something is true and another when it is false.

Squeeze blanks

Refal can also clean up text by removing extra spaces. One rule finds two spaces next to each other and replaces them with a single space. If there are no extra spaces, it just returns the text as it is.

Squeeze using explicit looping

Another way to remove extra spaces uses a loop. This rule checks for two spaces and removes them, continuing until there are no more extra spaces left.

Related articles

This article is a child-friendly adaptation of the Wikipedia article on Refal, available under CC BY-SA 4.0.