Saturday, June 16, 2012

List Operations in Fuzuli Programming Language

Fuzuli, our new programming language and interpreter has several internal functions for list operations. Arrays are prominent objects of programming languages. Although many other programming languages use brackets for setting and getting values of array, this operator is not defined in Fuzuli. Lists are similar to Lisp's lists but they are different. As we said before, Fuzuli is neither a Lisp nor an Algol family, but it is something like a combination of them.
Any one dimensional Fuzuli list can be created using the list keyword. This keyword corresponds to internal ListExpression and can take infinite number of parameters to hold. An example for use of list keyword is given below:

(let a (list 12 3 4 5 "Hello" 5 4 2 10 2.13))

In the code above, a is list of elements 12, 3, 4, 5, "Hello", 5, 4, 2, 10, and 2.13, respectively. As we can see, the list a can hold elements from any type.

The keyword nth corresponds to the function nth for accessing elements using their indices. An example for use of nth is given below:

(let element (nth a 4))

The variable element now holds the value "Hello" because it has the indices of 4. Note that the index of first elements is zero. 4th element of list a can be changes as

(set a 4 "Fuzuli")

and variable a contains these elements:

12 3 4 5 "Fuzuli" 5 4 2 10 2.13


Lists can be constructed automatically in an increasing manner from an integer a to integer b. The code shown below is for creating a list from 1 to 1000:

(let biglist (: 1 1000))

 Ok! We borrowed this command from R because it is very nice and easy! Let's get the length of this list:

(let mylen (length biglist))

and mylen carries the value of 1000, the number of elements contained by biglist. One may need to append or prepend elements to lists. For those, we have append and prepend keywords for appending and prepending.

# Creating a list
(let mylist (list 1 2 3 4 5 6))

# Appending 7 to the end of mylist
(append mylist 7)

# Put a zero at the beginning
(prepend mylist 0)

# Print out the list
(print mylist)

The output is

[0, 1, 2, 3, 4, 5, 6, 7]

Well! How about removing elements? Lets remove the "4" from this list:

# Removing 4
(remove mylist 4)

The output is

[0, 1, 2, 3, 5, 6, 7]

There is also a find keyword for determining location of a given element in a list. Look at the code:

(let mylist (list "Jan" "Jun" "Aug" "Sep"))
(let index (find mylist "Jun"))
(print index)


The output is 1 because "Jun" has the index of 1 in array mylist.

There are extra functions in utils.nfl package for list operations. Those functions are not built-in but shipped within Fuzuli. Current utils.nfl package contains shuffle, sorta and sortb functions for mixing, ascending sorting and descending sorting of elements of a given list.

Another important point of Fuzuli lists is multi-dimensionality. We mentioned that Fuzuli lists can contain any type of objects. These object can exactly be an other list! And those list can take lists as their elements and so on... Let's create a 2x2 matrix of elements.

(let matrix
    (list
        (list 1 2)
        (list 3 4)
    )
)

(print matrix)

The output is


[[1, 2], [3, 4]]

The matrix given below has a dimension of 3x5:

(let matrix
    (list
        (list 1 2 3 4 5)
        (list 6 7 8 9 10)
        (list 11 12 13 14 15)
    )
)

(print matrix)

The output is

[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]

Accessing elements of multi-dimensional lists is easy! Follow the code:

(let matrix
    (list
        (list 1 2 3 4 5)
        (list 6 7 8 9 10)
        (list 11 12 13 14 15)
    )
)

(print (nth (nth matrix 0) 0) "\n")
(print (nth (nth matrix 1) 3) "\n")
(print (nth (nth matrix 2) 3) "\n")
(print (nth (nth matrix 2) 4) "\n")


The output is

1
9
14
15


becase matrix[0][0] is 1, matrix[1][3] is 9, matrix[2][3] is 14 and matrix[2][4] is 15 in C or Java notation.

Have fun with Fuzuli lists!







No comments:

Post a Comment

Thanks