Implementing permutation given a list of all possible combinations - list

I am attempting to implement a version of permutation using a function I already wrote i'm calling pair-divide which gives me all the possible pair combinations of a list. For example (list 9 10 11) would become the list of tuples
(list (tuple empty (9 10 11))
(tuple (9) (10 11))
(tuple (9 10) (11))
(tuple (9 10 11) empty)))
My idea to implement permutation is this:
Use pair-divide to create the tuples of a single element e.g
(pair-divide 1) =
(list (tuple empty (1))
(tuple (1) empty)))
Then insert the next element in the given list into the middle of these tuples. Then repeat until the list is empty.
In my mind, that should enable me to create all the possible variations of a given list, but I am struggling to understand how to actually implement this idea.

Related

Insertion into a list doesn't reflect outside function whereas deletion does?

I am new to Lisp. Deletion of an item in a list by a function gets reflected outside the function but insertion doesn't. How can I do the same for insertion?
For example
(defun test (a b)
(delete 1 a)
(delete 5 b)
(append '(5) b)
(member '5 b))
(setq x '(2 3 1 4))
(setq y '(8 7 5 3))
(test x y)
;;x and y after function ends
x
(2 3 4)
y
(8 7 3)
Why doesn't append affect list y? How can I insert something into y from within the function?
Append isn't supposed to modify anything
Why doesn't append affect list y?
The first sentence of the documentation on append is (emphasis added):
append returns a new list that is the concatenation of the copies.
No one ever said that append is supposed to modify a list.
You can't change the value of a lexical binding outside its scope
How can I insert something into y from within the function?
In the most general sense, you cannot. What happens if the value of y is the empty list? There's no way with a function (as opposed to a macro) to make something like this work in Common Lisp:
(let ((list '())
(insert list 1)
l)
;=> (1)
A function cannot change the lexical binding of a variable outside its scope1, so there's no way for insert to change the value of list.
You can, of course, modify the internal structure of an object, so if the value of list is some non-empty list, then you could modify the structure of that list. The value of list wouldn't change (i.e., it would still the same cons cell), but the list represented by that cons cell would change. E.g.,
(defun prepend (list element)
(let ((x (first list)))
(setf (rest list) (list* x (rest list))
(first list) element)))
(let ((list (list 1 2)))
(prepend list 'a)
list)
;=> (a 1 2)
Save return values
In general, you need to get into the habit of saving the results of functions. Most functions won't modify their arguments, so you need to save their results. Some functions are permitted, but not required, to modify their arguments, and they don't have to modify in a predictable way, so you need to save their results too. E.g., your code could be:
(defun test (a b)
(setf a (delete 1 a))
(setf b (delete 5 b))
(setf b (append '(5) b))
(member 5 b))
(test ...)
;=> true
1 You could work around this by giving it a setter function that closed over the binding, etc. But those kind of techniques would be workarounds.

How to make a list out of a structure in racket?

I made an structure (struct g( a b c d))
and I define it as (define per (g 1 6 5 4))
however I want to use per as a list argument for another function I want to define.. it gives me an error
first: contract violation expected: (and/c list? (not/c empty?))
given: #
how can I make per a list with arguments of a structure? or there is no other ways of making something similar.
When you are making a struct it's very much like objects in other languages. You have a struct "g" with 4 named slots. Eg. you access the first with (g-a struct-var) and so on.
If you want a list then make a list. If you need to make a list from a g you need to do something like (list (g-a x) (g-b x) (g-c x) (g-d x)) and to do the opposite you do (apply g lst)

CLISP: Ordering a list depending on the order of elements in another

I have a list
(SetQ L '(1 j 3 k 4 h 5 n 6 w))
I have to do a function Order that has a list with 'n' atoms at entry, it must check if each atom of that list is contained in list L and order them according to the order specified in list L, if the atom is not part of the list L then the result will be displayed
(Defun Order lst)
(SetQ L2'(w o 5 j 3))
I want to verify this:
(Order L2)
result should return:
(J 3 5 W)
Hints:
Earlier, you asked this question:
CLISP : Check if two elements are in order one after another in a list
This function is related to the problem because it can be used as a comparison function in a call to the standard Lisp function sort.
The Lisp function intersection can produce a list which contains only those elements of one list which appear in another. This is a set operation, so it may squash duplicates; another way is to use remove-if-not where the test predicate is a lambda function which uses member to express the idea "remove all elements of this list which are not members of that other list".

How do you find list of a list inside the Scheme list?

I am working on an assignment and I need to find a list inside of a list. For example, if we have
(has-list? '(1 2 (3 4) 5))
than it will return true because (3 4) is a list inside of a bigger list.
The function (list? l) will return #t if l is a list, and #f if it's not
(define (has-list l)
(if (null? l)
_____
(or (________) (_________))))
fill in the blanks!
Well, if the Scheme implementation in hand provides a library function like any which expects a predicate and a list as arguments to test any existence of an element in the list satisfying the predicate, you can simply write (any list? '(1 2 (3 4) 5)), otherwise, do fill the blanks prelic left for you.

Scheme how to create a list

Okay this may sound like a ridiculous question, but how do you return a list in scheme.?
Based on seeing some of your other questions, I think you may be having trouble getting your head wrapped around the concepts central to a functional language such as Scheme.
At the level you're learning Scheme (novice), every function you write has an input and an output, and the body of every function is a single expression. Whatever value that expression evaluates to is returned by the function. There is no need to explicitly "return" anything as you would in an imperative language like Java or C; it just happens as a direct consequence of evaluating the expression.
The body of a function is a single expression. It's not like Java where the body of a method consists of a series of instructions:
do this
then do that
then do something else
then return something (maybe)
Scheme functions evaluate a single expression; nothing more. Here's a simple function that adds 5 to whatever number is passed as an argument:
(define (add5 x)
(+ x 5))
The body of the function is (+ x 5), which is just an expression to be evaluated. The value of x is plugged in, the + (addition) function is applied to x and 5, and the result is returned.
Lists aren't much different. All you need is an expression that will construct a list. Two have already been mentioned: list is used to build a list from scratch if you already have all the elements; cons is used to add a single element to an existing list and is often used recursively.
Here's a function that consumes a number n and builds the list (n n-1 n-2 ... 0)
(define (makelist n)
(if (= n 0)
(list 0) ; base case. Just return (0)
(cons n (makelist (- n 1))))) ; recursive case. Add n to the head of (n-1 n-2 ... 0)
In both the base and recursive cases, a list is returned by simply evaluating an expression that uses one of the list-building functions.
Here's another example. This one uses our add5 function to add 5 to each element of a list of numbers (lon):
(define (add5list lon)
(if (null? lon)
`() ; base case: lon is empty. Return an empty list.
(cons (add5 (car lon)) (add5list (cdr lon))))) ; recursive case.
; Add 5 to the head of lon and prepend it to the tail of lon
Again, both the base and recursive cases are returning lists by evaluating expressions that result in lists.
The key thing to remember about Scheme is all functions return something, and that something is simply the result of evaluating an expression. The body of a Scheme function is a single expression.
You probably want simply: '(2 3 5 7 11) or (list 2 3 5 7 11)?
You can also construct lists by specifying an element and a list to add it to: (cons 2 (cons 3 '()))
Here's an example of returning a list from a function:
(define returnlist
(lambda(a b c)
(cons a (cons b (cons c '())))
))
(returnlist 2 3 4)
Return value will be the list: (list 2 3 4)
Another not-so-well known way to do this:
> ((lambda x x) 2 3 5 7 11)
(2 3 5 7 11)
that is, the "list" function itself can be defined as:
> (define list (lambda x x))