Scheme Cons (with #f as a second statement) output - scheme

I was wondering, If
(cons (quote (a b c)) #f)
gives an output
(( a b c ))
Then what output does this give:
(cons (quote (a b c)) #t)
?
Thank you

The first expression will not evaluate to ((a b c)) in most interpreters, it seems that your interpreter is evaluating #f as an empty list:
(cons (quote (a b c)) '())
=> '((a b c))
Having said that, you just substituted a #f with a #t, the standard results will look like this:
(cons (quote (a b c)) #f)
=> '((a b c) . #f)
(cons (quote (a b c)) #t)
=> '((a b c) . #t)
Why don't you try it online? in here for instance.

CommonLisp:
* (if '() 'true 'false)
FALSE
Scheme:
> (if '() 'true 'false)
true
And back in CommonLisp:
* (cons (quote (a b c)) nil)
((A B C))

Related

Omitting parentheses in cond leads to strange result

Consider the following snippet
(define (f a b c)
(
cond ((and (< b c) (< b a)) (+ c a))
((and (< a c) (< a b)) (+ c b))
((and (< c b) (< c a)) (+ b a))
)
)
(display (f 2 1 3)) ; 5
(newline)
(display (f 2 8 3)) ; 11
(newline)
(display (f 2 8 -3)) ; 10
Now if I comment the second line and second line from bottom
(define (f a b c)
;(
cond ((and (< b c) (< b a)) (+ c a))
((and (< a c) (< a b)) (+ c b))
((and (< c b) (< c a)) (+ b a))
;)
)
The result is
#<undef>
11
10
I could not explain why omitting the parentheses lead to that result. In the second case, I expected thtat the complier would treat cond ((and (< b c) (< b a)) (+ c a)), ((and (< a c) (< a b)) (+ c b)) and ((and (< a c) (< a b)) (+ c b)) as three expressions, the latter two invalid, instead it seems they got executed.
Normally cond keyword should raise an exception when it's interpreted.
But, if your interpreter does not throw any error, you are in the case of block statement, in which the evaluation of the last expression provides the result, the other ones are computed only for side-effects. The code reduces to this:
(define (f a b c) ((and (< c b) (< c a)) (+ b a))))

How to compute the number of times pattern in one list appears in other list in Scheme

I am stuck up in a Scheme program for about 5 hours. The program that I am working on should take two lists as input and then compute the number of times the pattern within the first list appears on the second list.
For example : > (patt '(b c) '(a b c d e b c)) ==> answer = 2
(patt '(a b c) '(a b c a b c d e a b c c c)) ==> answer = 3
(patt '((a b) c) '(a b (a b) c d e b c)) ==> answer = 1
Below is the code that I have till now.
(define (patt lis1 lis2)
(cond
((null? lis1) 0)
((null? lis2) 0)
[(and (> (length lis1) 1) (eq? (car lis1) (car lis2))) (patt (cdr lis1) (cdr lis2))]
((eq? (car lis1) (car lis2)) (+ 1 (patt lis1 (cdr lis2))))
(else (patt lis1 (cdr lis2)))
))
Can someone please help me solve this. Thanks!
Consider the subproblem of testing if a list starts with another list.
Then do this for every suffix of the list. Sum up the count of matches.
If you want non overlapping occurrences, you can have the prefix match, return the suffix of the list so that you can skip over the matching part.
Also use equals? for structural equality, not eq? which is for identity.
You need to divide the problem into parts:
(define (prefix? needle haystack)
...)
(prefix? '() '(a b c)) ; ==> #t
(prefix? '(a) '(a b c)) ; ==> #t
(prefix? '(a b c) '(a b c)) ; ==> #t
(prefix? '(a b c d) '(a b c)) ; ==> #f
(prefix? '(b) '(a b c)) ; ==> #t
(define (count-occurences needle haystack)
...)
So with this you can imagine searching for the pattern (count-occurences '(a a) '(a a a a)). When it is found from the first element you need to search again on the next. Thus so that the result is 3 for the (a a a a) since the matches overlap. Every sublist except when it's the empty list involves using prefix?
Good luck!
(define (patt list1 list2)
(let ([patt_length (length list1)])
(let loop ([loop_list list2]
[sum 0])
(if (>= (length loop_list) patt_length)
(if (equal? list1 (take loop_list patt_length))
(loop (cdr loop_list) (add1 sum))
(loop (cdr loop_list) sum))
sum))))
After giving this homework problem a little time to marinate, I don't see the harm in posting additional answers -
(define (count pat xs)
(cond ((empty? xs)
0)
((match pat xs)
(+ 1 (count pat (cdr xs))))
(else
(count pat (cdr xs)))))
(define (match pat xs)
(cond ((empty? pat)
#t)
((empty? xs)
#f)
((and (list? pat)
(list? xs))
(and (match (car pat) (car xs))
(match (cdr pat) (cdr xs))))
(else
(eq? pat xs))))
(count '(a b c) '(a b c a b c d e a b c c c)) ;; 3
(count '((a b) c) '(a b (a b) c d e b c)) ;; 1
(count '(a a) '(a a a a)) ;; 3

Transforming this into an expression

Hi guys i'm wanted to know if i have the correct expression for this picture, if not why please
(a((f(b c))(g h))e)
You're close, but not quite right. It'll be more clear if we build the list structure explicitly using cons; this is more like it:
(cons 'a
(cons (cons (cons 'f
(cons 'b 'c))
(cons 'g
(cons 'h '())))
(cons 'e '())))
=> '(a ((f b . c) g h) e)
Notice that in this part: (f b . c) we have an improper list, because the sublist doesn't end in null.
You answer is incorrect as it doesn't properly express the improper list (f b . c). Also the parentheses around g h are an error.
With dotted pairs the full expression would be:
'(a ((f b . c) g h) e)
Note that '(f b . c) is not the same as '(f (b c)).
See that '(f (b c)) is:
(cons 'f (cons (cons 'b (cons 'c '())) '()))
Rather than what '(f b . c) is:
(cons 'f (cons (cons 'b 'c) '()))
Note the improper list.

how to remove item from list using racket programming

I am working on a racket program where I need to pass an expression in a list, and return the variables used in that list.
Input:
'(A or (B and C))
Output:
'(A B C)
I tried the the below code:
(define Remove
(lambda (L)
(flatten L)))
For input:
'(A or (B and C))
It returns:
'(A or B and C)
Now, I want to remove 'or' and 'and' here and just want '(A B C).
I tried this:
(remove and L)
But it's not working.
I really appreciate some suggestions here.
Here is a possible solution:
(define (rm-and-or lst)
(filter (lambda (x)
(and (not (equal? x 'and))
(not (equal? x 'or))))
(flatten lst)))
(rm-and-or '(A and (B or C))) ==> '(A B C)
Flattens the list and filters 'and & 'or out of the result.

write an expression composed from car and cdr that will return c from the list '(a (b c) (d))

write an expression composed from car and cdr that will return c from the list '(a (b c) (d)).
I am using program called Dr Racket
I tried
(car (car (car (cdr '(a (b c) (d))))))
to get c by it self but it does not work.
The error states:
mcar: contract violation
expected: mpair?
given: b
You have a small error in your code: Notice that you need the second element of the second element (which is an inner list) in the outer list. Your code is stating: retrieve the first element of the first element of the second element of the list:
(car (car (car (cdr '(a (b c) (d))))))
... Which causes an error. What you intended was this:
(car (cdr (car (cdr '(a (b c) (d))))))
Let's look at it step by step:
(cdr '(a (b c) (d))) ; => '((b c) (d)) : rest of the outer list
(car (cdr '(a (b c) (d)))) ; => '(b c) : second element of the outer list
(cdr (car (cdr '(a (b c) (d))))) ; => '(c) : rest of the inner list
(car (cdr (car (cdr '(a (b c) (d)))))) ; => 'c : second element of the inner list
(cadadr '(a (b c) (d)))
cadadr is syntactic sugar for (car (cdr (car (cdr ...)))

Resources