Return a predecessor for a given value and given list - scheme

I'm familiar with recursion procedures, but somehow I cannot solve this problem: I want to return a predecessor value for a given list.
(define (pred value lst)
...)
(pred 3 (list 8 3 7 3)) should return 8
(pred 2 (list 1 2 2 2 2)) should return 1
Note that I only want to return the "first" occurrence of a predecessor, therefore in this first example the number 7 does not have to be returned.
I'm currently stuck because I "loose information" about the predecessor value, once I call a recursion by (pred value (rest lst)) ... I don't know to "store" this information in e.g. a list.
Thanks for any help! I'm already trying for hours...

So here is how to fix this. You make a helper that takes additional arguments. One of those can be the previous element. eg.
(define (iterate-pred value last lst)
...)
(pred 3 '(1 2 3 4)) ; ==>
(iterate-pred 3 1 '(2 3 4)) ; ==>
(iterate-pred 3 2 '(3 4)) ; ==>
; ==> 2
So to update the variables you just call the same procedure again with the new values. It is just as easy as iterating the list to begin with, which uses the same strategy.
You can implement these as internal procedure either with define/letrec or you can use named let. Then you can omit value in the helper since you have access to it through the scope.
Good luck!

Related

Any way to map functions with more than one argument in Scheme/Racket?

I'm currently doing an introductory course in Racket/Scheme, and am currently studying map, apply, and fold. For the most part, I've been assuming that map can only work on lambdas that accept a single argument. However, for certain problems I would find it incredibly useful to get around this somewhat - say, having a function that can be mapped to a list whilst also carrying an accumulator that updates independently for each recursive function call. While I can't get much more specific than that for fear of violating Honor Code, is there any way to get around being unable to give each recursive call an accumulator if you plan to use map?
map takes as many lists as you want:
(map + '(1 2 3) '(3 2 1) '(3 3 3)) ; ==> (7 7 7)
fold has an accumulator:
(foldl (lambda (a b c acc) (+ a b c acc)) 1 '(1 2 3) '(3 2 1) '(3 3 3)) ; ==> 22

DrRacket 2 list inputs and reversing only one

I'll preface this by saying this is for an assignment, but I am not sure how to approach this after going through other stackoverflow questions for an hour or two.
I'm attempting to create a reverse function that accepts 2 lists as input and returns the first list reverse appended with the second list.
Example: (reverse '(1 2) '(3 4)) --> (2 1 3 4)
My code is below, I have tried conditional statements such as when the first list is NOT null then do the main logic, then when it is only return l2 (which still returned (3 4 2 1) instead of (2 1 3 4).
The problem I'm having is that no matter what I do the second list is always at the beginning of the first reversed list.
(define (myreverse l1 l2)
(if (null? l) l2)
(append (myreverse(cdr l1) l2) (list(car l1))))
Consider the following:
(reverse '(1 2 3 4))
=> '(4 3 2 1)
(append (reverse '(1 2)) '(3 4))
=> '(2 1 3 4)
Think about how you can implement reverse, and how you can use it in your myreverse procedure.
With racket/scheme it is very helpful to think of these kinds of problems from the bottom up.
For example, what do you do when the first list is null?? Well, you're done! so just return the second list. What do you do when it has just one element? Well, we tack that element onto the second list and we're done. But a moment's thought tells us that all the cases are now covered.
(define (rev-first left right)
(if (null? left)
right
(rev-first (??? left) (???? right))))
Actually this kind of recursion is captured with foldl and foldr more generally. But I don't know if it's instructive for you at this point.

Scheme language add one to every list element

Here is the problem:
Define a function addOne, which takes a list of numbers and returns a list where each number in the input list is increased by one. For example, (addOne ′(1 2 3 4)) should return (2 3 4 5), and (addOne ′(2 4 6 8)) should return (3 5 7 9).
I'm new for scheme language, need the help. Thank you!
Using map:
(define (add-one nums)
(map (lambda (x) (+ x 1)) nums))
I tried to think of a simple solution to this problem. I created a helper procedure, plus1, which takes in a number and returns the sum of that number and 1:
(define (plus1 x)
(+ x 1))
Then, I defined addOne using map:
(define (addOne lst)
(map plus1 lst))
Basically, map takes in two arguments: a procedure and a list. It applies that procedure to every item of that list, then returns the modified list according to that procedure. In this case, the map part of addOne just takes every item of the inputted list, adds 1 to it, and returns the new list.
Please respond with questions or feedback!

Scheme functions and lists, multiply,add

i am trying to write 2 functions in scheme, the first would multiply every value in a list by a user specified value, 2nd function would add a number to all the values in the list from previous result.
i tried something like that but racket throws an error.
(define test (list 1 1 2 3 5))
(define funca(*(test)(2)))
In Scheme we use the map higher-order procedure for applying a function over a list of elements - bear in mind that you can't multiply a list, what we can do is multiply each of its elements . For example, to multiply each of the elements by two do this:
(define test (list 1 1 2 3 5))
(map (lambda (element) (* 2 element))
test)
=> '(2 2 4 6 10)
Notice how we pass a lambda as parameter to map: that's a function that will get applied to each of the elements in the input list, returning a new list with the results. Similarly if we need to, say, add one to the elements in a list:
(map (lambda (element) (+ 1 element))
test)
=> '(2 2 3 4 6)
The above examples are hard-coded to multiply by two and to add one. For solving your problem, you just have to put each of the above snippets inside a function and pass along the correct parameters in the right places (left as an exercise for the reader).

Extracting only numbers within a list

Is there a way to extract only numbers within a list?
I'm using the beginner language package so I cannot use filter which is a bummer.
(list a 1 2 b d 3 5) => 1 2 3 5 etc
I want to use this as a part of my helper function but I cannot figure it out!
thanks!
Ideally this problem should be solved using the filter higher-order procedure, like this:
(filter number? '(a 1 2 b d 3 5))
=> '(1 2 3 5)
... But because this looks like a homework, I'll give you some hints on how to solve the problem by hand, just fill-in the blanks:
(define (only-numbers lst)
(cond (<???> ; is the list empty?
<???>) ; return the em´pty list
(<???> ; is the 1st element in the list a number?
(cons <???> ; then cons the first element
(only-numbers <???>))) ; and advance the recursion
(else ; otherwise
(only-numbers <???>)))) ; simply advance the recursion
Notice that this solution follows a well-known template, a recipe of sorts for recursively processing a list and in turn creating a new list as output. Don't forget to test your procedure:
(only-numbers '(a 1 2 b d 3 5))
=> '(1 2 3 5)
(only-numbers '(1 2 3 4 5))
=> '(1 2 3 5)
(only-numbers '(a b c d e))
=> '()

Resources