How to code Power Set in Lisp R5RS [duplicate] - set

This question already has answers here:
How to do a powerset in DrRacket?
(5 answers)
Closed 7 years ago.
I'm new to functional programming and I have no idea how to code this in Lisp. For example, for a given power set such as (1 2 3), how do I code it in a way to make it: (WITHOUT using Lambda functions)
( () (1) (2) (3) (1 2 3) )
So far, I have:
(define (powerSet lis)
(if (null? lis) '(()))
)
(define (APPENDS lis1 lis2)
(cond
((null? lis1) lis2)
(else (cons (car lis1)
(APPENDS (cdr lis1) lis2)))
)
)
Which just returns the empty set, or nothing.
EDIT:
Thank you so much Chris! That made so much sense. The second variation (without the append-map function) works well. However, if you input (powerset'(1 2 3 4)), it gives you:
(()
(1)
(2)
(1 2)
(3)
(1 3)
(2 3)
(1 2 3)
(4)
(1 4)
(2 4)
(1 2 4)
(3 4)
(1 3 4)
(2 3 4)
(1 2 3 4))
Is there anyway for me to make it look like:
(()
(1)
(2)
(3)
(4)
(1 2)
(1 3)
(1 4)
(2 3)
(2 4)
(3 4)
(1 2 3)
(1 2 4)
(1 3 4)
(2 3 4)
(1 2 3 4))
Thanks so much!

All user-defined functions are lambda (or case-lambda) expressions, including the powerset function you're defining. There is no way to avoid it. However, you can hide the lambda identifier by using internal definitions (it's still a lambda behind the scenes!†).
With this in mind, here's an implementation (requires Racket or SRFI 1):
(define (powerset lst)
(define (make-pair x)
(list x (cons (car lst) x)))
(if (null? lst)
'(())
(append-map make-pair (powerset (cdr lst)))))
If you're trying to avoid append-map or higher-order functions in general, you could jump through a few hoops to do the same thing:
(define (powerset lst)
(define (inner next)
(if (null? next)
'()
(cons (car next)
(cons (cons (car lst) (car next))
(inner (cdr next))))))
(if (null? lst)
'(())
(inner (powerset (cdr lst)))))
† An expression like
(define (foo bar)
baz)
is actually expanded into the following equivalent expression:
(define foo
(lambda (bar)
baz))

Related

Pair combinations in scheme

I'm trying to find the various combinations that can be made with a list of N pairs in scheme. Here is where I'm at thus far:
(define (pair-combinations list-of-pairs)
(if (null? list-of-pairs)
nil
(let ((first (caar list-of-pairs))
(second (cadar list-of-pairs))
(rest (pair-combinations (cdr list-of-pairs))))
(append
(list (cons first rest))
(list (cons second rest))
))))
Now, I'm not sure if the logic is correct, but what I notice immediately is the telescoping of parentheticals. For example:
(define p1 '( (1 2) (3 4) (5 6) ))
(pair-combinations p1)
((1 (3 (5) (6)) (4 (5) (6))) (2 (3 (5) (6)) (4 (5) (6))))
Obviously this is from the repetition of the list (... within the append calls, so the result looks something like (list 1 (list 2 (list 3 .... Is there a way to do something like the above in a single function? If so, where am I going wrong, and how would it be properly done?
The answer that I'm looking to get would be:
((1 3 5) (1 3 6) (1 4 5) (1 4 6) (2 3 5) (2 3 6) (2 4 5) (2 4 6))
That is, the possible ways to choose one element from N pairs.
Here is one way to think about this problem. If the input is the empty list, then the result is (). If the input is a list containing a single list, then the result is just the result of mapping list over that list, i.e., (combinations '((1 2 3))) --> ((1) (2) (3)).
Otherwise the result can be formed by taking the first list in the input, and prepending each item from that list to all of the combinations found for the rest of the lists in the input. That is, (combinations '((1 2) (3 4))) can be found by prepending each element of (1 2) to each of the combinations in (combinations '((3 4))), which are ((3) (4)).
It seems natural to express this in two procedures. First, a combinations procedure:
(define (combinations xss)
(cond ((null? xss) '())
((null? (cdr xss))
(map list (car xss)))
(else
(prepend-each (car xss)
(combinations (cdr xss))))))
Now a prepend-each procedure is needed:
(define (prepend-each xs yss)
(apply append
(map (lambda (x)
(map (lambda (ys)
(cons x ys))
yss))
xs)))
Here the procedure prepend-each takes a list xs and a list of lists yss and returns the result of prepending each x in xs to the lists in yss. The inner map takes each list ys in yss and conses an x from xs onto it. Since the inner mapping produces a list of lists, and the outer mapping then produces a list of lists of lists, append is used to join the results before returning.
combinations.rkt> (combinations '((1 2) (3 4) (5 6)))
'((1 3 5) (1 3 6) (1 4 5) (1 4 6) (2 3 5) (2 3 6) (2 4 5) (2 4 6))
Now that a working approach has been found, this could be converted into a single procedure:
(define (combinations-2 xss)
(cond ((null? xss) '())
((null? (cdr xss))
(map list (car xss)))
(else
(apply append
(map (lambda (x)
(map (lambda (ys)
(cons x ys))
(combinations-2 (cdr xss))))
(car xss))))))
But, I would not do that since the first version in two procedures seems more clear.
It might be helpful to look just at the results of prepend-each with and without using append:
combinations.rkt> (prepend-each '(1 2) '((3 4) (5 6)))
'((1 3 4) (1 5 6) (2 3 4) (2 5 6))
Without using append:
(define (prepend-each-no-append xs yss)
(map (lambda (x)
(map (lambda (ys)
(cons x ys))
yss))
xs))
combinations.rkt> (prepend-each-no-append '(1 2) '((3 4) (5 6)))
'(((1 3 4) (1 5 6)) ((2 3 4) (2 5 6)))
It can be seen that 1 is prepended to each list in ((3 4) (5 6)) to create a list of lists, and then 2 is prepended to each list in ((3 4) (5 6)) to create a list of lists. These results are contained in another list, since the 1 and 2 come from the outer mapping over (1 2). This is why append is used to join the results.
Some Final Refinements
Note that prepend-each returns an empty list when yss is empty, but that a list containing the elements of xs distributed among as many lists is returned when yss contains a single empty list:
combinations.rkt> (prepend-each '(1 2 3) '(()))
'((1) (2) (3))
This is the same result that we want when the input to combinations contains a single list. We can modify combinations to have a single base case: when the input is '(), then the result is (()). This will allow prepend-each to do the work previously done by (map list (car xss)), making combinations a bit more concise; the prepend-each procedure is unchanged, but I include it below for completeness anyway:
(define (combinations xss)
(if (null? xss) '(())
(prepend-each (car xss)
(combinations (cdr xss)))))
(define (prepend-each xs yss)
(apply append
(map (lambda (x)
(map (lambda (ys)
(cons x ys))
yss))
xs)))
Having made combinations more concise, I might be tempted to go ahead and write this as one procedure, after all:
(define (combinations xss)
(if (null? xss) '(())
(apply append
(map (lambda (x)
(map (lambda (ys)
(cons x ys))
(combinations (cdr xss))))
(car xss)))))

List of lists to normal list in scheme

I'm trying to make a scheme function that returns a 'normal' list from a pair made by lists of lists.
I'm trying to change something like this:
((((((() 1) 2) 3) 4) (12 13 14)) ((((() 8) 9) 10) 11) (5 6 7))
into something like this:
(1 2 3 4 12 13 14 8 9 10 11 5 6 7)
I've tried using tail recursion, but my code just returns the same initial pair.
Then I did this, but it also doesn't work and kind of shuffles the list:
(define (tolist l1 lista)
(if (empty? (cdr lista))
null
(if (empty? (car lista))
(append l1 (cdr lista))
(tolist (append l1 (car lista)) (list (cdr lista)) ))))
What can I do?
Oscar Lopez has given you a good answer, although I would test l1 with list? rather than pair?, as with his code (flatten '(1 . 2)) would produce (1 2), which is probably not your desired result.
With that correction his code can be turned into a fold:
(define (flatten seq)
(letrec ((flatten-with-append
(lambda (elt acc) ;; args as for SRFI-1 fold
(if (list? elt)
(fold flatten-with-append acc elt)
(append acc (list elt))))))
(flatten-with-append seq '())))
One problem with this way of doing it is that it appends, which is not very efficient. It is usually better to cons into the accumulator and then reverse. This would be likely to be more optimal:
(define (flatten seq)
(letrec ((flatten-with-cons
(lambda (elt acc) ;; args as for SRFI-1 fold
(if (list? elt)
(fold flatten-with-cons acc elt)
(cons elt acc)))))
(reverse (flatten-with-cons seq '()))))

contract violation expected: number?-Scheme

I am new to racket and scheme and I am attempting to map the combination of a list to the plus funtion which take each combination of the list and add them together like follows:
;The returned combinations
((1 3) (2 3) (1 4) (2 4) (3 4) (1 5) (2 5) (3 5) (4 5) (1 6) (2 6) (3 6) (4 6) (5 6) (1 2) (2 2) (3 2) (4 2) (5 2) (6 2))
; expected results
((2) (5) (5).....)
Unfortunately I am receiving the contract violation expected error from the following code:
;list of numbers
(define l(list 1 2 3 4 5 6 2))
(define (plus l)
(+(car l)(cdr l)))
(map (plus(combinations l 2)))
There are a couple of additional issues with your code, besides the error pointed out by #DanD. This should fix them:
(define lst (list 1 2 3 4 5 6 2))
(define (plus lst)
(list (+ (car lst) (cadr lst))))
(map plus (combinations lst 2))
It's not a good idea to call a variable l, at first sight I thought it was a 1. Better call it lst (not list, please - that's a built-in procedure)
In the expected output, weren't you supposed to produce a list of lists? add a call to list to plus
You're not passing plus in the way that map expects it
Do notice the proper way to indent and format your code, it'll help you in finding bugs
You want (cadr l). Not (cdr l) in your plus function:
(define (plus l)
(+ (car l) (cadr l)))
Where x is (cons 1 (cons 2 '())):
(car x) => 1
(cdr x) => (cons 2 '())
(cadr x) == (car (cdr x)) => 2

Infix to Prefix conversion scheme

I'm trying to figure out how to convert an infix expression to prefix in Scheme.
I found this post which does what I want but in the opposite direction. What changes when going from infix->prefix instead of prefix->infix?
Edit: I forgot to mention I need to account and handle for variables. For example the input
'(2 + 3 * a ^ 5 + b)
It's rather trivial to modify the algorithm you link to:
(define (infix->prefix lst)
(cond
((list? lst)
(unless (= 3 (length lst)) (error "not 3 elements"))
(let ((operand1 (car lst))
(operator (cadr lst))
(operand2 (caddr lst)))
(list operator
(infix->prefix operand1)
(infix->prefix operand2))))
(else lst)))
Testing:
> (infix->prefix '(1 + 2))
'(+ 1 2)
> (infix->prefix '(1 + (2 * 3)))
'(+ 1 (* 2 3))
> (infix->prefix '((1 / 4) + (2 * 3)))
'(+ (/ 1 4) (* 2 3))
This is not a general algorithm though; if you need something more elaborate then please show some examples of conversions you need to do.
EDIT Here's an example code that works for longer expressions but doesn't implement operator precedence:
(define (infix->prefix lst)
(if (list? lst)
(if (null? (cdr lst))
; list with one element -> return element
(infix->prefix (car lst))
; list with more than one element
(list (cadr lst)
(infix->prefix (car lst))
(infix->prefix (cddr lst))))
; not a list -> return element
lst))
Testing:
> (infix->prefix '(2 + 3 * a ^ 5 + b))
'(+ 2 (* 3 (^ a (+ 5 b))))

get 7 not (7) out of the list?

Here's my code:
(define step1_list1 '(1 3 (5 7) 9))
(car (cdr (cdr (step1_list1))))
(define step1_list2 '((7)))
(car (step1_list2))
(define step1_list3 '(1 (2 (3 (4 (5 (6 7)))))))
(car (cdr (cdr (cdr (cdr (cdr step1_list3))))))
))
Running this code causes an error:
(1 3 (5 7) 9) is not applicable
What is the problem?
Start small.
(define mylist '(1 2 3))
(display mylist)
(display (car mylist))
(display (car (mylist)))
Run each of those in turn, and see what you get at each step. Once you understand why you get the output you do, then you should be able to fix the code in your question.
In Scheme, (non-quoted) parentheses mean function application. So (car (step1_list2)) tries to execute step1_list2 as a procedure (and then take the car of the result). Instead, you want:
(car step1_list2)

Resources