0

I am trying to make a nested function that should be run recursively in Clojure in order to collect some information. I have seen some techniques such as fn, loop, doseq but I am not good with them for now.

First let me explain why I want to use a recursive funtion: I have a class and this class include 2 methods itself, these methods are namely getLeft() and getRight(), plus the return type of the methods are also a class. I do want simply traverse all getLeft and getRight methods in order to get the proper number from them.

Just to be clear, I put a schema:

My Main Class:

                     +-- getRight(): class  - getLeft/getRight
++ getLeft(): class ---  
                     +-- getLeft((): class  - getLeft/getRight
                     +-- getRight(): class  - getLeft/getRight
++ getRight() :class
                     +-- getLeft((): class  - getLeft/getRight

I also made a function called "loopingP" but when I add this func to my another function, it gives an error. Error: NullPointerException org.mtrcclojure.demo/loopingP (NO_SOURCE_FILE:4)

     (defn loopingP [points]
       (if (= (getKind points) 5)
        ( (loopingP (getRight points)) (loopingP (getLeft points)))
       (if (= (getKind points) 3) (println "Yep"))))

How can I properly use a nested function in Clojure for my purpose? Thanks in advance!

Solution is: putting a []

(defn loopingP [points]
       (if (= (getKind points) 5)
        [ (loopingP (getRight points)) (loopingP (getLeft points))]
       (if (= (getKind points) 3) (println "Yep"))))
2
  • What is points in your example? Commented Nov 20, 2013 at 17:39
  • points means a poincut class that is included in ajdt Commented Nov 20, 2013 at 17:42

1 Answer 1

1

You have an extraneous set of parenthesis around your recursion case - perhaps you meant to group the getRight and getLeft calls into a pair? the base case always returns nil (either from the false branch of the if, or from the println) so the leftmost recursion gets applied, and thus you call nil as a function (leading to a NullPointerException).

Sign up to request clarification or add additional context in comments.

5 Comments

if I eliminate one nested call from the loopingP , the function receives me a string "Yep". But How can I use two nested call for left and right?
If you were trying to build a tuple with two elements, you can use the vector literal [(loopingP (getRight points)) (loopingP (getLeft points))].
yes exactly I did like your answer @juan.facorro and I have received now this output : [[5 _0] nil] nil]. how can I split them?
I don't exactly know what you mean by splitting them. What is the final result you hope to get?
Onw way to do it is flattening the result and then filtering non nil elements: (->> points loopingP flatten (filter identity)).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.