|
The V programming language is a purely functional programming language based on composition of functions rather than lambda calculus [1]. It was inspired by Joy but is closer in spirit to PostScript.
FeaturesConcatenativeThe language is concatenative, meaning that it is based on composition of functions and does not have a need for formal parameters. It is also stack based with postfix notation. Every thing is a function (also called a word) in this language. Each word takes its arguments from the stack and places its output(s) in the stack. The literals (strings and numbers) are functions that consume nothing but place themselves in the stack. Arithmetic2 3 * 4 + will compute (2 * 3) + 4 2 dup * will compute 2 * 2 (dup is a word that duplicates the top most word in the stack) HomoiconicThe primary data structure is the Quote, represented by '[' and ']' It is also used to represent the code. DefinitionsUnlike Joy which enforces static word definitions, V allows definition of words during runtime using the word '.' It is similar to PostScript in this respect. Defining the word square in vThe word '.' pops the last quote from the stack, splits it into a pair containing name and definition, and associates the name with the definition. [square dup *]. QuickSortThe Quick sort is defined as below. Notice how the input to qsort is in the same form as the definition of qsort itself.
[qsort
[joinparts [pivot [*list1] [*list2] : [*list1 pivot *list2]] view].
[split_on_first_element uncons [>] split&].
[small?] []
[split_on_first_element] [joinparts]
binrec].
Using Qsort: [0 9 6 7 8 4 6 2] qsort ExplanationThe first two lines (joinparts and split_on_first_element) are local word definitions. They are used later inside binrec. (The joinparts uses the stack shuffling view mentioned below). joinpartsThe word joinparts takes a pivot element and two lists and joins them together into a single list with the pivot in the middle and the elements of two original lists in the sides. split_on_first_elementthis method takes a list of numbers (a quote) as the parameters, and pushes back 3 values into the stack. The first value is the first element of list (pivot), second value is a list of elements lesser than this value and third value is a a list containing elements greater than the pivot. Eg: [5 2 3 6 7 8 9] uncons [>] split& will output 5 [2 3] [6 7 8 9] binrecbinrec expects 4 quotes on top of the stack. The first is termination condition, second is the quote to be evaluated if the termination condition is met, the third quote recursively takes the list on the stack apart, and its output is assembled back by the forth quote. binrec is one of the high level combinators (others are linrec, genrec, tailrec and primrec) that was inherited from Joy and gives the concatenative programming its unique flavour. binrec translates the above as shown below. Notice how the qsort is applied recursively to list1 and list2 (output of split_on_first_element). The result of these and the pivot that was left in stack by split_on_first_element is joined together by joinparts. [qsort [joinparts [pivot [*list1] [*list2] : [*list1 pivot *list2]] view]. [split_on_first_element uncons [>] split&]. [small?] [] [split_on_first_element [list1 list2 : [list1 qsort list2 qsort joinparts]] view i] ifte]. MacrosV provides the word 'view' used to rearrange the position of values in the stack. Most of the stack manipulation words in V are defined using view Quadratic root in V using the stack manipulating word view [quad-root [a b c : [0 b - b b * 4 a * c * - sqrt + 2 a * /]] view i ]. Exception HandlingV provides a means of handling exceptions using throw and catch. FFI to JavaAn FFI syntax for making use of other libraries loaded in the JVM is also provided in the JVM implementation of V. [java.util.Date new] java puts will print {Tue Mar 13 19:59:22 IST 2007} External links |
This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License.
Mercedes Car
This site monitored by SitePinger.net