How do I pass (not f) as an f?












0















I have a filter function that takes in a predicate like odd?
and processes a list. Here is the code



(define (manual-filter f? lst)
(cond
[(empty? lst) empty]
[(f? (first lst)) (cons (first lst) (manual-filter f? (rest lst)))]
[else (manual-filter f? (rest lst))]))


How do I go about passing (not (f?)) as the function?










share|improve this question























  • (define even? (compose not odd?))?

    – Sylwester
    Nov 25 '18 at 0:22













  • it's for a homework question so we can't use abstract functions such as compose, and odd? was an example, it could be any predicate

    – Syvash
    Nov 25 '18 at 0:24


















0















I have a filter function that takes in a predicate like odd?
and processes a list. Here is the code



(define (manual-filter f? lst)
(cond
[(empty? lst) empty]
[(f? (first lst)) (cons (first lst) (manual-filter f? (rest lst)))]
[else (manual-filter f? (rest lst))]))


How do I go about passing (not (f?)) as the function?










share|improve this question























  • (define even? (compose not odd?))?

    – Sylwester
    Nov 25 '18 at 0:22













  • it's for a homework question so we can't use abstract functions such as compose, and odd? was an example, it could be any predicate

    – Syvash
    Nov 25 '18 at 0:24
















0












0








0


0






I have a filter function that takes in a predicate like odd?
and processes a list. Here is the code



(define (manual-filter f? lst)
(cond
[(empty? lst) empty]
[(f? (first lst)) (cons (first lst) (manual-filter f? (rest lst)))]
[else (manual-filter f? (rest lst))]))


How do I go about passing (not (f?)) as the function?










share|improve this question














I have a filter function that takes in a predicate like odd?
and processes a list. Here is the code



(define (manual-filter f? lst)
(cond
[(empty? lst) empty]
[(f? (first lst)) (cons (first lst) (manual-filter f? (rest lst)))]
[else (manual-filter f? (rest lst))]))


How do I go about passing (not (f?)) as the function?







scheme racket






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 23:44









SyvashSyvash

41




41













  • (define even? (compose not odd?))?

    – Sylwester
    Nov 25 '18 at 0:22













  • it's for a homework question so we can't use abstract functions such as compose, and odd? was an example, it could be any predicate

    – Syvash
    Nov 25 '18 at 0:24





















  • (define even? (compose not odd?))?

    – Sylwester
    Nov 25 '18 at 0:22













  • it's for a homework question so we can't use abstract functions such as compose, and odd? was an example, it could be any predicate

    – Syvash
    Nov 25 '18 at 0:24



















(define even? (compose not odd?))?

– Sylwester
Nov 25 '18 at 0:22







(define even? (compose not odd?))?

– Sylwester
Nov 25 '18 at 0:22















it's for a homework question so we can't use abstract functions such as compose, and odd? was an example, it could be any predicate

– Syvash
Nov 25 '18 at 0:24







it's for a homework question so we can't use abstract functions such as compose, and odd? was an example, it could be any predicate

– Syvash
Nov 25 '18 at 0:24














2 Answers
2






active

oldest

votes


















4














the not function has the signature Any -> Boolean. And is essentially:



(define (not x)
(if (eq? x #f) #t #f))


What you are looking for is a function that inverts the output to another given function. (Or basically, a function of the signature: (Any -> Boolean) -> (Any -> Boolean)



As @Sylwester suggested, the easiest way to do this is with compose. You could define it as:



(define (invert f)
(compose not f))


Now you could, say, define even? as:



(define even? (invert odd?))


You can also define invert without using compose. I'll give you the template and leave the rest as an exercise:



;; Invert the results of a predicate
;; (Any -> Boolean) -> (Any -> Boolean)
(define (invert f)
(lambda (x)
(cond
[(f x) ...]
[else ...])))


(Note that racket has a shorthand for functions like this:



 (define ((invert f) x)
(cond
[(f x) ...]
[else ...]))





share|improve this answer
























  • It is not invert. It is negate.

    – PetSerAl
    Nov 25 '18 at 10:46



















0














Solution



Write a function, which returns a lambda-expression with applies not to the application of your predicate function on any argument.



(define (my-not predicate-func)
(lambda (x) (not (predicate-func x))))


(my-not f?) returns then a function which is the inverted predicate function of f?.
(So no compose needed ... - well my-not is manually compose-ing the function not with f? (the predicate-function) in its function definition body).



I called it my-not to not to overwrite the already existing not function in Racket which converts booleans thus returns a boolean.
(my-not takes a function and returns a function - Function -> Function or lambda -> lambda :) .)



my-not could be also called invert.



Application



Thus, you can give (my-not f?) to your manual-filter function instead of f?:



(manual-filter (my-not f?) lst)


Try out



You can also test:



(even? 3) ;; #f
(even? 4) ;; #t

((my-not even?) 3) ;; #t
((my-not even?) 4) ;; #f

;; so `(my-not even?)` behaves exactly like `odd?` - the inverse of `even?`
(odd? 3) ;; #t
(odd? 4) ;; #f





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',
    autoActivateHeartbeat: false,
    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%2f53463386%2fhow-do-i-pass-not-f-as-an-f%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    the not function has the signature Any -> Boolean. And is essentially:



    (define (not x)
    (if (eq? x #f) #t #f))


    What you are looking for is a function that inverts the output to another given function. (Or basically, a function of the signature: (Any -> Boolean) -> (Any -> Boolean)



    As @Sylwester suggested, the easiest way to do this is with compose. You could define it as:



    (define (invert f)
    (compose not f))


    Now you could, say, define even? as:



    (define even? (invert odd?))


    You can also define invert without using compose. I'll give you the template and leave the rest as an exercise:



    ;; Invert the results of a predicate
    ;; (Any -> Boolean) -> (Any -> Boolean)
    (define (invert f)
    (lambda (x)
    (cond
    [(f x) ...]
    [else ...])))


    (Note that racket has a shorthand for functions like this:



     (define ((invert f) x)
    (cond
    [(f x) ...]
    [else ...]))





    share|improve this answer
























    • It is not invert. It is negate.

      – PetSerAl
      Nov 25 '18 at 10:46
















    4














    the not function has the signature Any -> Boolean. And is essentially:



    (define (not x)
    (if (eq? x #f) #t #f))


    What you are looking for is a function that inverts the output to another given function. (Or basically, a function of the signature: (Any -> Boolean) -> (Any -> Boolean)



    As @Sylwester suggested, the easiest way to do this is with compose. You could define it as:



    (define (invert f)
    (compose not f))


    Now you could, say, define even? as:



    (define even? (invert odd?))


    You can also define invert without using compose. I'll give you the template and leave the rest as an exercise:



    ;; Invert the results of a predicate
    ;; (Any -> Boolean) -> (Any -> Boolean)
    (define (invert f)
    (lambda (x)
    (cond
    [(f x) ...]
    [else ...])))


    (Note that racket has a shorthand for functions like this:



     (define ((invert f) x)
    (cond
    [(f x) ...]
    [else ...]))





    share|improve this answer
























    • It is not invert. It is negate.

      – PetSerAl
      Nov 25 '18 at 10:46














    4












    4








    4







    the not function has the signature Any -> Boolean. And is essentially:



    (define (not x)
    (if (eq? x #f) #t #f))


    What you are looking for is a function that inverts the output to another given function. (Or basically, a function of the signature: (Any -> Boolean) -> (Any -> Boolean)



    As @Sylwester suggested, the easiest way to do this is with compose. You could define it as:



    (define (invert f)
    (compose not f))


    Now you could, say, define even? as:



    (define even? (invert odd?))


    You can also define invert without using compose. I'll give you the template and leave the rest as an exercise:



    ;; Invert the results of a predicate
    ;; (Any -> Boolean) -> (Any -> Boolean)
    (define (invert f)
    (lambda (x)
    (cond
    [(f x) ...]
    [else ...])))


    (Note that racket has a shorthand for functions like this:



     (define ((invert f) x)
    (cond
    [(f x) ...]
    [else ...]))





    share|improve this answer













    the not function has the signature Any -> Boolean. And is essentially:



    (define (not x)
    (if (eq? x #f) #t #f))


    What you are looking for is a function that inverts the output to another given function. (Or basically, a function of the signature: (Any -> Boolean) -> (Any -> Boolean)



    As @Sylwester suggested, the easiest way to do this is with compose. You could define it as:



    (define (invert f)
    (compose not f))


    Now you could, say, define even? as:



    (define even? (invert odd?))


    You can also define invert without using compose. I'll give you the template and leave the rest as an exercise:



    ;; Invert the results of a predicate
    ;; (Any -> Boolean) -> (Any -> Boolean)
    (define (invert f)
    (lambda (x)
    (cond
    [(f x) ...]
    [else ...])))


    (Note that racket has a shorthand for functions like this:



     (define ((invert f) x)
    (cond
    [(f x) ...]
    [else ...]))






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 25 '18 at 0:35









    Leif AndersenLeif Andersen

    11.4k135585




    11.4k135585













    • It is not invert. It is negate.

      – PetSerAl
      Nov 25 '18 at 10:46



















    • It is not invert. It is negate.

      – PetSerAl
      Nov 25 '18 at 10:46

















    It is not invert. It is negate.

    – PetSerAl
    Nov 25 '18 at 10:46





    It is not invert. It is negate.

    – PetSerAl
    Nov 25 '18 at 10:46













    0














    Solution



    Write a function, which returns a lambda-expression with applies not to the application of your predicate function on any argument.



    (define (my-not predicate-func)
    (lambda (x) (not (predicate-func x))))


    (my-not f?) returns then a function which is the inverted predicate function of f?.
    (So no compose needed ... - well my-not is manually compose-ing the function not with f? (the predicate-function) in its function definition body).



    I called it my-not to not to overwrite the already existing not function in Racket which converts booleans thus returns a boolean.
    (my-not takes a function and returns a function - Function -> Function or lambda -> lambda :) .)



    my-not could be also called invert.



    Application



    Thus, you can give (my-not f?) to your manual-filter function instead of f?:



    (manual-filter (my-not f?) lst)


    Try out



    You can also test:



    (even? 3) ;; #f
    (even? 4) ;; #t

    ((my-not even?) 3) ;; #t
    ((my-not even?) 4) ;; #f

    ;; so `(my-not even?)` behaves exactly like `odd?` - the inverse of `even?`
    (odd? 3) ;; #t
    (odd? 4) ;; #f





    share|improve this answer






























      0














      Solution



      Write a function, which returns a lambda-expression with applies not to the application of your predicate function on any argument.



      (define (my-not predicate-func)
      (lambda (x) (not (predicate-func x))))


      (my-not f?) returns then a function which is the inverted predicate function of f?.
      (So no compose needed ... - well my-not is manually compose-ing the function not with f? (the predicate-function) in its function definition body).



      I called it my-not to not to overwrite the already existing not function in Racket which converts booleans thus returns a boolean.
      (my-not takes a function and returns a function - Function -> Function or lambda -> lambda :) .)



      my-not could be also called invert.



      Application



      Thus, you can give (my-not f?) to your manual-filter function instead of f?:



      (manual-filter (my-not f?) lst)


      Try out



      You can also test:



      (even? 3) ;; #f
      (even? 4) ;; #t

      ((my-not even?) 3) ;; #t
      ((my-not even?) 4) ;; #f

      ;; so `(my-not even?)` behaves exactly like `odd?` - the inverse of `even?`
      (odd? 3) ;; #t
      (odd? 4) ;; #f





      share|improve this answer




























        0












        0








        0







        Solution



        Write a function, which returns a lambda-expression with applies not to the application of your predicate function on any argument.



        (define (my-not predicate-func)
        (lambda (x) (not (predicate-func x))))


        (my-not f?) returns then a function which is the inverted predicate function of f?.
        (So no compose needed ... - well my-not is manually compose-ing the function not with f? (the predicate-function) in its function definition body).



        I called it my-not to not to overwrite the already existing not function in Racket which converts booleans thus returns a boolean.
        (my-not takes a function and returns a function - Function -> Function or lambda -> lambda :) .)



        my-not could be also called invert.



        Application



        Thus, you can give (my-not f?) to your manual-filter function instead of f?:



        (manual-filter (my-not f?) lst)


        Try out



        You can also test:



        (even? 3) ;; #f
        (even? 4) ;; #t

        ((my-not even?) 3) ;; #t
        ((my-not even?) 4) ;; #f

        ;; so `(my-not even?)` behaves exactly like `odd?` - the inverse of `even?`
        (odd? 3) ;; #t
        (odd? 4) ;; #f





        share|improve this answer















        Solution



        Write a function, which returns a lambda-expression with applies not to the application of your predicate function on any argument.



        (define (my-not predicate-func)
        (lambda (x) (not (predicate-func x))))


        (my-not f?) returns then a function which is the inverted predicate function of f?.
        (So no compose needed ... - well my-not is manually compose-ing the function not with f? (the predicate-function) in its function definition body).



        I called it my-not to not to overwrite the already existing not function in Racket which converts booleans thus returns a boolean.
        (my-not takes a function and returns a function - Function -> Function or lambda -> lambda :) .)



        my-not could be also called invert.



        Application



        Thus, you can give (my-not f?) to your manual-filter function instead of f?:



        (manual-filter (my-not f?) lst)


        Try out



        You can also test:



        (even? 3) ;; #f
        (even? 4) ;; #t

        ((my-not even?) 3) ;; #t
        ((my-not even?) 4) ;; #f

        ;; so `(my-not even?)` behaves exactly like `odd?` - the inverse of `even?`
        (odd? 3) ;; #t
        (odd? 4) ;; #f






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 25 '18 at 10:41

























        answered Nov 25 '18 at 9:59









        Gwang-Jin KimGwang-Jin Kim

        2,474217




        2,474217






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463386%2fhow-do-i-pass-not-f-as-an-f%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

            Wiesbaden

            Marschland

            Dieringhausen