Clojure: NullPointerException at recursive function

Viewed 34

I'm trying to make a recursive function at Clojure, and something strange is happening: when I test directly a base case to the function it works fine, but when I test the recursive case, I'm getting a NullPointerException. I've searched a lot at the web and in StackOverflow, and I've also been trying a lot of things to find out the problem, but I couldn't make it even after hours and hours. So then, this is my code:

(defn imprimir 
    ([elem]
        (if (not (sequential? elem)) (if (= elem \space) (prn) (println elem) ))
        (if (not (sequential? elem)) (flush))
        (if (not (sequential? elem)) elem)
    )
    
    ([lis orig]
        ;(if (empty? lis) ((prn)(flush)) ((println "Entra al else") (prn lis) (print \space) (imprimir (rest lis) orig)) ) 
        (if (empty? lis) (println lis))
        (if (empty? lis) (flush) ((println "Entra al else") (prn lis)  (imprimir (rest lis) orig)))
        ;(if (empty? lis) (println "Entro al if") (println "Entro al else"))
        (if (empty? lis) (println "Caso base" ))
        (if (empty? lis) orig )
        ;orig
    )
)
 
;(println(imprimir nil "saraza"))
;(println(imprimir 5))
;(println(imprimir '(1 2 3 4 5) "saraza"))
(println(imprimir '() "saraza"))
(println(imprimir '(1 2 3 4 5) "saraza"))
;(println(imprimir '(1) "saraza"))

So there, this is my output:

()
Caso base
saraza
Entra al else
(1 2 3 4 5)
Entra al else
(2 3 4 5)
Entra al else
(3 4 5)
Entra al else
(4 5)
Entra al else
(5)
()
Caso base
Syntax error (NullPointerException) compiling at (C:\Users\54113\Documents\Programacion 3\TP Clojure\imprimir_v3.clj:23:1).
    null

Full report at:
C:\Users\54113\AppData\Local\Temp\clojure-67199383983303661.edn

This is supposed to return, and after that print, the string "saraza", after displaying the list with one less element in every recursive call. I mean, the output of the line "(println(imprimir '(1 2 3 4 5) "saraza"))" should be (without the debug prints) :

(1 2 3 4 5)
(2 3 4 5)
(3 4 5)
(4 5)
(5)
saraza

Does anyone have an idea of what's going wrong?

0 Answers
Related