Clojure recursive function gives null-pointer exception [duplicate]
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.
recursion clojure
marked as duplicate by amalloy
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.
add a comment |
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.
recursion clojure
marked as duplicate by amalloy
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.
add a comment |
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.
recursion clojure
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
recursion clojure
asked Nov 15 at 22:21
Per Alexandersson
1,3401224
1,3401224
marked as duplicate by amalloy
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
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.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
Ah, that makes sense, so the error were at a completely different place than i expected.
– Per Alexandersson
Nov 16 at 0:15
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Ah, that makes sense, so the error were at a completely different place than i expected.
– Per Alexandersson
Nov 16 at 0:15
add a comment |
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.
Ah, that makes sense, so the error were at a completely different place than i expected.
– Per Alexandersson
Nov 16 at 0:15
add a comment |
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.
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.
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
add a comment |
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
add a comment |