The object ___ is not applicable [duplicate] - scheme

This question already has answers here:
"application: not a procedure" in binary arithmetic procedures
(1 answer)
Getting every nth atom using scheme does not pick up the last atom
(3 answers)
Closed 8 years ago.
Hi I'm writing something that grabs every third element after the first element. However I can't test the logic due to ";The object (a b c d e f g) is not applicable." The code is below, atom? checks if it's a list, listful is defined as an empty list.
(DEFINE (threes var)
(if(atom? var)
((newline) (DISPLAY "Bad input") ))
(APPEND(listful (CAR var)))
(if(> 3 (length var))
(threes (cdddr(listful)))
(listful))
)
Can anyone give me some tips? Here is how I'm calling the method in the Scheme environment.
>(threes (list1))
>(threes '(A B C D E F))

An if can only have one expression as the consequent, and one as the alternative. If you need more than one you have to use a begin to enclose the sequence of expressions - surrounding the expressions with () won't work, and that's causing the error reported, because Scheme interprets () as function application. This would be the correct syntax:
(define (threes var)
(if (atom? var)
(begin
(newline)
(display "Bad input")))
(append (listful (car var)))
(if (> 3 (length var))
(begin
(threes (cdddr (listful)))
(listful))))
… However, that's unlikely to work. What you want to do has been asked a couple of times in the last days, in particular here is my own previous answer:
(define (threes lst)
(cond ((or (null? lst) (null? (cdr lst))) lst)
((null? (cdr (cdr lst))) (list (car lst)))
(else (cons (car lst)
(threes (cdr (cdr (cdr lst))))))))
For example:
(threes '(a b c d e f g h i j k l m n o p))
=> '(a d g j m p)
See the original question for other ways to solve the problem, with detailed explanations.

Related

How to rotate a list that goes in the right direction

I want to be able to make my list go one step to the right. Here's my example code that makes it go one step to the left.
(define (rotate-L lst) (append (cdr lst) (list (car lst))))
(rotate-L '(a b c))
(b c a)
I'm having trouble understanding why when I make it backward, i get an error
(define (rotate-L lst) (append (car lst) (list (cdr lst))))
(rotate-L '(a b c))
SchemeError: argument 0 of append has wrong type (string)
Current Eval Stack:
0: (rotate-L (quote (a b c)))
(define rotr
(lambda (l)
(if (null? l)
l
((lambda (s) (s s l cons))
(lambda (s l c)
(if (null? (cdr l))
(c (car l) '())
(s s (cdr l)
(lambda (f r)
(c f (cons (car l) r))))))))))
(define rotl
(lambda (l0)
(if (null? l0)
l0
((lambda (s) (s s (cdr l0) (lambda (r) r)))
(lambda (s l c)
(if (null? l)
(c (list (car l0)))
(s s (cdr l)
(lambda (r)
(c (cons (car l) r))))))))))
Here is a test:
(rotr '())
(rotr '(a))
(rotr '(a b))
(rotr '(a b c))
(rotl '())
(rotl '(a))
(rotl '(a b))
(rotl '(a b c))
whose output is so:
1 ]=> (rotr '())
;Value: ()
1 ]=> (rotr '(a))
;Value: (a)
1 ]=> (rotr '(a b))
;Value: (b a)
1 ]=> (rotr '(a b c))
;Value: (c a b)
1 ]=> (rotl '())
;Value: ()
1 ]=> (rotl '(a))
;Value: (a)
1 ]=> (rotl '(a b))
;Value: (b a)
1 ]=> (rotl '(a b c))
;Value: (b c a)
It's worth remembering what lists look like as structures made of conses:
(1 2 3 4) looks like this, for instance
So pretty obviously (car '(1 2 3 4)) is a number, not a list, and so (append (car '(1 2 3 4)) ...) is not going to work, because append wants its arguments to be lists. But even if you fix this to be correct, you're not doing what you think:
(define (not-rotating lst)
(append (list (car lst)) (cdr lst)))
This simply takes the first element of the list, makes a single-element list from it, and appends the rest of the list to it. Well, you could write this more easily:
(define (not-rotating lst)
(cons (car lst) (cdr list)))
and it should be clear that this is doing nothing at all useful.
To rotate the list 'right' you need to:
find the last element of the list;
construct a list which is all of the original list except for that element;
and glue the last element onto the start of that list.
The only way to find the last element of the list is to walk along the list until you get to it. And you also need to make a copy of all the elements of the list but the last one, which also means walking down the list. It is natural to do these two operations at the same time:
walk down the list, building up a copy of it;
when you get to the last element, attach it to the start of the copy.
Here is a terrible way of doing this:
(define (rotate-r/terrible lst)
(let rrl ((tail lst)
(building '()))
(if (null? (cdr tail))
;; we've got to the end
(cons (car tail) building)
(rrl (cdr tail)
(append building (list (car tail)))))))
This is terrible because at each step it appends the current element to the end of the list it is building. That's terrible for not one but two reasons:
appending two lists takes time proportional to the length of the first list;
appending two lists requires a complete copy of the first list to be made.
That makes this both quadratic in the length of the list, which is terrible, and also very 'consy' – it allocates lots of ephemeral storage. But it does work.
So: there is, not surprisingly a better way of doing this: a way which is both linear and makes no more than two copies of the list. I'm not going to give it here, but it's surprisingly close to the above terrible solution. Two hints:
what is the natural way to accumulate elements into a list? is it at the start, or at the end of a list?
how do you turn the list you accumulated like that into the list you want?

Remove all duplicates from a list in scheme [duplicate]

This question already has an answer here:
Removing all duplicate members from a list in scheme
(1 answer)
Closed 4 years ago.
Write a SCHEME function remove-duplicates that removes all duplicates from a list. (Hint: you might start by defining a function which removes all duplicates of a particular given value v from a list; then what?)
Example:
(remove-duplicates ’(1 2 3 4 5 3 1)) should produce
’(1 2 3 4 5)
I have this so far:
(define (remove v l)
(if (null? l)
l
(if (equal? v (car l))
(cdr l)
(cons (car l)
(remove v (cdr l))))))
(define (remove-duplicates l)
(cond ((null? l) '())
((null? (cdr l)) l)
((equal? (car l) (car (cdr l)))
(remove (cdr (cdr l)) (remove-duplicates (cdr (cdr l)))))
(else (cons (car l) (remove-duplicates (cdr l))))))
The whole point of abstraction is that you can have simpler code that does some of the job which is testable. Have you tested remove on its own?
(remove 'a '(g a b c a b c a))
; ==> (g b c a b c a)
So when it find one match the rest of the list is (cdr l). Are you just expecting one duplicate? This is the most important part of your solution.
When it comes to remove-duplicates you do too much. When the list is not empty the first element is the first element of the result consed with the recursion with the argument being the rest of argument with the first element removed. ie. (cons (car l) (remove-duplicates (remove (car l) (cdr l)). No more comparing necessary.

Scheme collecting similar items in a list and finding most common item in a list [duplicate]

This question already has answers here:
Count occurrence of element in a list in Scheme?
(4 answers)
Closed 8 years ago.
I want to make a function that occurs how many times an element occurs in a list. For example in the list: '(a b c b b c c a) I want it to return a nested list with:
'((a 2) (b 3) (c 3))
I know the function will look sort of like this:
(define collect-similar
(lambda (elm ls)
(cond
[(null? ls) '()]
[(equal? elm (car ls))]
I know that I need to continue checking through the list until it arrives back at the base case of the null list and I can check the rest of the list by using cadr. But I'm not too sure on how to get the value and how to make it return the nested list.
The next function I'm trying to write finds the most common element in the list. For example running the function on the list '(a a a a a b c) will simply return a. I know I can make use of the collect-similar function and find which number is the highest.
This has been asked before, just adapt one of #ChrisJester-Young's bagify implementations. For example:
(define (collect-similar lst) ; a slightly modified `bagify`
(hash->list
(foldl (lambda (key ht)
(hash-update ht key add1 0))
'#hash()
lst)))
(collect-similar '(a b c b b c c a))
=> '((a . 2) (b . 3) (c . 3))
With collect-similar in place, it's simple to find the most common element:
(define (most-common lst)
(let loop ((alst (collect-similar lst)) ; use previous procedure
(maxv '(#f . -inf.0)))
(cond ((null? alst) (car maxv))
((> (cdar alst) (cdr maxv))
(loop (cdr alst) (car alst)))
(else
(loop (cdr alst) maxv)))))
(most-common '(a a a a a b c))
=> 'a

Help explaining how `cons` in Scheme work?

This is the function that removes the last element of the list.
(define (remove-last ll)
(if (null? (cdr ll))
'()
(cons (car ll) (remove-last (cdr ll)))))
So from my understanding if we cons a list (eg. a b c with an empty list, i.e. '(), we should get
a b c. However, testing in interaction windows (DrScheme), the result was:
If (cons '() '(a b c))
(() a b c)
If (cons '(a b c) '())
((a b c))
I'm like what the heck :(!
Then I came back to my problem, remove all elements which have adjacent duplicate. For example,
(a b a a c c) would be (a b).
(define (remove-dup lst)
(cond ((null? lst) '())
((null? (cdr lst)) (car lst))
((equal? (car lst) (car (cdr lst))) (remove-dup (cdr (cdr lst))))
(else (cons (car lst) (car (cdr lst))))
)
)
It was not correct, however I realize the answer have a . between a b. How could this happen?
`(a . b)`
There was only one call to cons in my code above, I couldn't see which part could generate this .. Any idea?
Thanks,
cons build pairs, not lists. Lisp interpreters uses a 'dot' to visually separate the elements in the pair. So (cons 1 2) will print (1 . 2). car and cdr respectively return the first and second elements of a pair. Lists are built on top of pairs. If the cdr of a pair points to another pair, that sequence is treated as a list. The cdr of the last pair will point to a special object called null (represented by '()) and this tells the interpreter that it has reached the end of the list. For example, the list '(a b c) is constructed by evaluating the following expression:
> (cons 'a (cons 'b (cons 'c '())))
(a b c)
The list procedure provides a shortcut for creating lists:
> (list 'a 'b 'c)
(a b c)
The expression (cons '(a b c) '()) creates a pair whose first element is a list.
Your remove-dup procedure is creating a pair at the else clause. Instead, it should create a list by recursively calling remove-dup and putting the result as the second element of the pair. I have cleaned up the procedure a bit:
(define (remove-dup lst)
(if (>= (length lst) 2)
(if (eq? (car lst) (cadr lst))
(cons (car lst) (remove-dup (cddr lst)))
(cons (car lst) (remove-dup (cdr lst))))
lst))
Tests:
> (remove-dup '(a b c))
(a b c)
> (remove-dup '(a a b c))
(a b c)
> (remove-dup '(a a b b c c))
(a b c)
Also see section 2.2 (Hierarchical Data and the Closure Property) in SICP.
For completeness, here is a version of remove-dup that removes all identical adjacent elements:
(define (remove-dup lst)
(if (>= (length lst) 2)
(let loop ((f (car lst)) (r (cdr lst)))
(cond ((and (not (null? r))(eq? f (car r)))
(loop f (cdr r)))
(else
(cons (car lst) (remove-dup r)))))
lst))
Here in pseudocode:
class Pair {
Object left,
Object right}.
function cons(Object left, Object right) {return new Pair(left, right)};
So,
1. cons('A,'B) => Pair('A,'B)
2. cons('A,NIL) => Pair('A,NIL)
3. cons(NIL,'A) => Pair(NIL,'A)
4. cons('A,cons('B,NIL)) => Pair('A, Pair('B,NIL))
5. cons(cons('A 'B),NIL)) => Pair(Pair('A,'B),NIL)
Let's see lefts and rights in all cases:
1. 'A and 'B are atoms, and whole Pair is not a list, so (const 'a 'b) gives (a . b) in scheme
2. NIL is an empty list and 'A is an atom, (cons 'a '()) gives list (a)
3. NIL and 'A as above, but as left is list(!), (cons '() 'a) gives pair (() . a)
4. Easy case, we have proper list here (a b).
5. Proper list, head is pair (a . b), tail is empty.
Hope, you got the idea.
Regarding your function. You working on LIST but construct PAIRS.
Lists are pairs (of pairs), but not all pairs are lists! To be list pair have to have NIL as tail.
(a b) pair & list
(a . b) pair not list
Despite cons, your function has errors, it just don't work on '(a b a a c c d). As this is not related to your question, I will not post fix for this here.

Scheme Programming finding items in nested loops

I have the following items
(define itemslist
(list 'a1 'b2 'c3 (list 'z1 'z2) 'd5 'e6))
My method to find items is below
(define find-item
(lambda (item itemslist)
(cond ((null? itemslist) #f)
((list? (car itemslist))
(cond ((null? itemslist) #f)
(else (find-item item (car itemslist)))))
((equal? stn (car itemslist)) (display "found"))
(else (find-stn stn (cdr itemslist)))
)
)
)
With my method above I can find a1, b2, c3, z1, z2. But when I want to find d5 onwards, it returns nothing. It seems to have skip the stack. Btw, I am just starting to learn Scheme so simple explanation would be better.
One more qns, how about if I have this
(list 'a1 'b2 'c3 (list 'z1 (list 'y1 'y2) 'z2) 'd5 'e6)
does this works as well? Thanks!
Yes, you skip lists.
Example:
'(1 '(2 3) 4)
If (list? '(2 3)) is true, the else part (outer cond) wont be evaluated so 4 is skipped.
So, either you place the else part inside list? block or you redesign your code.
If the itemlist is a list/pair you can pass it right away in a recursive call so
you can avoid writing code like (car itemslist) inside predicates thus make the code simple.
Here is a redesigned (simplified) version:
(define (find-item item l)
(cond ((equal? item l) #t)
((pair? l) (or (find-item item (car l)) (find-item item (cdr l))))
(else #f)))
Tip: you can also can write lists with '(...) notation instead of (list ...), ie
(find-item 'z2 '('a1 'b2 'c3 '('z1 '('y1 'y2) 'z2) 'd5 'e6))
#t
(find-item 'e6 '('a1 'b2 'c3 '('z1 '('y1 'y2) 'z2) 'd5 'e6))
#t
To write more for the sake of writing more... This is usually called a "tree-walk" because a nested list like that is really a binary tree. Lists are really made up of binary pairs, so when you have a pair (l . r), l is the left branch of the tree and r is the right branch of the tree.
For example, '(1 (2 3) 4) is short for '(1 . ((2 . (3 . ())) . (4 . ()))) which can be drawn as
.
/ \
1 .
/ \
/ .
/ / \
. 4 ()
/ \
2 .
/ \
3 ()
and at any pair (l . r), car gets you the left tree and cdr gets you the right. So, when you write (pair? ls), you're really asking if you're at a branch in the tree, at which point you should recur on both the left branch (car) and the right branch (cdr). Hope that helps you understand lists.
Even though you got your [specific] answer, here's something that might help you with similar questions.
(define describe
(lambda (e)
(cond #;((list? e)
`(list ,#(map describe e)))
((pair? e)
`(cons ,(describe (car e))
,(describe (cdr e))))
((vector? e)
`(vector ,#(map describe (vector->list e))))
((or (null? e) (symbol? e)) `',e)
(else e))))
This procedure prints the code that generates a given sexpr. For example:
> (describe '(a 2 b 3))
(cons 'a (cons 2 (cons 'b (cons 3 '()))))
It will also place the quotes where needed, so it places them before symbols or () but not before numbers. When you are comfortable with nested pairs, you may want to remove the #; on the third line, to generate more compact output:
> (describe '(a 2 b 3))
(list 'a 2 'b 3)
This code can teach you many things about quote. For example:
> (describe ''''a)
(list 'quote (list 'quote (list 'quote 'a)))

Resources