scheme iteration of summing in lists - list

i want to ask if someone wants to help me with a iterative scheme code that gives the sum of two lists. I already have the recursive version of the code.
(define (sum-lists l1 l2)(cond ((and (null? l1) (null? l2)) '())
((null? l1) l2)
((null? l2) l1)
(else (cons (+ (car l1) (car l2)) (sum-lists (cdr l1) (cdr l2))))))

The general "iterative procedure" recipe:
Write a helper function that takes an accumulator parameter.
Create each intermediate result in the accumulator.
When you terminate the recursion, return the accumulator.
Pass it a suitable initial accumulator.
It's common when transforming lists to accumulate the result in reverse and then reverse the result when you're done.
A trivial example, incrementing each list element by one:
(define (add-1 ls)
(if (null? ls)
'()
(cons (+ 1 (car ls)) (add-1 (cdr ls)))))
To make it iterative, create a helper function where you cons to the accumulator instead of the recursive result:
(define (add-one ls acc)
(if (null? ls)
(reverse acc)
(add-one (cdr ls) (cons (+ 1 (car ls)) acc))))
(Note that, except for the introduction of acc, the recursion contains the same parts as before but in a different order.)
Then you start it off with an empty accumulator:
(define (add-1 ls)
(add-one ls '()))
Your case has slightly more involved base cases since you need to accomodate inputs of different lengths.
Finishing it left as an exercise.
(Another exercise: figure out why your first condition, (and (null? l1) (null? l2)), is unnecessary.)

Related

Delete every second item in the list Scheme

My program works with all lists except the improper lists (which have an atom in the cdr field of the last cons cell). Please help upgrade this program to work with the improper lists:
(define (ndelete lst)
(let recur ((i 1) (rest lst))
(cond ((null? rest) '())
((= i 2) (recur 1 (cdr rest)))
(else (cons (car rest) (recur (+ i 1) (cdr rest)))))))
You just need to fix your base condition, (null? rest). If you want to support improper lists, you should check for (not (pair? rest)) instead.
Of course, this has an annoying side-effect of making your function handle any object - not just lists. For any non-list object, it just returns nil. If that's a problem for you, you'll need to encapsulate your recursive function and make sure lst is in fact a list. Like so:
(define (ndelete lst)
(letrec ((recur (lambda (i rest)
(cond ((not (pair? rest)) '())
((= i 2) (recur 1 (cdr rest)))
(else (cons (car rest) (recur (+ i 1) (cdr rest))))))))
(if (pair? lst)
(recur 1 lst)
(raise (condition (make-error)
(make-message-condition `(,lst is not a pair)))))))

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, Check if anything in two lists are the same

Is it possible to check two list against each other if anything is the same in them?
(check-list '(hey cookie monkey) '(apple pizza cookie) ==> #t
I tried something like
(define (check-list list element)
(let ((x list))
(cond ((null? x) #f)
((eq? (car x) (car element)) #t)
(else (check-list (cdr x) element))))
(check-list list (cdr element)))
I know this is not correctly written but don't know how to tackle this problem.
Anyone that can help me?
Sometimes it helps to formulate the process of the solution to a problem in your natural language. Let's simplify the problem a bit.
How do you check if one element is contained in a list? One way to do that would be to compare that one element with each element in the list until you found it - somewhere along the lines you have already done - but not quite. A quick draft would be:
(define (member? e lst)
(cond ((null? lst) #f) ; empty list doesn't contain e
(or (eq? e <??>) ; either the first element is e or
(member? e <??>))) ; the rest of the list contains e
We can use that previous knowledge to solve the real problem at hand. We know how to search for one element in a list, and now we need to search for each element in a list in another list.
(define (check-list lst1 lst2)
(if (or (null? lst1) (null? lst2)) #f ; empty list(s) share no elements
(or (member? <??> <??>) ; first element of lst1 in lst2?
(member? <??> <??>)))) ; rest of lst1 in lst2?
The <??> should be substituded with the appropriate expressions for selecting the parts of the lists.
Similar to a prior answer but exploiting logic primitives:
(define (intersect? list1 list2)
(and (not (null? list1))
(or (member (car list1) list2)
(intersect? (cdr list1) list2))))
If the lists are wicket long you might want to hash the first list and then just iterate through the second. This uses R5RS with srfi-69 and for small lists you'll get a little overhead but
(require srfi/69); alist->hash-table, hash-table-ref/default
(define (intersect? list1 list2)
(let ((hash (alist->hash-table (map (lambda (x) (cons x x)) list2) equal? )))
(let loop ((list list1))
(and (not (null? list))
(or (hash-table-ref/default hash (car list) #f)
(loop (cdr list)))))))
There seems to be a little confusion. The "big" problem here is how to determine if two lists share at least one element in common, let's write a procedure for that called element-in-common?. Before tackling this problem, we need to determine if a single element belongs in one list, that's what check-list should do (notice that in your code check-list receives as a second parameter an element, but you're treating it as if it were a list of elements).
You don't have to write the check-list procedure, it already exists and it's called member. With that knowledge in hand, we can solve the big problem - how to determine if at least one of the elements in one list (let's call it lst1) is in another list (called lst2)?
Simple: we iterate over each of the elements in lst1 using recursion, asking for each one if it belongs in lst2. If just one element of lst1 is a member of lst2, we return #t. If none of the elements in lst1 is in lst2, we return #f. Something like this:
(define (element-in-common? lst1 lst2)
(cond (<???> ; is the first list empty?
<???>) ; then there are no elements in common
((member <???> lst2) ; is the current element of `lst1` in `lst2`?
<???>) ; then there IS an element in common
(else ; otherwise
(element-in-common? <???> lst2)))) ; advance recursion
Don't forget to test your code:
(element-in-common? '(hey cookie monkey) '(apple pizza cookie))
=> #t
(element-in-common? '(hey cookie monkey) '(apple pizza pie))
=> #f
You can use memq to check if first element on the first list is on the second list, and if not then check recursively if something in the rest of the first list is in the second list:
(define (check-list list1 list2)
(cond ((null? list1) #f)
((memq (car list1) list2) #t)
(else (check-list (cdr list1) list2))))
Here's an answer using higher-order functions
in mit-schme
(define (check-list L1 L2)
(apply boolean/or (map (lambda (x) (member? x L2)) L1)))
(define remove-item
(lambda (lst ele)
(if (null? lst)
'()
(if (equal? (car lst) ele)
(remove-item (cdr lst) ele)
(cons (car lst)
(remove-item (cdr lst) ele))))))

MIT Scheme List Sorted in Ascending order

I'm taking a practice exam for a course in programming that deals with MIT Scheme. One of the questions asks:
"Complete the procedure (in-order ls) to return the list ls, except stop just before the first value that is not strictly greater than the previous value in ls. In other words, in-order should return the portion of ls starting from the beginning that is sorted in strictly increasing order. Assume that ls contains only non-negative integers."
The question then shows several examples:
(in-order '(1 2 3 4)) ; should return (1 2 3 4)
(in-order '(1 2 3 3 4 5)) ; should return (1 2 3)
(in-order '(3 2)) ; should return (3)
(in-order '(3)) ; should return (3)
This is my attempt at a solution:
(define (in-order ls)
(cond ((null? ls) ls)
((< (car ls) (cadr ls))
(cons (car ls) (cons (in-order (cdr ls)) ())))
((>= (car ls) (cadr ls)) (car ls))
(else "Nothing")))
It comes close to working with the 2nd and 3rd example, but outright fails with the 1st and 4th example. I know that it keeps trying to pass a null as part of the argument, but I'm unsure of how to work around this. Other than that, is there anything else that I'm getting wrong?
This gets you there:
(define (in-order ls)
(if (null? ls)
'()
(let looking ((result (list (car ls))) (ls (cdr ls)))
(if (or (null? ls) (not (< (car result) (car ls))))
(reverse result)
(looking (cons (car ls) result)
(cdr ls))))))
The tail recursive looking always has the last value as the result's car. So the comparison for stopping becomes (not (< (car result) (car ls)))
In your code the (cons (in-order ...) ()) is almost surely wrong. The predicate (< (car ls) (cadr ls)) will fail on anything like '(3) - you need something like (null? (cdr ls)) to avoid that.
In a non-tail recursive algorithm, similar to yours, it would be:
(define (in-order ls)
(cond ((or (null? ls) (null? (cdr ls))) ls) ; nothing left
((< (car ls) (cadr ls)) ; extend and continue
(cons (car ls) (in-order (cdr ls))))
(else (list (car ls))))) ; last one

Scheme List Derangement (Rearrangement of sorts)

im trying to write a function in Scheme where i accept a list and return all the different derangements (look below for definition) as a list of lists
derangement: A list where no item is in the same place as the original list
ex: '(a b c) -> '(cab)
any help is appreciated!
Compute all of the permutations of the input list and then filter out the ones that have an element in the same position as the input list. If you need more detail, leave a comment.
Edit 1:
Define (or maybe it's defined already? Good exercise, anyway) a procedure called filter that takes as its first argument a procedure p and a list l as its second argument. Return a list containing only the values for which (p l) returns a truthy value.
Define a procedure derangement? that tests if a list l1 is a derangement of l2. This will be handy when paired with filter.
The most obvious solution would be something like this:
(define filtered-permutations
(lambda (lst)
(filter
(lambda (permuted-list)
(deranged? permuted-list lst))
(permute lst))))
Since the number of derangements is considerably lower than then number of permutations, however, this is not very efficient. Here is a solution that mostly avoids generating permutations that are not derangements, but does use filter once, for the sake of simplicity:
(define deranged?
(lambda (lst1 lst2)
(if (null? lst1)
#t
(if (eq? (car lst1) (car lst2))
#f
(deranged? (cdr lst1) (cdr lst2))))))
(define derange
(lambda (lst)
(if (< (length lst) 2)
;; a list of zero or one elements can not be deranged
'()
(permute-helper lst lst))))
(define derange-helper
(lambda (lst template)
(if (= 2 (length lst))
(let ((one (car lst))
(two (cadr lst)))
(filter
(lambda (x)
(deranged? x template))
(list (list one two) (list two one))))
(let ((anchor (car template)))
(let loop ((todo lst)
(done '())
(result '()))
(if (null? todo)
result
(let ((item (car todo)))
(if (eq? item anchor)
;; this permutation would not be a derangement
(loop (cdr todo)
(cons item done)
result)
(let ((permutations
(map
(lambda (x)
(cons item x))
(derange-helper (append (cdr todo) done)
(cdr template)))))
(loop (cdr todo)
(cons item done)
(append result permutations)))))))))))