I need to understand how can I access to an input of a function which is given to another function as an input.
For example; I have a function called f which simply does (define f (lambda (x) (if (null? x) #t (car x)))) this. I need to write a function which takes this f as an input and returns another function such that,
-Define a function (twoback f) which takes a function f as its input.
-As its output, it should return a new function g which has the following behaviour:
g(x) = #t if x is the empty list or a list of length 1.
= f(y) where y=(cdr x) otherwise.
And the function will called like this : ((twoback f3) (list #t #f #t #f))
So actually my question is : How can I access the list given with the function call in the function that I'm going to write(the twoback function) ? Because i need to check whether it is empty or not.
Short answer, by currying the x parameter. Something like this:
(define twoback
(lambda (f)
(lambda (x)
...))) ; fill-in with the logic requested
The above will define a procedure called twoback which receives f as parameter, and in turn it will return a new procedure which receives x as parameter. This second procedure being returned is the one called g in the question, from it you can access both f and x as you would normally do.
Now just complete the ... part with the expected output.
I think this is what you mean:
(define (twoback f)
(lambda (x)
(if
(or (null? x) (null? (cdr x)))
#t
(f (cdr x)))))
(define f (lambda (x) (if (null? x) #t (car x))))
Related
I was looking for a way to repeat a function call a certain number of times and accumulate the results in a list and couldn't find anything in the standard library, so I wrote one. It's simple to write, but it seems like such an obvious thing that I feel like there must be an accepted way of doing this using standard library functions.
Here are the two functions I'm looking to replace:
(define (repeat n f)
(unless (<= n 0)
(f)
(repeat (sub1 n) f)))
(define (accumulate n f)
(let loop ([n n] [l empty])
(if (<= n 0)
l
(loop (sub1 n)
(cons (f) l)))))
Is there any simpler way of achieving this?
If your function does not any take arguments, you can use build-list
Example:
#lang racket
;; The function you want to call many times
(define (f)
#t)
;; Use build list with a lambda to wrap your function because
;; build-list want a function takin integer as its second argument
(build-list 5 (lambda (x) (f)))
result:
'(#t #t #t #t #t)
Edit: you can also define a function to wrap the lambda
(define (repeat-then-accumulate n f)
(build-list n (lambda (x) (f)))
)
Usage:
;; Using the f function defined earlier
(repeat-then-accumulate 10 f)
result:
'(#t #t #t #t #t #t #t #t #t #t)
Edit2: If you want to have fixed args, you could do something like
#lang racket
;; The function you want to call many times
(define (f a b)
(+ a b))
(define (repeat-then-accumulate n f args)
(build-list n (lambda (x) (apply f args)))
)
Usage:
(repeat-then-accumulate 10 f '(3 5))
Result:
'(8 8 8 8 8 8 8 8 8 8)
It looks like #AlexKnauth didn't feel like taking your internet points for his answer, and he phrased it as a comment. I'm not proud, though... Use racket's list comprehension form:
(for/list ([i (in-range n)]) (f i))
(I added an explicit "in-range", just to get better error-checking.)
New to Lisp. Trying to pass a list into a recursive function and do something with the first item in the list each time. This is the function so far:
(setq colors '(red blue green yellow orange pink purple))
(defun my-function (x)
(if (> (length x) 0)
(let ((c (car x))
c)
(my-function x))))
Keep getting an error saying x is a void element. Not sure what to do.
If I reformat your function, maybe you can see what you're doing wrong:
(defun my-function (x)
(if (> (length x) 0) ; do nothing if list is empty
(let ((c (car x)) ; bind c to (car x)
c) ; bind c to nil instead
; c is never used
(my-function x)))) ; recursively call function
; with unmodified x
; until the stack is blown
Keep getting an error saying x is a void element.
I imagine you're calling (my-function x) with an undefined x instead of passing it your colors list with (my-function colors), but that's certainly not your only problem.
I have here a function that I need to modify so that I would avoid the double recursive call of (f (car l)) . First of all I can't figure it out what it shows..
If I pass (f '((3 4) 5 6)) it shows me CAR: 3 is not a list
Can anybody help me understand and then modify it?
(DEFUN F (L)
(COND
((NULL L) 0)
((> (f (car l)) 2) (+ (car l) (f (cdr l))))
(T (f (CAR L)))
))
You can figure out what this function should accept as input by looking at what it does with the input, and what it returns by looking at what each case returns. There are three cases:
Case 1
((NULL L) 0)
In this case, L can be nil, and 0, which is a number, is returned.
Case 2
((> (f (car l)) 2) (+ (car l) (f (cdr l))))
In this case, we call both car and cdr on l, so l had better be a cons. We also compare (f (car l)) with 2, so f must return a number, at least for whatever type (car l) is. Since we're calling + with (car l), (car l) must be a number. So f must return a number when given a number. Now, we're also calling + with (f (cdr l)), so whatever type (cdr l) has, f had better return a number for it, too.
Case 3
(T (f (CAR L)))
This doesn't put many constraints on us. This just says that if we didn't have either of the first two cases, then we return (f (car l)). Since checking the second case didn't fail, and because we're calling (car l), l still has to be a cons in this case.
So what is f?
Well, it's still not immediately clear what f is, but we can write it as a piecewise function, and maybe that will help. It take a list, which is either the empty list or a cons that has a first and a rest.
f [] = 0
f x:xs = if (f x) > 2
then x + (f xs)
else (f x)
To modify it so that you only call (f (car l)) is easy enough, although since we know that the input needs to be a list, I'm going to use first and rest to suggest that, rather than car and cdr.
(defun f (list)
(if (endp list)
0
(let ((tmp (f (first list))))
(if (> tmp 2)
(+ (first list)
(f (rest list)))
tmp))))
Let's try to walk through some possible inputs and try to cover the different code branches. What sort of input could we call this with? Well, we can call it with ():
CL-USER> (f '())
0
That takes care of the first then branch. Now what if we want to hit the second? Then we need to pass something that's not the empty list, so it looks like (? . ??). Now the first thing that has to happen is a recursive call to(f (first list)). The only way that this is going to work is if(first list)is also a list that we can pass tofand get a value back. Then(first list)` must have either been the empty list or another suitable list. So we can call:
CL-USER> (f '(() a b c))
0
In general, we can call f with () and with any list such that (first (first (first ... (first list)))) is (). Can we call it with anything else? It doesn't appear so. So now we know what the acceptable inputs to f are:
input ::= ()
| (input . anything)
and the output will always be 0.
I was wondering if anyone could explain this lambda expression and how the output is derived. I put it into the interpreter and am getting ((2) 2). I'm just not sure why it's giving me that instead of just (2 2).
((lambda x (cons x x)) 2)
The expression (lambda x (cons x x)) produces a function; the function puts all arguments into a list x; the function returns (cons x x).
Your expression calls the above function with an argument of 2. In the function x is (2) (a list of all the arguments). The function (cons '(2) '(2)) returns ((2) 2)
(cons x x)
is not the same as
(list x x)
since it produces dotted pairs, e.g. (cons 2 2) returns (2 . 2).
But when the right side of a dotted pair is a list, the whole thing is a list. (lambda x expr) takes an arbitrary number of arguments, puts them in a list x, so that's (2) here. The dotted pair ((2) . (2)) is printed as ((2) 2) per Lisp conventions.
Yep, you've ran off the deep end of scheme.
You've stublled across the notation that allows you to write a function that accepts zero or more arguments. (any number really)
(define add-nums
(lambda x
(if (null? x)
0
(+ (car x) (apply add-nums (cdr x))))))
(add-nums 1 87 203 87 2 4 5)
;Value: 389
If you just want one argument you need to enclose x in a set of parenthesis.
And you want to use
(list x x)
or
(cons x (cons x '())
as the function body, as a properly formed list will have an empty list in the tail position.
You probably wanted to write:
((lambda (x) (cons x x)) 2)
(note the brackets around x).
In the following code, I am trying to understand how the variable whatami gets its value. In following the logic, I see that the procedure (lambda (y) (/ x y)) is the parameter that I am passing to the method average-damp, and is represented within that method as the variable f. It seems as though (/ x y) and (average (f whatami) whatami) need to be executed, but I can't figure out the order of execution. Any help is appreciated.
(define (average x y)
(/ (+ x y) 2))
(define (fixed-point f start)
(define tolerance 0.00001)
(define (close-enuf? u v)
(< (abs (- u v)) tolerance))
(define (iter old new)
(if (close-enuf? old new)
new
(iter new (f new))))
(iter start (f start)))
(define average-damp
(lambda (f)
(lambda (whatami) (average (f whatami) whatami))))
; square root with average damping
(define (_sqrt x)
(fixed-point
(average-damp (lambda (y) (/ x y)))
1))
(_sqrt 4.0)
The average-damp procedure takes a procedure as its argument and returns a procedure as its value. When given a procedure that takes one argument, average-damp returns another procedure that computes the average of the values before and after applying the original function f to its argument. It's inside the fixed-point procedure where that returned function is applied (iteratively).
So the average-damp procedure doesn't execute either (/ x y) or (average(f whatami) whatami) at all, it just uses the function passed to it to create a new function that it returns.