Learning a New Programming Language Part 2: Language Types - Functional Languages
(Page 4 of 4 )
To someone who is used to procedural or object oriented languages, the world of functional programming will seem rather strange at first.
In functional programming everything is a function. Lisp is a popular functional language used in artificial intelligence, some CAD applications and even some Web applications. In Lisp everything is a function. There are only two data types, an atom (a data item, either numeric or symbolic) or a list (a container).
Unlike the minor differences in syntax of various languages from the procedural or object oriented categories, Lisp syntax is quite different, so we will go over it first and then move on to the syntactical differences in various procedural and OO languages.
One variation in syntax between languages can be which character is used for comments in code. In Lisp the comment character is a colon.
Parenthesis are used to surround expressions. An expression to add three numbers in Lisp would use the following syntax.
(+ 1 2 3)
; the result of this would be 6
Assigning a value to a variable is also quite different. The SETF function takes two arguments. The first argument is the name of the variable and the second is the value.
(SETF b 3.25)
; The variable b contains the number 3.25
As in the above example, where we use the SETF function to set the value of a variable, Lisp selectors like "FIRST" are used to retrieve values.
(FIRST (2 3 45))
; This will return 2 from the list
The DEFUN function is used to define functions. As always in Lisp the whole thing is in parenthesis.
(DEFUN myOwnFunction
(argument)
(function code here)
(function code here)
)
Each line in this function is called a form. The final form would be used to return a value from the function. Variables are not isolated from code outside a function unless you use the LET form to establish its scope.
There are also if constructs in LISP but, as you might have guessed, they are functions too.
(IF (EQUAL b a) (SETF c 25) (SETF c 125))
The EQUAL predicate determines whether a and b are both the same type (either an atom or a list). If they are, the form in the first argument is executed; otherwise, the form in the second argument is executed.
For looping in LISP it is encouraged that you employ recursion. Recursion is a technique by which a function calls itself under certain conditions. Recursion is available in many languages and can be more useful than loop constructs in certain situations.
There are a few loop constructs that you can use if you wish. These include DOTIMES, DOLIST, DO and LOOP.
Finally LISP does support a form of associative array or hash using the ASSC function as a constructor.
If this seems to be a little basic, and you're wondering how one could use LISP for complex applications, it is important to note that this is simply an abbreviated explanation of the core LISP language.
As I mentioned before, for interpreted languages such as LISP, features such as access to networks, string manipulation, file handling and other more complex operations are provided by the interpreter in the form of built in functions that extend the language. You would need to find a full featured LISP interpreter in order to use LISP for these applications.
Conclusion
In the next and final part of this series we will examine syntax differences in many of the more popular languages, and look at various development platforms that are available.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |