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!










share|improve this question
























  • 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















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!










share|improve this question
























  • 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













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!










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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


















  • 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
















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












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.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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
































    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.






    share|improve this answer

























      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.






      share|improve this answer























        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 days ago









        glennsl

        8,674102545




        8,674102545






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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




















































































            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine