SCHEME- comparing lists and returning only the values that do not appear in the second list - list

I am having a hard time debugging this code and was hoping somebody might be able to help with pseudocode or examples of similar problems. Thank you ahead of time!
Define a non-recursive Scheme procedure (remove-them2 lst n) which returns a list of all elements of lst that do not appear in the list n.
Here is what I have so far:
(define (remove-them2 lst n)
(if (null? lst)
n
(let ((tail (remove-them2 cons((cdr lst) (cdr n)))))
(if (member (car lst) (car n) tail)
tail
(cons (car lst) (car n) tail)))))

I'm afraid there are several syntax errors in your solution, please take some time to study how to write procedures in Scheme and how to invoke procedures. To name a few:
We don't call a procedure like this: cons(...), the correct way is (cons ...)
cons expects two arguments, but you're passing three
remove-them2 expects two arguments, but you're passing one
member expects two arguments, but you're passing three
The way you're building the output list is incorrect
We don't need to ask for (car n), we're interested in checking if an element is a member of the whole n list
There's no need to have a tail variable here, it's easier if we just call the recursion directly; and anyway you're using tail incorrectly.
It's not clear what algorithm you tried to implement, but the actual solution is simple - although we'll have to rewrite the code from scratch to make it work:
(define (remove-them2 lst n)
(if (null? lst)
'()
(if (member (car lst) n)
(remove-them2 (cdr lst) n)
(cons (car lst) (remove-them2 (cdr lst) n)))))
EDIT: It's also possible to write a solution without using recursion:
(define (remove-them2 lst n)
(filter (lambda (e) (not (member e n)))
lst))
Either way, it works as expected:
(remove-them2 '(1 2 3 4 5 6 7) '(1 3 5))
=> '(2 4 6 7)

Without knowing which subset of the language you are allowed to use it's hard to answer this reasonably.
I'll assume that you want something which is not syntactically recursive (so you can't use a function with an accumulator: I'm not sure what the right word is for this), you are allowed to use map and append, but you're not allowed to use filter as that seems like such an immediate answer.
With those constraints here is how you can do this:
(define (remove-them2 l baddies)
(apply append (map (lambda (e)
(if (memv e baddies)
'()
(list e)))
l)))
I think this is pretty artificial and horrible, but it works (in languages like CL it would not portably work however, which is why I think it's horrible).

Related

Recursively adding to a list using Scheme

I'm brand new to functional programming, and relatively new to programming as a whole. So I'm pretty lost trying to understand the syntax of Scheme. But this is a pretty simple question. I'm trying to create a function that recursively fills and prints a list from numbers x to y. Between recursion and this being a new language for me, I'm quite stuck.
(define (gen-list x y)
(if (> start end)
'()
(append '() (gen-list ((+ 1 x) y)) ) ))
If I were to enter in (gen-list 1 5) I would expect the result to be 1 2 3 4 5. This is currently giving me an error of "application: not a procedure" when it tries to call itself again. I have gotten around the error, but not been able to print anything remotely like what I'd like to. Any help is appreciated.
You have a couple of errors:
The parameters are called x and y, but you refer to them as start and end (I'd suggest to use start and end instead, they make the code easier to understand.)
You have more parentheses than needed in the last line. This is very important and an endless source of confusion for beginners. Don't surround all expressions with () unless you want to call a procedure.
We recursively build new lists with cons, append is for concatenating existing lists.
You're not actually using start, which is the current element in the recursion, to build the new list - you're just appending empty lists.
A list is an element consed to another list, or the empty list '(). That's why we return '() in the base case. For example, this is how a two-element list looks like: (cons 1 (cons 2 '())).
With all said and done, this is the proper way to build our list:
(define (gen-list start end)
(if (> start end)
'()
(cons start
(gen-list (+ start 1) end))))
As a final comment: the above procedure already exists in Racket, you don't need to rewrite it. Read about range in the documentation.
One of the problems with the 'obvious' answer to this question is that it doesn't really work very well. Consider this:
(define (gen-list start end)
(if (> start end)
'()
(cons start
(gen-list (+ start 1) end))))
Well, if start is much less than end there are going to be a huge number of recursive calls on the stack, because this is a properly recursive function: the recursive call to gen-list is a real call and has to return before the call to cons (which is a tail call) can happen.
The way to deal with this is to turn patterns which look like (cons x (<recursive-call> ...)) into patterns which look like (<tail-call> ... (cons x ...)): you need a function with an extra argument, an accumulator. This means that the calls which were previously recursive are now tail calls and everything is therefore good: the process is now iterative.
The problem with this is that lists come out backwards (you need to think about why this is, but it's obvious after a bit of thought). So you then need to reverse the result. Fortunately reversing a list is also an iterative process, so that's OK.
But in this case, well, you can just count backwards! So a simple-minded approach looks like this, using a locally-defined auxiliary function (this can be defined as a top-level function, but why bother?):
(define (gen-list low high)
(define (gla i result)
(if (< i low)
result
(gla (- i 1) (cons i result))))
(gla high '()))
You can see this is counting backwards: the initial call to gla starts with high & then constructs the list backwards. So, now:
> (gen-list 1 3)
'(1 2 3)
As we want.
This is such a common pattern in Scheme that there is a special construct for it: named let. So we can rewrite the above more idiomatically as:
(define (gen-list low high)
(let gla ([i high] [result '()])
(if (< i low)
result
(gla (- i 1) (cons i result)))))
This is exactly the same as the previous answer: it just moves the initial call to the top and combines it with the local definition of gla. This is probably the idiomatic Scheme way to do something like this (although people who write more Scheme than me might differ: I'm really a CL person and have inevitably poor taste).
This is where the story should end, but I can't resist adding the following. In the bad old days of comp.lang.lisp, people used to ask obvious homework questions and since there was no karma system one approach was to give an answer which solved the problem ... while being absurdly opaque.
So first of all we can turn gla into a function which gets passed a continuation to call rather than knowing it must call itself:
(define (gen-list low high)
(let ([gla (λ (cont i result)
(if (< i low)
result
(cont cont (- i 1) (cons i result))))])
(gla gla high '())))
And then, of course we can turn (let ([x y]) ...) into ((λ (x) ...) y):
(define (gen-list low high)
((λ (gla)
(gla gla high '()))
(λ (cont i result)
(if (< i low)
result
(cont cont (- i 1) (cons i result))))))
And that's a nice, pure answer ... which no student would ever come up with.
An alternative approach which is even more malicious is just to explicitly use the Y combinator, of course.
Just adding tail call recursive version
(define (gen-list start end (acc '()) #:step (step 1))
(cond ((> start end) (reverse acc))
(else (gen-list (+ start step) end (cons start acc)))))
Personally I love cond because you have all the conditions then below each other (or an else) - this is the style of The little Schemer a very good book for learning recursive thinking.
You need more than just the (> start end) base case, you also need a (= start end) base case in which you return (list start)

Clojure - Recursive up function

Hi I'm kind of new to clojure and I'm trying to write a function called up that removes a pair of parentheses from each top level element of a list. If the top level element is not a list, then it is added as well. For example,
>(up '((1 2) (3 4)))
(1 2 3 4)
>(up '(x (y) z))
(x y z)
Right now, I'm having a problem with the function ending too soon if I'm trying to remove one pair of parentheses. I want to do this recursively and without the help of other functions if possible. What I have at the moment:
(defn up [lst]
(if (empty? lst)
()
(if (list? (first lst))
(up (first lst))
(cons (first lst) (up (rest lst))))))
I know that the problem is that I am cons-ing an empty list with the last element of a nested list which ends my function, but I can't figure out how else to do it.
Diego's comment seems to indicate there were other answers here, but I don't see them now, so here goes...
Your function ends too soon because, when it hits an item that is itself a list, it recursively calls up on that item and ignores the rest of the items in the original list. (up (first lst))
The minimal change to your code would be to, instead, recursively call up on the concatenation of that first list item and the rest of the list. (up (concat (first lst) (rest lst)))
Even better would be to use the existing core function flatten instead of up.
On a side note, you would generally want to achieve recursion using recur instead of calling up directly, in order to avoid a stack overflow for large input lists.

Why cannot find `when` form when I use neil/sicp?

I was solving exercise-3.23 of section 3.3.2 of SICP,
which is an implementation of deque.
As Racket doesn't support set-car! and set-cdr!,so I use #lang planet neil/sicp from SICP Support for DrRacket,and wrote the code:
#lang planet neil/sicp
(define (make-deque) (cons '() '()))
(define (empty-deque? dq) (null? (front-ptr dq)))
;;; just here, I use when form
(define (front-delete-deque! dq)
(cond ((empty-deque? dq)
(error "FRONT-DELETE-DEQUE! called with empty deque" dq))
(else
(set-car! dq (caddr (front-ptr dq)))
(when (null? (front-ptr dq))
(set-cdr! dq '())))))
(define (front-ptr dq) (car dq))
(define (rear-ptr dq) (cdr dq))
I got an error: when: unbound identifier in module, which is very strange.
I think it has something to do with the neil/sicp, since Racket has when form.
And can someone explain what exactly #lang planet neil/sicp means and matters?
PS: forget how I implement the deque.
It's quite possible that the implementors of the neil/sicp package chose not to support when - after all, it's not part of the standard language and (as far as I can remember) it was never mentioned in SICP. But fear not, when is not essential and you can write something equivalent, just substitute this part:
(when (null? (front-ptr dq))
(set-cdr! dq '()))
With this:
(if (null? (front-ptr dq))
(set-cdr! dq '())
#f)
And if you're feeling bold you can even write your own when macro, one of the nice things of Scheme is that you can extend its syntax. Left as an exercise for the reader.
SICP is beautiful stuff; beautifully designed to work with Scheme, plain and simple. Rather than dealing with the idiosyncrasies of Racket, such as not having set-car! apparently, why not just choose a proper Scheme?
There are different Scheme standards. The most recent, R7RS, defines when and provides a definition as:
(define-syntax when
(syntax-rules ()
((when test result1 result2 ...)
(if test
(begin result1 result2 ...)))))
If you use a Scheme based on R5RS or R6RS they will either already define when or allow you to define it as above.

Scheme list counting function

I'm trying to write a Scheme function that counts all the items in a list, but unlike the length function that is available would count inner lists also so countAll '(a (a b)) would return 3 and not 2.
the first check is for an empty list, the second is supposed to check if the head of the list is currently a list itself, it should then add the length of THAT list to the total and call the next recursive call, if it isn't, it should simply skip to the third part which will add one to the total and call the function recursively.
I'm getting syntactical errors and I'm unsure about my approach. Am I going about this the right way? Are there better/Easier ways to do this?
(define countAll
(lambda (list)
(if (null? list)
0
((if (list? (car list)
(+ length (car list)
(countAll (cdr list))))))
(+ 1
(countAll (cdr list))))))
(+ 1
(countAll(cdr list)
)))))
You've made a mess of your parentheses. I would strongly advise rewriting this using cond, as in
(cond
((null? lst) 0)
((list? (car lst)) (???))
(else (???)))
This will make it easier to see what you are doing and much less likely that you mess up your parentheses. Nested ifs really are something to avoid except in the simplest of circumstances.
Your approach is nearly right, but you've missed something. You have made a big assumption about what happens when (list? (car lst)) evaluates to true. Can you think what it is? If you think about it hard enough, you'll realise that you cannot use the length function here.
The solution to this kind of problem follows a well-known structure, a recipe if you wish, for traversing a list of lists. This looks like homework, so I'll help you with the general idea and you go ahead filling-in the blanks:
(define (countAll lst)
(cond ((null? lst) ; if the list is empty.
<???>) ; then it doesn't have any elements
((not (list? (car lst))) ; if the first element in the list is not a list
(<???> (countAll <???>))) ; add one and advance the recursion over the `cdr`
(else ; otherwise
(+ (countAll <???>) ; add the result of the recursion over the `car`
(countAll <???>))))) ; with the result of the recursion over the `cdr`
If you need more help understanding how to structure a solution to this kind of problems dealing with lists of lists, I'd recommend you take a look at either The Little Schemer or How to Design Programs, both books will teach you how to grok recursive processes in general.
if you want to count elements of nested list you can use the deep recursion function as some people answer here. or what I do is use racket function (flatten) to make the nested list flat then use recursion on level 1 list as in the following code
(define (howMany ls)
(if (null? ls)
0
(+ 1 (howMany (cdr ( flatten ls )))))) ; flat the nested list and deal with it as a level 1 list.

Scheme matching elements

I'm currently playing a bit around with scheme, and I've been stuck for quite a while trying to figure out how to implement a test, that checks if any element in 'xs' is also stored in 'l'.
That is the mathematical function '\in', just for each element in the list xs.
Do you want to write it yourself for practice, or do you just need the functionality?
If you just want the functionality then use the answer by larsmans (I have never used that, but it looks good to me).
To implement try the following (disclaimer: I have not tested this)
(define has-intersect?
(lambda (xs l)
(cond
[(null? xs) #f]
[(member? (car xs) l) #t]
[else (has-intersect? (cdr xs) l)])))
In mathematical terms, you're checking whether the intersection of two lists/sets is non-empty. If you have an SRFI-1 library, this is trivial:
(not (null? (lset-intersection l xs)))
An implementation of SRFI-1 can be found in SLIB.
(Disclaimer: I'm a former SLIB contributor.)