What is the “return” in scheme?











up vote
2
down vote

favorite












I am trying to translate some python code to scheme.



def test(x):
if x > 1:
do something
if x > 10:
return
if x == 4:
do something
test(11)


I did not find the "return" in scheme. I want to quit from the test function when x >10.
How can I simulate the "return" in scheme?
(I am using guile)
Thanks










share|improve this question
























  • So in your example do something does something that result in a side effect? Perhaps if you said what the function was suppose to do we could make a more idiomatic Scheme version for you to look at. The answers so far try to mimic you code and is pretty bad Scheme.
    – Sylwester
    Apr 28 '14 at 22:41















up vote
2
down vote

favorite












I am trying to translate some python code to scheme.



def test(x):
if x > 1:
do something
if x > 10:
return
if x == 4:
do something
test(11)


I did not find the "return" in scheme. I want to quit from the test function when x >10.
How can I simulate the "return" in scheme?
(I am using guile)
Thanks










share|improve this question
























  • So in your example do something does something that result in a side effect? Perhaps if you said what the function was suppose to do we could make a more idiomatic Scheme version for you to look at. The answers so far try to mimic you code and is pretty bad Scheme.
    – Sylwester
    Apr 28 '14 at 22:41













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I am trying to translate some python code to scheme.



def test(x):
if x > 1:
do something
if x > 10:
return
if x == 4:
do something
test(11)


I did not find the "return" in scheme. I want to quit from the test function when x >10.
How can I simulate the "return" in scheme?
(I am using guile)
Thanks










share|improve this question















I am trying to translate some python code to scheme.



def test(x):
if x > 1:
do something
if x > 10:
return
if x == 4:
do something
test(11)


I did not find the "return" in scheme. I want to quit from the test function when x >10.
How can I simulate the "return" in scheme?
(I am using guile)
Thanks







scheme






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 27 '14 at 19:31

























asked Apr 27 '14 at 19:24









scot

1613




1613












  • So in your example do something does something that result in a side effect? Perhaps if you said what the function was suppose to do we could make a more idiomatic Scheme version for you to look at. The answers so far try to mimic you code and is pretty bad Scheme.
    – Sylwester
    Apr 28 '14 at 22:41


















  • So in your example do something does something that result in a side effect? Perhaps if you said what the function was suppose to do we could make a more idiomatic Scheme version for you to look at. The answers so far try to mimic you code and is pretty bad Scheme.
    – Sylwester
    Apr 28 '14 at 22:41
















So in your example do something does something that result in a side effect? Perhaps if you said what the function was suppose to do we could make a more idiomatic Scheme version for you to look at. The answers so far try to mimic you code and is pretty bad Scheme.
– Sylwester
Apr 28 '14 at 22:41




So in your example do something does something that result in a side effect? Perhaps if you said what the function was suppose to do we could make a more idiomatic Scheme version for you to look at. The answers so far try to mimic you code and is pretty bad Scheme.
– Sylwester
Apr 28 '14 at 22:41












5 Answers
5






active

oldest

votes

















up vote
2
down vote













In Scheme there isn't an explicit return keyword - it's a lot simpler than that, the value of the last expression in a sequence of expressions is the one that gets returned. For example, your Python code will translate to this, and notice that the (> x 10) case had to be moved to the bottom, so if it's true it can exit the function immediately with a #f value.



(define (test x)
(if (> x 1)
(do-something))
(if (= x 4)
(do-something))
(if (> x 10)
#f))

(test 11)
=> #f


In fact, after reordering the conditions we can remove the last one, but beware: an unspecified value will be returned if x is not 4, according to Guile's documentation - in other words, you should always return a value in each case, and an if expression should have both consequent and alternative parts.



(define (test x)
(if (> x 1)
(do-something))
(if (= x 4)
(do-something)))

(test 11)
=> unspecified


And by the way, I believe the logic in the Python code is a bit off. The first condition will always be evaluated whenever a value of x greater than 1 is passed, but if it is less than or equal to 1 the returned value in Python will be None and in Scheme is unspecified. Also the original function isn't explicitly returning a value - in Python this means that None will be returned, in Scheme the returned value will be either (do-something) if x happens to be 4, or unspecified in any other case.






share|improve this answer























  • This isn't really a faithful translation of the python code, which would have performed the actions for part 1 and 2.
    – amalloy
    Apr 27 '14 at 23:42










  • @amalloy you're right, I updated my answer with a direct translation
    – Óscar López
    Apr 28 '14 at 0:25






  • 1




    A more faithful translation would be (define (test x) (if (> x 1) (do-something)) (if (not (> x 10)) (if (= x 4) (do-something)))), since if (x) return; {block} is equivalent to if (!x) {block}.
    – Adam Rosenfield
    Apr 28 '14 at 0:29










  • @amalloy: Well, you could use call/cc here for an escape continuation :)
    – leppie
    Apr 28 '14 at 5:01


















up vote
2
down vote













In Racket the most literal translation is:



#lang racket    
(define (test x)
(let/ec return
(when (> x 1)
(do-something))
(when (> x 10)
(return 42))
(when (= x 4)
(do-something))))

(define (do-something)
(display "!"))
(test 11)


The let/ec is short for let/escape-continuation. Look up the equivalent control structure in the manual for your Scheme implementation of choice.



The example displays one ! and then returns 42.






share|improve this answer





















  • It is a Scheme question, not a Racket question. How difficult is it to replace let/ec with call/cc to help the OP directly?
    – GoZoner
    Apr 28 '14 at 22:28










  • As far as I knwow Guile also has let/ec.
    – soegaard
    Apr 29 '14 at 16:21


















up vote
1
down vote













The implicit return of Scheme can be illustrated by comparing how you can implement a simple function, such as square, in Python and scheme.



In Python:



def square(x):
return x*x;


In Scheme:



(define (square x)
(* x x))





share|improve this answer




























    up vote
    1
    down vote













    As others have said, the last expression's value in a function is its return value, so you just have to arrange for exclusive execution pathways in your code, to achieve this effect.



    (if <test> <consequent> <alternative>) is the basic branching operation in Scheme:



    (define (test x)
    (if (> x 1)
    (do_something)
    #f)
    (if (> x 10)
    #f ; return #f
    ;; else:
    (if (= x 4)
    (do_something)
    ;; else:
    #f)))

    (test 11)


    Or we could use cond to avoid the needlessly nested structure in the code:



    (define (test x)
    (if (> x 1)
    (do_something)
    #f)
    (cond
    ( (> x 10) #f)
    ( (= x 4) (do_something))
    ( else #f)))





    share|improve this answer




























      up vote
      0
      down vote













      You may use call/cc



      (define (test x) (call/cc (lambda (k)
      (if x
      (k x)
      (k))
      (display "never displayed"))))




      > (test 3)

      3
      > (test #f)

      >


      You can return no value using (k).



      Read about Continuations.






      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%2f23327911%2fwhat-is-the-return-in-scheme%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        2
        down vote













        In Scheme there isn't an explicit return keyword - it's a lot simpler than that, the value of the last expression in a sequence of expressions is the one that gets returned. For example, your Python code will translate to this, and notice that the (> x 10) case had to be moved to the bottom, so if it's true it can exit the function immediately with a #f value.



        (define (test x)
        (if (> x 1)
        (do-something))
        (if (= x 4)
        (do-something))
        (if (> x 10)
        #f))

        (test 11)
        => #f


        In fact, after reordering the conditions we can remove the last one, but beware: an unspecified value will be returned if x is not 4, according to Guile's documentation - in other words, you should always return a value in each case, and an if expression should have both consequent and alternative parts.



        (define (test x)
        (if (> x 1)
        (do-something))
        (if (= x 4)
        (do-something)))

        (test 11)
        => unspecified


        And by the way, I believe the logic in the Python code is a bit off. The first condition will always be evaluated whenever a value of x greater than 1 is passed, but if it is less than or equal to 1 the returned value in Python will be None and in Scheme is unspecified. Also the original function isn't explicitly returning a value - in Python this means that None will be returned, in Scheme the returned value will be either (do-something) if x happens to be 4, or unspecified in any other case.






        share|improve this answer























        • This isn't really a faithful translation of the python code, which would have performed the actions for part 1 and 2.
          – amalloy
          Apr 27 '14 at 23:42










        • @amalloy you're right, I updated my answer with a direct translation
          – Óscar López
          Apr 28 '14 at 0:25






        • 1




          A more faithful translation would be (define (test x) (if (> x 1) (do-something)) (if (not (> x 10)) (if (= x 4) (do-something)))), since if (x) return; {block} is equivalent to if (!x) {block}.
          – Adam Rosenfield
          Apr 28 '14 at 0:29










        • @amalloy: Well, you could use call/cc here for an escape continuation :)
          – leppie
          Apr 28 '14 at 5:01















        up vote
        2
        down vote













        In Scheme there isn't an explicit return keyword - it's a lot simpler than that, the value of the last expression in a sequence of expressions is the one that gets returned. For example, your Python code will translate to this, and notice that the (> x 10) case had to be moved to the bottom, so if it's true it can exit the function immediately with a #f value.



        (define (test x)
        (if (> x 1)
        (do-something))
        (if (= x 4)
        (do-something))
        (if (> x 10)
        #f))

        (test 11)
        => #f


        In fact, after reordering the conditions we can remove the last one, but beware: an unspecified value will be returned if x is not 4, according to Guile's documentation - in other words, you should always return a value in each case, and an if expression should have both consequent and alternative parts.



        (define (test x)
        (if (> x 1)
        (do-something))
        (if (= x 4)
        (do-something)))

        (test 11)
        => unspecified


        And by the way, I believe the logic in the Python code is a bit off. The first condition will always be evaluated whenever a value of x greater than 1 is passed, but if it is less than or equal to 1 the returned value in Python will be None and in Scheme is unspecified. Also the original function isn't explicitly returning a value - in Python this means that None will be returned, in Scheme the returned value will be either (do-something) if x happens to be 4, or unspecified in any other case.






        share|improve this answer























        • This isn't really a faithful translation of the python code, which would have performed the actions for part 1 and 2.
          – amalloy
          Apr 27 '14 at 23:42










        • @amalloy you're right, I updated my answer with a direct translation
          – Óscar López
          Apr 28 '14 at 0:25






        • 1




          A more faithful translation would be (define (test x) (if (> x 1) (do-something)) (if (not (> x 10)) (if (= x 4) (do-something)))), since if (x) return; {block} is equivalent to if (!x) {block}.
          – Adam Rosenfield
          Apr 28 '14 at 0:29










        • @amalloy: Well, you could use call/cc here for an escape continuation :)
          – leppie
          Apr 28 '14 at 5:01













        up vote
        2
        down vote










        up vote
        2
        down vote









        In Scheme there isn't an explicit return keyword - it's a lot simpler than that, the value of the last expression in a sequence of expressions is the one that gets returned. For example, your Python code will translate to this, and notice that the (> x 10) case had to be moved to the bottom, so if it's true it can exit the function immediately with a #f value.



        (define (test x)
        (if (> x 1)
        (do-something))
        (if (= x 4)
        (do-something))
        (if (> x 10)
        #f))

        (test 11)
        => #f


        In fact, after reordering the conditions we can remove the last one, but beware: an unspecified value will be returned if x is not 4, according to Guile's documentation - in other words, you should always return a value in each case, and an if expression should have both consequent and alternative parts.



        (define (test x)
        (if (> x 1)
        (do-something))
        (if (= x 4)
        (do-something)))

        (test 11)
        => unspecified


        And by the way, I believe the logic in the Python code is a bit off. The first condition will always be evaluated whenever a value of x greater than 1 is passed, but if it is less than or equal to 1 the returned value in Python will be None and in Scheme is unspecified. Also the original function isn't explicitly returning a value - in Python this means that None will be returned, in Scheme the returned value will be either (do-something) if x happens to be 4, or unspecified in any other case.






        share|improve this answer














        In Scheme there isn't an explicit return keyword - it's a lot simpler than that, the value of the last expression in a sequence of expressions is the one that gets returned. For example, your Python code will translate to this, and notice that the (> x 10) case had to be moved to the bottom, so if it's true it can exit the function immediately with a #f value.



        (define (test x)
        (if (> x 1)
        (do-something))
        (if (= x 4)
        (do-something))
        (if (> x 10)
        #f))

        (test 11)
        => #f


        In fact, after reordering the conditions we can remove the last one, but beware: an unspecified value will be returned if x is not 4, according to Guile's documentation - in other words, you should always return a value in each case, and an if expression should have both consequent and alternative parts.



        (define (test x)
        (if (> x 1)
        (do-something))
        (if (= x 4)
        (do-something)))

        (test 11)
        => unspecified


        And by the way, I believe the logic in the Python code is a bit off. The first condition will always be evaluated whenever a value of x greater than 1 is passed, but if it is less than or equal to 1 the returned value in Python will be None and in Scheme is unspecified. Also the original function isn't explicitly returning a value - in Python this means that None will be returned, in Scheme the returned value will be either (do-something) if x happens to be 4, or unspecified in any other case.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Apr 28 '14 at 1:01

























        answered Apr 27 '14 at 19:36









        Óscar López

        173k22223318




        173k22223318












        • This isn't really a faithful translation of the python code, which would have performed the actions for part 1 and 2.
          – amalloy
          Apr 27 '14 at 23:42










        • @amalloy you're right, I updated my answer with a direct translation
          – Óscar López
          Apr 28 '14 at 0:25






        • 1




          A more faithful translation would be (define (test x) (if (> x 1) (do-something)) (if (not (> x 10)) (if (= x 4) (do-something)))), since if (x) return; {block} is equivalent to if (!x) {block}.
          – Adam Rosenfield
          Apr 28 '14 at 0:29










        • @amalloy: Well, you could use call/cc here for an escape continuation :)
          – leppie
          Apr 28 '14 at 5:01


















        • This isn't really a faithful translation of the python code, which would have performed the actions for part 1 and 2.
          – amalloy
          Apr 27 '14 at 23:42










        • @amalloy you're right, I updated my answer with a direct translation
          – Óscar López
          Apr 28 '14 at 0:25






        • 1




          A more faithful translation would be (define (test x) (if (> x 1) (do-something)) (if (not (> x 10)) (if (= x 4) (do-something)))), since if (x) return; {block} is equivalent to if (!x) {block}.
          – Adam Rosenfield
          Apr 28 '14 at 0:29










        • @amalloy: Well, you could use call/cc here for an escape continuation :)
          – leppie
          Apr 28 '14 at 5:01
















        This isn't really a faithful translation of the python code, which would have performed the actions for part 1 and 2.
        – amalloy
        Apr 27 '14 at 23:42




        This isn't really a faithful translation of the python code, which would have performed the actions for part 1 and 2.
        – amalloy
        Apr 27 '14 at 23:42












        @amalloy you're right, I updated my answer with a direct translation
        – Óscar López
        Apr 28 '14 at 0:25




        @amalloy you're right, I updated my answer with a direct translation
        – Óscar López
        Apr 28 '14 at 0:25




        1




        1




        A more faithful translation would be (define (test x) (if (> x 1) (do-something)) (if (not (> x 10)) (if (= x 4) (do-something)))), since if (x) return; {block} is equivalent to if (!x) {block}.
        – Adam Rosenfield
        Apr 28 '14 at 0:29




        A more faithful translation would be (define (test x) (if (> x 1) (do-something)) (if (not (> x 10)) (if (= x 4) (do-something)))), since if (x) return; {block} is equivalent to if (!x) {block}.
        – Adam Rosenfield
        Apr 28 '14 at 0:29












        @amalloy: Well, you could use call/cc here for an escape continuation :)
        – leppie
        Apr 28 '14 at 5:01




        @amalloy: Well, you could use call/cc here for an escape continuation :)
        – leppie
        Apr 28 '14 at 5:01












        up vote
        2
        down vote













        In Racket the most literal translation is:



        #lang racket    
        (define (test x)
        (let/ec return
        (when (> x 1)
        (do-something))
        (when (> x 10)
        (return 42))
        (when (= x 4)
        (do-something))))

        (define (do-something)
        (display "!"))
        (test 11)


        The let/ec is short for let/escape-continuation. Look up the equivalent control structure in the manual for your Scheme implementation of choice.



        The example displays one ! and then returns 42.






        share|improve this answer





















        • It is a Scheme question, not a Racket question. How difficult is it to replace let/ec with call/cc to help the OP directly?
          – GoZoner
          Apr 28 '14 at 22:28










        • As far as I knwow Guile also has let/ec.
          – soegaard
          Apr 29 '14 at 16:21















        up vote
        2
        down vote













        In Racket the most literal translation is:



        #lang racket    
        (define (test x)
        (let/ec return
        (when (> x 1)
        (do-something))
        (when (> x 10)
        (return 42))
        (when (= x 4)
        (do-something))))

        (define (do-something)
        (display "!"))
        (test 11)


        The let/ec is short for let/escape-continuation. Look up the equivalent control structure in the manual for your Scheme implementation of choice.



        The example displays one ! and then returns 42.






        share|improve this answer





















        • It is a Scheme question, not a Racket question. How difficult is it to replace let/ec with call/cc to help the OP directly?
          – GoZoner
          Apr 28 '14 at 22:28










        • As far as I knwow Guile also has let/ec.
          – soegaard
          Apr 29 '14 at 16:21













        up vote
        2
        down vote










        up vote
        2
        down vote









        In Racket the most literal translation is:



        #lang racket    
        (define (test x)
        (let/ec return
        (when (> x 1)
        (do-something))
        (when (> x 10)
        (return 42))
        (when (= x 4)
        (do-something))))

        (define (do-something)
        (display "!"))
        (test 11)


        The let/ec is short for let/escape-continuation. Look up the equivalent control structure in the manual for your Scheme implementation of choice.



        The example displays one ! and then returns 42.






        share|improve this answer












        In Racket the most literal translation is:



        #lang racket    
        (define (test x)
        (let/ec return
        (when (> x 1)
        (do-something))
        (when (> x 10)
        (return 42))
        (when (= x 4)
        (do-something))))

        (define (do-something)
        (display "!"))
        (test 11)


        The let/ec is short for let/escape-continuation. Look up the equivalent control structure in the manual for your Scheme implementation of choice.



        The example displays one ! and then returns 42.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 28 '14 at 9:00









        soegaard

        23.7k43774




        23.7k43774












        • It is a Scheme question, not a Racket question. How difficult is it to replace let/ec with call/cc to help the OP directly?
          – GoZoner
          Apr 28 '14 at 22:28










        • As far as I knwow Guile also has let/ec.
          – soegaard
          Apr 29 '14 at 16:21


















        • It is a Scheme question, not a Racket question. How difficult is it to replace let/ec with call/cc to help the OP directly?
          – GoZoner
          Apr 28 '14 at 22:28










        • As far as I knwow Guile also has let/ec.
          – soegaard
          Apr 29 '14 at 16:21
















        It is a Scheme question, not a Racket question. How difficult is it to replace let/ec with call/cc to help the OP directly?
        – GoZoner
        Apr 28 '14 at 22:28




        It is a Scheme question, not a Racket question. How difficult is it to replace let/ec with call/cc to help the OP directly?
        – GoZoner
        Apr 28 '14 at 22:28












        As far as I knwow Guile also has let/ec.
        – soegaard
        Apr 29 '14 at 16:21




        As far as I knwow Guile also has let/ec.
        – soegaard
        Apr 29 '14 at 16:21










        up vote
        1
        down vote













        The implicit return of Scheme can be illustrated by comparing how you can implement a simple function, such as square, in Python and scheme.



        In Python:



        def square(x):
        return x*x;


        In Scheme:



        (define (square x)
        (* x x))





        share|improve this answer

























          up vote
          1
          down vote













          The implicit return of Scheme can be illustrated by comparing how you can implement a simple function, such as square, in Python and scheme.



          In Python:



          def square(x):
          return x*x;


          In Scheme:



          (define (square x)
          (* x x))





          share|improve this answer























            up vote
            1
            down vote










            up vote
            1
            down vote









            The implicit return of Scheme can be illustrated by comparing how you can implement a simple function, such as square, in Python and scheme.



            In Python:



            def square(x):
            return x*x;


            In Scheme:



            (define (square x)
            (* x x))





            share|improve this answer












            The implicit return of Scheme can be illustrated by comparing how you can implement a simple function, such as square, in Python and scheme.



            In Python:



            def square(x):
            return x*x;


            In Scheme:



            (define (square x)
            (* x x))






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Apr 28 '14 at 4:50









            R Sahu

            163k1291184




            163k1291184






















                up vote
                1
                down vote













                As others have said, the last expression's value in a function is its return value, so you just have to arrange for exclusive execution pathways in your code, to achieve this effect.



                (if <test> <consequent> <alternative>) is the basic branching operation in Scheme:



                (define (test x)
                (if (> x 1)
                (do_something)
                #f)
                (if (> x 10)
                #f ; return #f
                ;; else:
                (if (= x 4)
                (do_something)
                ;; else:
                #f)))

                (test 11)


                Or we could use cond to avoid the needlessly nested structure in the code:



                (define (test x)
                (if (> x 1)
                (do_something)
                #f)
                (cond
                ( (> x 10) #f)
                ( (= x 4) (do_something))
                ( else #f)))





                share|improve this answer

























                  up vote
                  1
                  down vote













                  As others have said, the last expression's value in a function is its return value, so you just have to arrange for exclusive execution pathways in your code, to achieve this effect.



                  (if <test> <consequent> <alternative>) is the basic branching operation in Scheme:



                  (define (test x)
                  (if (> x 1)
                  (do_something)
                  #f)
                  (if (> x 10)
                  #f ; return #f
                  ;; else:
                  (if (= x 4)
                  (do_something)
                  ;; else:
                  #f)))

                  (test 11)


                  Or we could use cond to avoid the needlessly nested structure in the code:



                  (define (test x)
                  (if (> x 1)
                  (do_something)
                  #f)
                  (cond
                  ( (> x 10) #f)
                  ( (= x 4) (do_something))
                  ( else #f)))





                  share|improve this answer























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    As others have said, the last expression's value in a function is its return value, so you just have to arrange for exclusive execution pathways in your code, to achieve this effect.



                    (if <test> <consequent> <alternative>) is the basic branching operation in Scheme:



                    (define (test x)
                    (if (> x 1)
                    (do_something)
                    #f)
                    (if (> x 10)
                    #f ; return #f
                    ;; else:
                    (if (= x 4)
                    (do_something)
                    ;; else:
                    #f)))

                    (test 11)


                    Or we could use cond to avoid the needlessly nested structure in the code:



                    (define (test x)
                    (if (> x 1)
                    (do_something)
                    #f)
                    (cond
                    ( (> x 10) #f)
                    ( (= x 4) (do_something))
                    ( else #f)))





                    share|improve this answer












                    As others have said, the last expression's value in a function is its return value, so you just have to arrange for exclusive execution pathways in your code, to achieve this effect.



                    (if <test> <consequent> <alternative>) is the basic branching operation in Scheme:



                    (define (test x)
                    (if (> x 1)
                    (do_something)
                    #f)
                    (if (> x 10)
                    #f ; return #f
                    ;; else:
                    (if (= x 4)
                    (do_something)
                    ;; else:
                    #f)))

                    (test 11)


                    Or we could use cond to avoid the needlessly nested structure in the code:



                    (define (test x)
                    (if (> x 1)
                    (do_something)
                    #f)
                    (cond
                    ( (> x 10) #f)
                    ( (= x 4) (do_something))
                    ( else #f)))






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Apr 29 '14 at 18:29









                    Will Ness

                    42.2k467118




                    42.2k467118






















                        up vote
                        0
                        down vote













                        You may use call/cc



                        (define (test x) (call/cc (lambda (k)
                        (if x
                        (k x)
                        (k))
                        (display "never displayed"))))




                        > (test 3)

                        3
                        > (test #f)

                        >


                        You can return no value using (k).



                        Read about Continuations.






                        share|improve this answer



























                          up vote
                          0
                          down vote













                          You may use call/cc



                          (define (test x) (call/cc (lambda (k)
                          (if x
                          (k x)
                          (k))
                          (display "never displayed"))))




                          > (test 3)

                          3
                          > (test #f)

                          >


                          You can return no value using (k).



                          Read about Continuations.






                          share|improve this answer

























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            You may use call/cc



                            (define (test x) (call/cc (lambda (k)
                            (if x
                            (k x)
                            (k))
                            (display "never displayed"))))




                            > (test 3)

                            3
                            > (test #f)

                            >


                            You can return no value using (k).



                            Read about Continuations.






                            share|improve this answer














                            You may use call/cc



                            (define (test x) (call/cc (lambda (k)
                            (if x
                            (k x)
                            (k))
                            (display "never displayed"))))




                            > (test 3)

                            3
                            > (test #f)

                            >


                            You can return no value using (k).



                            Read about Continuations.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 19 at 7:03









                            Will Ness

                            42.2k467118




                            42.2k467118










                            answered Mar 9 '15 at 5:06









                            Karl Lee

                            1




                            1






























                                 

                                draft saved


                                draft discarded



















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f23327911%2fwhat-is-the-return-in-scheme%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Costa Masnaga

                                Fotorealismo

                                Sidney Franklin