Friday, November 23, 2012

Trees/leaves, Harvest, Flatten, Depth, Apply and map functions

After the Tutorial earlier this week, I was able to figure out what each functions within the nested.rkt. So
I will briefly explain the functions covered in nested.rkt and if there are any questions, feel free to leave and comment on this post and I will get back to them as soon as possible.

Trees and leaves
They first define a tree and leaves:

Tree = A structure that has values left and right from the root. We can treat this like an organizational tree that we often see when trying to organize small to big things.

i.e. (define tree1 (make-gtree "AAA" leaf1 leaf2)) will give:

Tree1
leaf1 leaf2

Leaf = Definition for input that has empty left and right terms
i.e. (define leaf1 (make-gtree "ACTG" empty empty))

ACTG
Empty Empty

Harvest
Harvest function will grab all of the 'trees' and 'leaves' together and creates a list from it.
i.e. Given that 
(define tree1 (make-gtree "AAA" leaf1 leaf2))
(define leaf1 (make-gtree "ACTG" empty empty))
(define leaf2 (make-gtree "GTCA" empty empty))

Thus if one provides input of 'harvest tree1', one should have an output of (list "AAA" "ACTG" "GTCA")
If one were to plot it into a diagram, it would look something like this:

AAA
ACTG GTCA
  Empty Empty Empty Empty

Flatten
Next function that's mentioned is flatten. Which grabs all the list that is within a big list, and combines all of them to create a one big list that contains all the variables within the lists.

Flatten is defined as:
(define (flatten L)
  (cond
    [(not (list? L)) (list L)]
    [else (apply append (map flatten L))]))


So, if we have a list as :
(define L1 (list 1 2 3 (list 4 5 6) 7 8 (list 9 1 (list 2 3))))

and we apply flatten function to it, one would get :
(list 1 2 3 4 5 6 7 8 9 1 2 3))


Depth
And finally, depth function will find how many lists are found within a given input.

Depth is defined as:
(define (depth L)
  (cond
    [(not (list? L)) 0]
    [else (add1 (apply max (map depth L)))]))


So if we have a list as:
(list 1 (list 2) 3))


and we apply depth function to it, one would get an output of:
2

This is because there are two lists within the given function.

Map vs Apply
Although it is not directly mentioned within the nested.rkt, there are 2 other important functions that must be discussed about. They are the map and apply function.

Map will apply the given operation to a given list and provide an output of a list that contains all the variables that the operation was applied to. 

For example,
(map add1 (list 1 2 3 4)) would give an output of (list 2 3 4 5) because all the variables within the list were added by 1.

Apply on the other hand is a little different. First of all, it provides a numerical output and not a list even thought the input was a list to begin with. The apply function will use the given operation and treat like all the variables within a given list as part of same equation to apply the operation with.

For example,
(apply + (list 1 2 3 4)) would give an output of (10) since 1+2+3+4=10.


                         

No comments:

Post a Comment