boolean function for a sublist in scheme - list

I need make a boolean function for evaluating two list,for example:
(define list1 '((1 2) (4 5) (8 6) (2 8)))
(define list2 '((1 2) (8 6)))
list2 is a sublist of list1, and must returned #t, but I don't know how do it, I try with this function for comparing two list
(define (sublist? lst1 lst2)
(if (null? lst2)
#f
(if(list? (car lst2))
(sublist? lst1 (car lst2))
(if (and(equal? car(lst1) (car lst2)) (equal? cdr(lst1) (car lst2)))
#t (sublist? lst1 (cdr lst2))))))
help :(

This sublist? behaves as a "subset?".
; sublist? : list list -> "boolean"
; return non-false if all elements of xs are in ys
(define (sublist? xs ys)
(or (null? xs) ; the empty list is a sublist of ys
(and ; for a non-empty list
(member (car xs) ys) ; the first element must be in ys
(sublist? (cdr xs) ys)))) ; and the rest of the elements as well.
This sublist? behaves as a "substring?"
; prefix? : list list -> boolean
; is xs a prefix of ys?
(define (prefix? xs ys)
(or (null? xs)
(and (equal? (car xs) (car ys))
(prefix? (cdr xs) (cdr ys)))))
; sublist? : list list -> boolean
; is xs a consecutive sublist of ys?
(define (sublist? xs ys)
(or (null? xs)
(prefix? xs ys)
(and (not (null? ys))
(prefix? xs (cdr ys)))))

A suggested solution:
(define list1 '((1 2) (4 5) (8 6) (2 8)))
(define list2 '((4 5) (8 6)))
(define (sublist? lst1 lst2)
(if (null? lst2)
#t
(if (and (null? lst1)
(not (null? lst2)))
#f
(if (list? (car lst2))
(or (and (sublist? (car lst1) (car lst2))
(sublist? (cdr lst1) (cdr lst2)))
(sublist? (cdr lst1) lst2))
(if (eq? (car lst1) (car lst2))
(sublist? (cdr lst1) (cdr lst2))
(sublist? (cdr lst1) lst2))))))
(sublist? list1 list2)
Explanation:
This is (not) simply handling all the edge-cases:
- If list2 is null - it is always a sublist of list1
- If we got to the end of list1 and list2 is not yet found - return false
- If (car list2) is a list - we need to check recursively two cases: if (car list1) equals to (car list2) or if (car list2) is somewhere else in (cdr list1)
- If (car list1) and (car list2) are the same - we'll check recursively with the rest of both lists: (cdr lst1) and (cdr lst2)
This answer was tested here

Related

How to reverse a list - scheme

(define (oddrev ls)
(cond ((null? ls) ls)
((null? (cdr ls)) ls)
(else (cons (car ls) (oddrev (cdr (cdr ls)))))))
I have a scheme that returns odd array elements but I want to reverse the list at the end.
How would I do that??
The trivial solution is to add a layer of indirection:
(define (oddrev-helper ls)
(cond ((null? ls) ls)
((null? (cdr ls)) ls)
(else (cons (car ls) (oddrev-helper (cdr (cdr ls)))))))
(define (oddrev ls) (reverse (oddrev-helper ls)))
This looks wasteful, but is much more efficient than repeatedly appending a singleton list at the end.
But if you have encountered the way to transform a recursive process to an iterative by way of an accumulator and tail recursion, you will have noticed that list procedures have a habit of producing the result in reverse.
You can take advantage of this if you actually want the result in reverse.
(define (oddrev-helper ls acc)
(cond ((null? ls) acc)
((null? (cdr ls)) (cons (car ls) acc))
(else (oddrev-helper (cdr (cdr ls)) (cons (car ls) acc)))))
(define (oddrev ls) (oddrev-helper ls '()))
By tail recursion (and leaving out the usual reverseing at the end when returning.
(define (oddrev ls (acc '()))
(cond ((or (null? ls) (null? (cdr ls))) acc)
(else (oddrev (cdr (cdr ls)) (cons (car ls) acc)))))
Or by standard higher order functions:
(define (oddrev ls)
(reverse (filter odd? ls)))
Try out:
(oddrev '(1 2 3 4 5 6))
;; '(5 3 1)
change the line
(cond ((or (null? ls) (null? (cdr ls))) acc)
to:
(cond ((or (null? ls) (null? (cdr ls))) (reverse acc))
to let it return in the input order: '(1 3 5)

Improper vs. proper list in Scheme

The original code I try to implement.. the output should be (1.4) (2.5) from my code.. I think you all know what I try to do....this is also tail recursion practice
my code
(define (myFunc lst1 lst2)
(if (or (null? lst1) (null? lst2))
'()
(list (cons (car lst1) (car lst2))
(myFunc (cdr lst1) (cdr lst2)))
))
after several of you gave me good advice about cons-pair.. so now it get's the dotted symbol in the middle.. then problem is that the improper list with empty list in the end..
when 2 input lists are like this ..... '(1 2 3 4) '(4 5 6))
my output is like this ; ((1 . 4) ((2 . 5) ((3 . 6) ())))
the empty list in the end of output shouldn't be there... so I couldn't understand about improper list , proper list....? is there are any document, I can look at?
Consider the difference between cons and list:
That is, (cons a b) creates a cell whose car is a and cdr is b.
(list a b) creates a cell whose car is a, but the cdr is a list, and the car of that list is b, while its cdr is nil.
If b is a list, the one on the left will be a list which has b as its tail, and with a added at the front of b.
The one on the right will also be a list, but one which has b as its second element, not as its tail like you want.
To fix your program, you only need to replace your list with a cons.
But your function is not tail-recursive, because it does things with the result of the recursive call.
To make it tail-recursive, a good way is usually to make a helper function which has an accumulator parameter.
I would probably write it something like this:
(define (zip-cars l1 l2)
(cons (car l1) (car l2)))
(define (zip-help l1 l2 result)
(if (or (null? l1) (null? l2))
result
(zip-help (cdr l1) (cdr l2) (cons (zip-cars l1 l2) result))))
(define (zip l1 l2)
(zip-help l1 l2 '()))
Just replace list with cons. Then your code will evaluate to `(cons (cons (cons .... (cons ... '())) and your list will be properly terminated.
(define (zip lst1 lst2)
(if (or (null? lst1) (null? lst2))
'()
(cons (cons (car lst1) (car lst2))
(zip (cdr lst1) (cdr lst2)))))
then
(zip '(1 2 3 4) '(4 5 6))
=> '((1 . 4) (2 . 5) (3 . 6))
This is not tail-recursive, though, since after returning from zip the consing still has to be done.
EDIT
An example of a tail-recursive version:
(define (zip lst1 lst2)
(let loop ((lst1 lst1) (lst2 lst2) (res '()))
(if (or (null? lst1) (null? lst2))
(reverse res)
(loop (cdr lst1)
(cdr lst2)
(cons (cons (car lst1) (car lst2)) res)))))

Scheme : Make operations on a list without changing its copy?

I have a helper to copy a list :
(define (list-copy list)
(if (null? list)
'()
(cons (car list) (list-copy (cdr list)))))
Then,
(define (multList lst1 lst2)
(define lstCopy2 (list-copy lst2))
(cond ((null? lst1) ())
((eq? (length lst1) (length lst2)) (cons (* (car lst1) (car lst2)) (multList (cdr lst1) (cdr lst2))))
((> (length lst1) (length lst2))
(if (null? lst2) lstCopy2
(cons (* (car lst1) (car lst2)) (multList (cdr lst1) (cdr lst2)))))
(else '())))
I'm trying to copy lst2 into lstCopy2 and then I would like lstCopy2 to stay intact when I'm working on lst2 in order to call lst2 (with the help of lstCopy2) as it was at first.
In my 3rd cond (when lenght lst1 > lenght lst2) when lst2 = () I would like to continue the process until lst1 is ().
Thanks for your help
As far as I can see your copy is not used. In addition, none of the procedure mutates anything so (equal? lst2 lstCopy2) will always be #t for every recursion / element of the list.
If lst1 is shorter than lst2 or you've reached the end of the list you will get an error since () is an illegal expression. perhaps you meant '()?
This could have been written a lot easier:
(require srfi/1)
(define (multList lst1 lst2)
(define (aux lst1 lst2)
(if (null? lst2)
'()
(cons (* (car lst1)
(car lst2))
(aux (cdr lst1) (cdr lst2)))))
(aux (apply circular-list lst1) lst2))
(multList '(1 -1) '(1 2 3 4 5 6)) ; ==> (1 -2 3 -4 5 -6)
;; with srfi-1-version of map we can use it to make the code shorter
(define (multList lst1 lst2)
(map * (apply circular-list lst1) lst2))
You're going about this in a very odd way. The standard way to do this is the define an inner function. Usually you inner function and then call it.
The way you were doing it you were making a copy of lst2 every time you called multList, which is not what i think you want.
But I don't see where you actually reference the original 2nd list so I don't see the reason behing what you want to do.
(define (multList oL1 oL2)
(define (helper lst1 lst2)
(cond ((null? lst1) '())
((null? lst2) (helper lst1 oL2))
(else
(cons (* (car lst1) (car lst2))
(helper (cdr lst1) (cdr lst2))))))
(helper oL1 oL2))
(multlist '(9 2 4) '(1 2))
;Value 14: (9 4 4)
(multlist '(1 2) '(9 2 4))
;Value 15: (9 4)
See what I mean about not really being a proper multiplication? (multist a b) is not always the same as (multlist b a).

How to make pairs from a numeric list based on cardinality?

I have a list '(1 2 1 1 4 5) and want output list as '((1 3)(2 1)(4 1)(5 1)). I have written a small code but I am stuck with how to calculate the cardinality for each number and then put it as pair in list. Can anyone please look at my code and give some ideas?
(define set2bags
(lambda (randlist)
(cond ((null? randlist) '())
(else
(sort randlist)
(makepairs randlist)))))
(define makepairs
(lambda (inlist)
(let ((x 0)) ((newlist '()))
(cond ((zero? (car inlist)) '())
(else
(eq? (car inlist)(car (cdr inlist)))
(+ x 1)
(makepairs (cdr inlist))
(append newlist (cons (car inlist) x)))))))
Your current solution is incorrect - it doesn't even compile. Let's start again from scratch, using a named let for traversing the input list:
(define set2bags
(lambda (randlist)
(cond ((null? randlist) '())
(else (makepairs (sort randlist >))))))
(define makepairs
(lambda (inlist)
(let loop ((lst inlist)
(prv (car inlist))
(num 0)
(acc '()))
(cond ((null? lst)
(cons (list prv num) acc))
((= (car lst) prv)
(loop (cdr lst) prv (add1 num) acc))
(else
(loop (cdr lst) (car lst) 1 (cons (list prv num) acc)))))))
Now it works as expected:
(set2bags '(1 2 1 1 4 5))
=> '((1 3) (2 1) (4 1) (5 1))
The trick is keeping a counter for the cardinality (I called it num), and incrementing it as long as the same previous element (I named it prv) equals the current element. Whenever we find a different element, we add a new pair to the output list (called acc) and reset the previous element and the counter.
Your code is fairly hard to read without proper formating.
I notice a two branch cond, which is easier to read as an if.
In your else clause of set2bags, you call (sort randlist) but leave it as is. You actually want to use this in the next s-expression (makepairs (sort randlist))
So far a pretty good idea.
Now in makepairs you should have better abstraction, say let variables like-first and unlike-first. If the inlist is null, then the function should be the null list, else it's the pair with the car being the list of the car of like-first and the length of like-first and the cdr being the result of calling makepairs on the unlike-first list
(define (makepairs inlist)
(let ((like-first (filter (lambda (x) (equal? x (car inlist)) inlist))
(unlike-first (filter (lambda (x) (not (equal? x (car inlist))) inlist)))
(if (null? inlist)
'()
(cons (list (car inlist) (length like-first)) (makepairs unlike-first)))))
more effecient version
(define (makepairs inlist)
(if (null? inlist)
'()
(let loop ((firsts (list (car inlist)))
(but-firsts (cdr inlist)))
(if (or (null? but-firsts)
(not (equal? (car firsts) (car but-firsts))))
(cons (list (car firsts) (length firsts))
(makepairs but-firsts))
(loop (cons (car but-firsts) firsts) (cdr but-firsts))))))
]=> (makepairs (list 1 1 1 2 4 5))
;Value 17: ((1 3) (2 1) (4 1) (5 1))
If you have your own implementation of sort, say a mergesort you could write this right into the merge part for the best effeciency.
(define (set2bags lst)
(mergesort2bags lst <))
(define (mergesort2bags lst pred)
(let* ((halves (divide-evenly lst))
(first-half (car halves))
(other-half (cadr halves)))
(cond ((null? lst) '())
((null? (cdr lst)) (list (list (car lst) 1)))
(else
(merge-bags
(mergesort2bags first-half pred)
(mergesort2bags other-half pred)
pred)))))
(define (divide-evenly lst)
(let loop
((to-go lst)
(L1 '())
(l2 '()))
(if (null? to-go)
(list L1 L2)
(loop (cdr to-go) (cons (car to-go) L2) L1))))
(define (merge-bags L1 L2 pred)
(cond ((null? L1) L2)
((null? L2) L1)
((pred (caar L1) (caar L2))
(cons (car L1) (merge-bags (cdr L1) L2 pred)))
((equal? (caar L1) (caar L2))
(cons (list (caar L1) (+ (cadar L1) (cadar L2)))
(merge-bags (cdr L1) (cdr L2) pred)))
(else (cons (car L2) (merge-bags L1 (cdr L2) pred)))))
(mergesort2bags (list 1 2 1 1 4 5) <)
;Value 46: ((1 3) (2 1) (4 1) (5 1))
I'm thinking for very large datasets with a lot of repetition this method would pay off.

Adding an element to a list in Scheme

I'm using R5RS Scheme and I just want to implement a function that returns the intersection of two given lists, but I can't do that because I cannot add an element to a list. Here is my code. How can I fix it? I'm really a beginner in Scheme - this is my first work using Scheme.
thx in advance..
(define list3 '())
(define (E7 list1 list2)
(cond
((null? list1)
list3)
((member (car list1) list2) (append list3 (list (car list1))))
)
(cond
((null? list1)
list3)
((not(null? list1)) (E7 (cdr list1) list2)
)
)
)
(E7 '(4 5) '(3 4))
Here is a recursive version that does the intersection instead of the union.
(define (intersect list1 list2)
(cond ((null? list1) list1)
((member (car list1) list2) (cons (car list1) (intersect (cdr list1) list2)))
(t (intersect (cdr list1) list2))))
I think I see your problem. There are two ways to add an element to a list.
The first way would be actually adding it:
(define (intersect list1 list2)
(define newlist list2)
(do ((iter1 list1 (cdr iter1)))
(not (null? iter1))
(if (not (member (car iter1) newlist))
(set! newlist (cons (car iter1) newlist)))))
(You'll probably have to look up the definition of do if you really want to use this.)
You may notice that that's quite ugly. That's because no one actually does it this way. Instead, you have to realize that calling a function creates a new variable as well. Try this:
(define (intersect list1 list2)
(cond ((null? list1) list2)
((member (car list1) list2) (intersect (cdr list1) list2))
(else (intersect (cdr list1) (cons (car list1) list2)))))
If you're familiar with algorithms, you'll notice that the code I just wrote is quite slow, but it illustrates the point: in each case, you do a little bit of work and then call your function again. If you're having trouble seeing why this works, run this function instead on your example:
(define (intersect list1 list2)
(display list1) (display " ") (display list2) (newline)
(cond ((null? list1) list2)
((member (car list1) list2) (intersect (cdr list1) list2))
(else (intersect (cdr list1) (cons (car list1) list2)))))
Here is some simplistic elisp:
(defun is (l1 l2)
(let ((rtn))
(mapc
(lambda (e)
(if (member e l1)
(push e rtn)))
l2)
rtn))
This behaves the same as the built-in intersection for these simple tests:
(is '(1 2 5) '(1 4 10 5)) => (5 1)
(intersection '(1 2 5) '(1 4 10 5)) => (5 1)
(is '(1 4 10 5) '(1 2 5)) => (5 1)
(intersection '(1 4 10 5) '(1 2 5)) => (5 1)
You're better off using set operations from srfi-1.