Clojure recursive function gives null-pointer exception [duplicate]












0















This question already has an answer here:




  • Executing multiple statements in if-else without nullpointer exception

    1 answer




When defining a simple recursive function with fn, as follows,



((fn myfunc [x] (if (zero? x) (println "STOP") ((println x) (myfunc (dec x))))) 6)


it prints 6,5,4,3,2,1,STOP and then I get a null-pointer exception in the REPL. Is this not legal syntax?



I know I can define functions with def, and there (should) not be any issue with recursion there. I am simply curious about the exception.










share|improve this question













marked as duplicate by amalloy clojure
Users with the  clojure badge can single-handedly close clojure questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 at 23:35


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















    0















    This question already has an answer here:




    • Executing multiple statements in if-else without nullpointer exception

      1 answer




    When defining a simple recursive function with fn, as follows,



    ((fn myfunc [x] (if (zero? x) (println "STOP") ((println x) (myfunc (dec x))))) 6)


    it prints 6,5,4,3,2,1,STOP and then I get a null-pointer exception in the REPL. Is this not legal syntax?



    I know I can define functions with def, and there (should) not be any issue with recursion there. I am simply curious about the exception.










    share|improve this question













    marked as duplicate by amalloy clojure
    Users with the  clojure badge can single-handedly close clojure questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 15 at 23:35


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















      0












      0








      0


      1






      This question already has an answer here:




      • Executing multiple statements in if-else without nullpointer exception

        1 answer




      When defining a simple recursive function with fn, as follows,



      ((fn myfunc [x] (if (zero? x) (println "STOP") ((println x) (myfunc (dec x))))) 6)


      it prints 6,5,4,3,2,1,STOP and then I get a null-pointer exception in the REPL. Is this not legal syntax?



      I know I can define functions with def, and there (should) not be any issue with recursion there. I am simply curious about the exception.










      share|improve this question














      This question already has an answer here:




      • Executing multiple statements in if-else without nullpointer exception

        1 answer




      When defining a simple recursive function with fn, as follows,



      ((fn myfunc [x] (if (zero? x) (println "STOP") ((println x) (myfunc (dec x))))) 6)


      it prints 6,5,4,3,2,1,STOP and then I get a null-pointer exception in the REPL. Is this not legal syntax?



      I know I can define functions with def, and there (should) not be any issue with recursion there. I am simply curious about the exception.





      This question already has an answer here:




      • Executing multiple statements in if-else without nullpointer exception

        1 answer








      recursion clojure






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 at 22:21









      Per Alexandersson

      1,3401224




      1,3401224




      marked as duplicate by amalloy clojure
      Users with the  clojure badge can single-handedly close clojure questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 15 at 23:35


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by amalloy clojure
      Users with the  clojure badge can single-handedly close clojure questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 15 at 23:35


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          2














          In lisps, using parenthesis means "treat the first argument in the list as a function, and invoke it with the rest of the arguments in the list".



          In your case, you have ((println x) (myfunc (dec x))). The first (println x) gets invoked, returning a nil, to get (nil (myfunc (dec x))), and then nil is attempting to get invoked as a function with the result of the recursion. This is what causing the NPE exception to be thrown.



          If you want to have multiple expressions in clojure, you could use the do macro, so the same code would look like: (do (println x) (myfunc (dec x))).



          do essentially runs all the expressions for their side-effects, returning only the result of the last expression.






          share|improve this answer





















          • Ah, that makes sense, so the error were at a completely different place than i expected.
            – Per Alexandersson
            Nov 16 at 0:15


















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          In lisps, using parenthesis means "treat the first argument in the list as a function, and invoke it with the rest of the arguments in the list".



          In your case, you have ((println x) (myfunc (dec x))). The first (println x) gets invoked, returning a nil, to get (nil (myfunc (dec x))), and then nil is attempting to get invoked as a function with the result of the recursion. This is what causing the NPE exception to be thrown.



          If you want to have multiple expressions in clojure, you could use the do macro, so the same code would look like: (do (println x) (myfunc (dec x))).



          do essentially runs all the expressions for their side-effects, returning only the result of the last expression.






          share|improve this answer





















          • Ah, that makes sense, so the error were at a completely different place than i expected.
            – Per Alexandersson
            Nov 16 at 0:15
















          2














          In lisps, using parenthesis means "treat the first argument in the list as a function, and invoke it with the rest of the arguments in the list".



          In your case, you have ((println x) (myfunc (dec x))). The first (println x) gets invoked, returning a nil, to get (nil (myfunc (dec x))), and then nil is attempting to get invoked as a function with the result of the recursion. This is what causing the NPE exception to be thrown.



          If you want to have multiple expressions in clojure, you could use the do macro, so the same code would look like: (do (println x) (myfunc (dec x))).



          do essentially runs all the expressions for their side-effects, returning only the result of the last expression.






          share|improve this answer





















          • Ah, that makes sense, so the error were at a completely different place than i expected.
            – Per Alexandersson
            Nov 16 at 0:15














          2












          2








          2






          In lisps, using parenthesis means "treat the first argument in the list as a function, and invoke it with the rest of the arguments in the list".



          In your case, you have ((println x) (myfunc (dec x))). The first (println x) gets invoked, returning a nil, to get (nil (myfunc (dec x))), and then nil is attempting to get invoked as a function with the result of the recursion. This is what causing the NPE exception to be thrown.



          If you want to have multiple expressions in clojure, you could use the do macro, so the same code would look like: (do (println x) (myfunc (dec x))).



          do essentially runs all the expressions for their side-effects, returning only the result of the last expression.






          share|improve this answer












          In lisps, using parenthesis means "treat the first argument in the list as a function, and invoke it with the rest of the arguments in the list".



          In your case, you have ((println x) (myfunc (dec x))). The first (println x) gets invoked, returning a nil, to get (nil (myfunc (dec x))), and then nil is attempting to get invoked as a function with the result of the recursion. This is what causing the NPE exception to be thrown.



          If you want to have multiple expressions in clojure, you could use the do macro, so the same code would look like: (do (println x) (myfunc (dec x))).



          do essentially runs all the expressions for their side-effects, returning only the result of the last expression.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 at 22:37









          Shlomi

          4,16911728




          4,16911728












          • Ah, that makes sense, so the error were at a completely different place than i expected.
            – Per Alexandersson
            Nov 16 at 0:15


















          • Ah, that makes sense, so the error were at a completely different place than i expected.
            – Per Alexandersson
            Nov 16 at 0:15
















          Ah, that makes sense, so the error were at a completely different place than i expected.
          – Per Alexandersson
          Nov 16 at 0:15




          Ah, that makes sense, so the error were at a completely different place than i expected.
          – Per Alexandersson
          Nov 16 at 0:15



          Popular posts from this blog

          How to change which sound is reproduced for terminal bell?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Can I use Tabulator js library in my java Spring + Thymeleaf project?