How do I pass (not f) as an f?
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
add a comment |
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
(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
add a comment |
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
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
scheme racket
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
add a comment |
(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
add a comment |
2 Answers
2
active
oldest
votes
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 ...]))
It is notinvert
. It isnegate
.
– PetSerAl
Nov 25 '18 at 10:46
add a comment |
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
add a comment |
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
});
}
});
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
Required, but never shown
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
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 ...]))
It is notinvert
. It isnegate
.
– PetSerAl
Nov 25 '18 at 10:46
add a comment |
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 ...]))
It is notinvert
. It isnegate
.
– PetSerAl
Nov 25 '18 at 10:46
add a comment |
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 ...]))
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 ...]))
answered Nov 25 '18 at 0:35
Leif AndersenLeif Andersen
11.4k135585
11.4k135585
It is notinvert
. It isnegate
.
– PetSerAl
Nov 25 '18 at 10:46
add a comment |
It is notinvert
. It isnegate
.
– 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
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Nov 25 '18 at 10:41
answered Nov 25 '18 at 9:59
Gwang-Jin KimGwang-Jin Kim
2,474217
2,474217
add a comment |
add a comment |
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.
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
Required, but never shown
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
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
Required, but never shown
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
Required, but never shown
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
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
(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