If then with multiple expressions OCaml
up vote
0
down vote
favorite
How can I code, in OCaml, something like this (main() in C)
for (i=0; i<y; i++)
if (x==5)
{
y=i;
return true;
}
It would return the boolean True and y would be equal to the corresponding i value.
Basically, in OCaml, I was wondering if you could have a if like:
for i=0 to y-1 do
if x=5 then
begin
y=i
true
end
else ()
done;;
I know the lines between begin-end aren't correctly implemented. I don't even know if what I want to do is possible in OCaml. If you are someone who has knowledge on this kindly share it with me. Thanks!
c for-loop if-statement ocaml
add a comment |
up vote
0
down vote
favorite
How can I code, in OCaml, something like this (main() in C)
for (i=0; i<y; i++)
if (x==5)
{
y=i;
return true;
}
It would return the boolean True and y would be equal to the corresponding i value.
Basically, in OCaml, I was wondering if you could have a if like:
for i=0 to y-1 do
if x=5 then
begin
y=i
true
end
else ()
done;;
I know the lines between begin-end aren't correctly implemented. I don't even know if what I want to do is possible in OCaml. If you are someone who has knowledge on this kindly share it with me. Thanks!
c for-loop if-statement ocaml
Your C code makes no sense.x
doesn't change in the loop, so the condition is either always true or never true. You need to provide a bit more context.
– melpomene
2 days ago
Isy
a local variable in your C code? If so, it is destroyed when the function returns, so the assignment is pointless.
– melpomene
2 days ago
I just want to be able to change a variable value and also return something in the same if-then in OCaml, instead of just returning
– Merlin
2 days ago
Can you show a complete example in C?
– melpomene
2 days ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
How can I code, in OCaml, something like this (main() in C)
for (i=0; i<y; i++)
if (x==5)
{
y=i;
return true;
}
It would return the boolean True and y would be equal to the corresponding i value.
Basically, in OCaml, I was wondering if you could have a if like:
for i=0 to y-1 do
if x=5 then
begin
y=i
true
end
else ()
done;;
I know the lines between begin-end aren't correctly implemented. I don't even know if what I want to do is possible in OCaml. If you are someone who has knowledge on this kindly share it with me. Thanks!
c for-loop if-statement ocaml
How can I code, in OCaml, something like this (main() in C)
for (i=0; i<y; i++)
if (x==5)
{
y=i;
return true;
}
It would return the boolean True and y would be equal to the corresponding i value.
Basically, in OCaml, I was wondering if you could have a if like:
for i=0 to y-1 do
if x=5 then
begin
y=i
true
end
else ()
done;;
I know the lines between begin-end aren't correctly implemented. I don't even know if what I want to do is possible in OCaml. If you are someone who has knowledge on this kindly share it with me. Thanks!
c for-loop if-statement ocaml
c for-loop if-statement ocaml
edited 2 days ago
asked 2 days ago
Merlin
446
446
Your C code makes no sense.x
doesn't change in the loop, so the condition is either always true or never true. You need to provide a bit more context.
– melpomene
2 days ago
Isy
a local variable in your C code? If so, it is destroyed when the function returns, so the assignment is pointless.
– melpomene
2 days ago
I just want to be able to change a variable value and also return something in the same if-then in OCaml, instead of just returning
– Merlin
2 days ago
Can you show a complete example in C?
– melpomene
2 days ago
add a comment |
Your C code makes no sense.x
doesn't change in the loop, so the condition is either always true or never true. You need to provide a bit more context.
– melpomene
2 days ago
Isy
a local variable in your C code? If so, it is destroyed when the function returns, so the assignment is pointless.
– melpomene
2 days ago
I just want to be able to change a variable value and also return something in the same if-then in OCaml, instead of just returning
– Merlin
2 days ago
Can you show a complete example in C?
– melpomene
2 days ago
Your C code makes no sense.
x
doesn't change in the loop, so the condition is either always true or never true. You need to provide a bit more context.– melpomene
2 days ago
Your C code makes no sense.
x
doesn't change in the loop, so the condition is either always true or never true. You need to provide a bit more context.– melpomene
2 days ago
Is
y
a local variable in your C code? If so, it is destroyed when the function returns, so the assignment is pointless.– melpomene
2 days ago
Is
y
a local variable in your C code? If so, it is destroyed when the function returns, so the assignment is pointless.– melpomene
2 days ago
I just want to be able to change a variable value and also return something in the same if-then in OCaml, instead of just returning
– Merlin
2 days ago
I just want to be able to change a variable value and also return something in the same if-then in OCaml, instead of just returning
– Merlin
2 days ago
Can you show a complete example in C?
– melpomene
2 days ago
Can you show a complete example in C?
– melpomene
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
If your question is about early return from a for loop, this is how you'd it:
exception Break of int
let n =
try
for i = 0 to 5 do
if i mod 2 = 0 then
raise (Break i)
done;
0
with
Break i -> i
You could also assign to a ref
cell instead of passing the value in the exception, if that fits your use case better. But this isn't the kind of code you should typically be writing in OCaml. Trying to emulate C in OCaml is almost always a bad idea.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
If your question is about early return from a for loop, this is how you'd it:
exception Break of int
let n =
try
for i = 0 to 5 do
if i mod 2 = 0 then
raise (Break i)
done;
0
with
Break i -> i
You could also assign to a ref
cell instead of passing the value in the exception, if that fits your use case better. But this isn't the kind of code you should typically be writing in OCaml. Trying to emulate C in OCaml is almost always a bad idea.
add a comment |
up vote
0
down vote
accepted
If your question is about early return from a for loop, this is how you'd it:
exception Break of int
let n =
try
for i = 0 to 5 do
if i mod 2 = 0 then
raise (Break i)
done;
0
with
Break i -> i
You could also assign to a ref
cell instead of passing the value in the exception, if that fits your use case better. But this isn't the kind of code you should typically be writing in OCaml. Trying to emulate C in OCaml is almost always a bad idea.
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
If your question is about early return from a for loop, this is how you'd it:
exception Break of int
let n =
try
for i = 0 to 5 do
if i mod 2 = 0 then
raise (Break i)
done;
0
with
Break i -> i
You could also assign to a ref
cell instead of passing the value in the exception, if that fits your use case better. But this isn't the kind of code you should typically be writing in OCaml. Trying to emulate C in OCaml is almost always a bad idea.
If your question is about early return from a for loop, this is how you'd it:
exception Break of int
let n =
try
for i = 0 to 5 do
if i mod 2 = 0 then
raise (Break i)
done;
0
with
Break i -> i
You could also assign to a ref
cell instead of passing the value in the exception, if that fits your use case better. But this isn't the kind of code you should typically be writing in OCaml. Trying to emulate C in OCaml is almost always a bad idea.
answered 2 days ago
glennsl
8,674102545
8,674102545
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239171%2fif-then-with-multiple-expressions-ocaml%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Your C code makes no sense.
x
doesn't change in the loop, so the condition is either always true or never true. You need to provide a bit more context.– melpomene
2 days ago
Is
y
a local variable in your C code? If so, it is destroyed when the function returns, so the assignment is pointless.– melpomene
2 days ago
I just want to be able to change a variable value and also return something in the same if-then in OCaml, instead of just returning
– Merlin
2 days ago
Can you show a complete example in C?
– melpomene
2 days ago