page_title: Flow-based programming
section_title: "Telegram Problem"
hierarchical_section_title: Flow-based programming / Examples / "Telegram Problem"
caption_attribution_description: English: Peter Naur's "Telegram problem"
context_page_description: In computer programming, flow-based programming is a programming paradigm that defines applications as networks of "black box" processes, which exchange data across predefined connections by message passing, where the connections are specified externally to the processes. These black box processes can be reconnected endlessly to form different applications without having to be changed internally. FBP is thus naturally component-oriented.
  FBP is a particular form of dataflow programming based on bounded buffers, information packets with defined lifetimes, named ports, and separate definition of connections.
context_section_description: FBP components often form complementary pairs.  This example uses two such pairs.  The problem described seems very simple as described in words, but in fact is surprisingly difficult to accomplish using conventional procedural logic.  The task, called the "Telegram Problem", originally described by Peter Naur, is to write a program which accepts lines of text and generates output lines containing as many words as possible, where the number of characters in each line does not exceed a certain length. The words may not be split and we assume no word is longer than the size of the output lines. This is analogous to the word-wrapping problem in text editors.
  In conventional logic, the programmer rapidly discovers that neither the input nor the output structures can be used to drive the call hierarchy of control flow. In FBP, on the other hand, the problem description itself suggests a solution:
  "words" are mentioned explicitly in the description of the problem, so it is reasonable for the designer to treat words as information packets (IPs)
  in FBP there is no single call hierarchy, so the programmer is not tempted to force a sub-pattern of the solution to be the top level.
  Here is the most natural solution in FBP (there is no single "correct" solution in FBP, but this seems like a natural fit):
  where DC and RC stand for "DeCompose" and "ReCompose", respectively.
  As mentioned above, Initial Information Packets (IIPs) can be used to specify parametric information such as the desired output record length (required by the rightmost two components), or file names.  IIPs are data chunks associated with a port in the network definition which become "normal" IPs when a "receive" is issued for the relevant port.
caption: Peter Naur's "Telegram problem"
exif: {}