Advice on how to tackle this lisp function. - list

I have written a function called my_rotate that takes a number from a user and creates a list up to five numbers. my_rotate then, pops off the first element of the list and adds it to the end of the list. Any advice on how I could write my_rotate to take in another number n and rotate the list based on the number n, in which the user entered.
Example:
> (my_rotate 1 2)
Outputs:
(3 4 5 1 2)
This is what I have so far:
(defun my_rotate (y)
(append (loop for i from (+ 1 y) to (+ 4 y) collect i)
(list y)))

Here the function.
I create two lists and then concatenate them.
(defun my-rotate (length shift)
"Return a list of given LENGTH, rotated by SHIFT."
(nconc
(loop for i from (1+ shift) to (- length shift -2) collect i)
(loop for i from 1 to shift collect i)))
(my-rotate 7 2)
==> (3 4 5 6 7 1 2)
Note that since both loops produce fresh lists, I use nconc instead of append.
If, however, you want to rotate an existing list, you will need to do something else:
(defun rotate-list (list shift)
"Rotate the given LIST by the specified SHIFT."
(let ((len (length list)))
(setq shift (mod shift len)) ; handle circular shifts
(append (nthcdr (- len shift) list)
(butlast list shift))))
(rotate-list '(1 2 3 4 5 6) 2)
==> (5 6 1 2 3 4)
(rotate-list '(1 2 3 4 5 6) 20)
==> (5 6 1 2 3 4) ; same because 20 = 2 mod 6
(rotate-list '(1 2 3 4 5 6) 0)
==> (1 2 3 4 5 6) ; unchanged
Note that nthcdr points inside the original list, so we have to use append to avoid modifying the argument.
Note also that we scan the list argument twice (once in nthcdr and once in butlast).
If your lists are huge, and profiling shows that this function is the bottleneck, you might want to re-write this using a loop (this is scenario is so unlikely, that I already regret having wasted my time writing this note).

Related

How to do a multiplying table on Scheme/Racket

So im extremely new to Scheme, and i've been trying to do a multiplying table, if you put
(multiplying-table 10 3) should give a list (3 6 9 12 15 18 21 24 27 30)
Here is my code
(define (multiplying-table n value)
(cond ((= n 0) '())
(else (* n value)(Multiplying-table(- n 1)))))
You need to use cons to combine the first number 3 with the list of the rest.
Eg. (3 6 ...) is (cons 3 (cons 6 ...)). You are instead having two expressions where the (* n value) is only for effect since the result is never used. Because of that every call should return the empty list.
Usually there are 2 ways to do it. With recursion inside cons or with an accumulator. Since lists are created from end to beginning you could count n downwards and build the list from end to beginning. The base case would just return the accumulator which by default is the empty list. Here is an example:
(define (sequence from to)
(let loop ((n to) (acc '()))
(if (< n from)
acc
(loop (- n 1) (cons n acc)))))
(sequence 5 10) ; ==> (5 6 7 8 9 10)

lisp: building a list of lists from a single list

I am trying to take a list of 16 numbers I have and make it into a list of 4, 4 element sublists to represent the game board of a magic square. I made a method that can take a list and return a single sublist, and now I am trying to recursively use this method to build the full board.
My problem however, is my initBoard returns nil no matter what and I know every other method is working as desired. Any clarification of my error would be greatly appreciated!
Also here is an example input list:
(4 5 15 10 14 11 1 8 9 16 6 3 7 2 12 13)
And what I want as the output would be:
((4 5 15 10) (14 11 1 8) (9 16 6 3) (7 2 12 13))
(defun smallList (lst cnt)
(cond ((>= cnt 4) nil)
(t (cons (car lst) (smallList (cdr lst) (+ 1 cnt))))))
(defun isEmpty (lst)
(if lst 1 -1))
(defun initBoard (lst)
(cond ((= (isEmpty lst) -1) nil)
(t (cons (smallList lst 0) (initBoard (cddddr lst))))))
Some remarks:
someList, lst, cnt is not idiomatic, use some-list, list, count
You don't need is-empty, just use endp or null, which returns a boolean (not -1 or 1). You could make an alias if you want (but why?):
(setf (symbol-function 'is-empty) #'endp)
You could use a loop for small-list:
(defun small-list (list)
(values (loop repeat 4 collect (pop list)) list))
The secondary value is the rest of the list, so that you don't need to cddddr.
But in fact, it might be better to initialize the whole board inside a single function:
(defun init-board (list)
(loop repeat 4 collect
(loop repeat 4 collect (pop list))))
The first LOOP collect lists of 4 elements, which are collected by the inner LOOP. The collected elements are popped from the input list.
Now, if I wanted to be extremely careful, I would add some checks and report errors on bad inputs:
(defun init-board (list)
(flet ((failure ()
(error "Input list should contain exactly 16 integers: ~S"
list)))
(loop
with current = list
repeat 4 collect
(loop
repeat 4
collect (if current
(let ((element (pop current)))
(check-type element integer)
element)
(failure)))
into board
finally (if list (failure) (return board)))))
Also, I would use a multi-dimensional array for boards.
(make-array '(4 4) :initial-contents (init-board list))
I just tested your three functions and it gave me the correct output, so perhaps your issue isn't where you think it is.
(initBoard '(4 5 15 10 14 11 1 8 9 16 6 3 7 2 12 13))
=> ((4 5 15 10) (14 11 1 8) (9 16 6 3) (7 2 12 13))
I would use the following recursive function:
(defun smalllist (l n)
(when l
(cons (subseq l 0 (min n (length l)))
(smalllist (nthcdr n l) n))))

Recursive functions that rotate n elements of a list to left and right in lisp

The function has 1 parameter, an integer.
For example rot-left(2 '(1 2 3 4 5)) should return (3 4 5 1 2 ) and rot-right(2 '(1 2 3 4 5)) should return (5 4 1 2 3).
I've tried this... it doesn't work but what it's supposed to do is add the last n elements of a list to an empty list.
(defun rot_left (n l)
(if (zerop n)
'()
(append (last l)
rot-left ((- n 1) (cdr l)))))
I will give a solution assuming that, if the function rot-right should rotate the elements of the list from right to left, (rot-right 2 '(1 2 3 4 5)) should produce (4 5 1 2 3) and not (5 4 1 2 3).
Then, assuming that this interpretation is correct, the functions can be written only by means of primitive operators in Common Lisp, without the use of iteration or recursion:
(defun rot-left(n l)
(append (nthcdr n l) (butlast l (- (length l) n))))
(defun rot-right(n l)
(rot-left (- (length l) n) l))
(defvar a '(1 2 3 4 5))
(rot-left 2 a) ; produces (3 4 5 1 2)
(rot-right 2 a) ; produces (4 5 1 2 3)

opposite of list-ref? (Racket)

Is there anything which acts as the opposite of list-ref, where instead of selecting certain values to add to a list, it'll take values away from a list?
I basically want to do the following
(list 1 2 3 4 5 6 7) (list 3 6 7) -> (list 1 2 4 5)
Where the values in list two get deleted from list one. (preferred)
Since I will always start with a list that goes from 1 to n,
the second list could also represent the location/position where a number on list 1 should be deleted. (less preferred)
I'm trying to create a code which will manipulate other functions to come up with these lists, so please be clear where each list is 'mentioned' in the code, as I sometimes get confused if people use x y and z and so forth with multiple lambda, local definitions, etc.
I have something here which does the opposite of what I want and I've been trying to alter it so instead of outputting the elements of x that are on y, it gives the elements of x which are NOT on y.
(define (selection x y)
(filter (lambda (e2)
(ormap (lambda (e1) (equal? e1 e2))
y))
x))
example:
(list 1 2 3 4 5 6 7 8 9 10)
(list 2 4 6 8 10))
-> (list 2 4 6 8 10))
Anybody have any ideas on how to change the output to what I need?
It sounds like you're using lists as sets. You could instead use Racket sets, and use the set-subtract function:
#lang racket
(set-subtract (set 1 2 3 4 5 6 7)
(set 3 6 7))
;; => (set 1 2 4 5)
remove will do the trick I guess.
> (remove* (list 1 2) (list 1 2 3 2 4 5 2))
'(3 4 5)
You can read the doc here.
Here's a simple recursive function that achieves what you want:
(define remove-list-from-list (lambda (list remlist)
(cond
[(null? list) '()]
[(member (car list) remlist) (remove-list-from-list (cdr list) remlist)]
[else (cons (car list) (remove-list-from-list (cdr list) remlist))])))
Now you can use it like so:
> (remove-list-from-list (list 1 2 3 4 5 6 7) (list 3 6 7))
'(1 2 4 5)

Strange behavior invoking destructive Common LISP function receiving as argument a list created with quote

I've been getting a strange behavior when invoking a destructive definition receiving as argument a local variable whose type is a list created with a quote.
Destructive function:
(defun insert-at-pos (pos list elem)
(if (= pos 0)
(cons elem list)
(let ((aux-list (nthcdr (1- pos) list)))
(setf (rest aux-list) (cons elem (rest aux-list)))
list)))
WRONG: Local variable is a list created with the special operator quote.
(defun test ()
(let ((l '(1 2 3)))
(print l)
(insert-at-pos 2 l 4)
(print l)))
> (test)
(1 2 3)
(1 2 4 3)
(1 2 4 3)
> (test)
(1 2 4 3)
(1 2 4 4 3)
(1 2 4 4 3)
> (test)
(1 2 4 4 3)
(1 2 4 4 4 3)
(1 2 4 4 4 3)
CORRECT: Local variable is a list created with function list.
(defun test2 ()
(let ((l (list 1 2 3)))
(print l)
(insert-at-pos 2 l 4)
(print l)))
or
(defun test2 ()
(let ((l '(1 2 3)))
(print l)
(setf l (cons (first l) (cons (second l) (cons 4 (nthcdr 2 l)))))
(print l)))
> (test2)
(1 2 3)
(1 2 4 3)
(1 2 4 3)
> (test2)
(1 2 3)
(1 2 4 3)
(1 2 4 3)
> (test2)
(1 2 3)
(1 2 4 3)
(1 2 4 3)
Does someone know the reason of this strange behaviour?
If you quote data in a function, then it is literal data. The effects of destructively modifying such literal data are undefined in the Common Lisp standard. In your example all function invocations share the same literal data and the implementation does not warn you that you are changing it. That's what most implementations do. But it would also possible to imagine an implementation which puts all code (and its literal data) into a read-only part of the memory.
You can get funky effects with this.
If you want to destructively modify a list without running into potential problems, then you need to create a fresh copy at runtime. For example by calling LIST or COPY-LIST. LIST will return a fresh consed list.
There are similar pitfalls. For example imagine a file with these definitions:
(defvar *foo* '(1 2 3 4 5 6 ... 10000))
(defvar *foo* '(0 1 2 3 4 5 6 ... 10000))
If you compile such a file with the file compiler, the compiler is allowed to create a compiled file, where the two variables share literal data - saving space. If you would change an element in either list, both might be changed.