Scheme list modification - regex

I am trying to write a scheme function that takes a list of the form:
((#f ((1 1) (2 1)))
(#f ((1 3) (5 1)))
(#f ((1 4) (7 1)))
)
and removes all the #f to give a list like:
( ((1 1) (2 1))
((1 3) (5 1))
((1 4) (7 1))
)
I have tried the following code but cannot get it to work:
(define meth
(lambda lst
(if (equal? (cdr lst) '())
(cdr (car lst))
(cons (list-ref (car lst) 1) (meth (cdr lst))))))
Does anyone know how to do this? Thanks.

You can just use map to apply the cdr function to each sublist in the list, like this: (map cdr lst). However this will give you
( (((1 1) (2 1)))
(((1 3) (5 1)))
(((1 4) (7 1)))
)
for your sample input, which is one level of nesting more than your sample output. So to get your sample output, use list-ref to get the second element of each sublist:
(define (meth lst) (map (lambda (x) (list-ref x 1)) lst))
Edit: As Eli Barzilay helpfully pointed out, there is the cadr function to get the second element of a list, so this can be shortened to:
(define (meth lst) (map cadr lst))

Here is a way to do it more closely to what you had:
(define meth
(lambda (lst)
(cond
((null? lst) '())
((cons (cadr (car lst)) (meth (cdr lst))))
)
)
)
(define a '(
(#f ((1 1) (2 1)))
(#f ((1 3) (5 1)))
(#f ((1 4) (7 1)))
))

Related

how to apply a function to all sublists recursively in common lisp?

now I have a list:
(+ x (- 4 9))
I first need (- 4 9) change to (- (4 . 0) (9 . 0))
(please do worry this part too much)
(defun typecheck (A)
(cond
((numberp A)
(cons A 0))
((equal A 'x)
(cons 1 1))
(t A)))
then I need to subtract (4 . 0) and (9 . 0) (still this is not my problem, I don't want to post this function because it is too long...
so it becomes
(+ x (-5 . 0))
now this time I change x to (1 . 1) so the list becomes (+ (1 . 1) (- 5 . 0))
I finally add them together (final result is (-4 . 1))
My main problem is how to let Lisp know I want to calculate them first after I got (- (4 . 0) (9 .0)) ? My function will just go stright to (+ (1 . 1) ((- 4 .0) (9 . 0)) and gave me an error msg.
My process :
(defun check (A)
(cond
((atom A)
(let ((newA (typecheck A)))
(calucalte A)))
((listp A)
(mapcar #'check A))
but this function won't store anything...and I have no idea how to do it :( can anyone give me some help? THANK YOU.
If I understood the problem correctly you should just write a single recursive function handling operations and number/symbol conversion, for example:
(defun tcheck (expr)
(cond
((numberp expr)
(cons expr 0))
((eq expr 'x)
(cons 1 1))
((listp expr)
(cond
((eq (first expr) '+)
(let ((a (tcheck (second expr)))
(b (tcheck (third expr))))
(cons (+ (car a) (car b))
(+ (cdr a) (cdr b)))))
((eq (first expr) '-)
(let ((a (tcheck (second expr)))
(b (tcheck (third expr))))
(cons (- (car a) (car b))
(- (cdr a) (cdr b)))))
(T
(error "Unknown operation"))))
(T expr)))
With the above function
(tcheck '(+ x (- 4 9)))
returns (-4 . 1)

DrRacket - creating a list within a list using minimal built in functions

I need a function that will do this:
Odd Length list
Input '(1 2 3 4 5) = '(1 (2 (3) 4) 5)
Even length list
Input '(1 2 3 4) = '(1 (2 () 3) 4)
It needs to use very minimal built in functions. I have spent hours trying to figure this out and I am completely out of ideas at this point.
Here is what I have:
(define (listInList L)
(define length (listLength L))
(define L2 (listInListHelper length L '() '()))
(define L3 (listInListHelper (- length 2) L L2 '()))
L3
)
(define (listInListHelper N L NL)
(cond
((= N 0) '()
((= N 1) (cons (list (car L)) NL))
(else (cons (cons (car L) (list (lastItem L))) NL)
(remove 1 L)))
)
)
(define (lastItem L)
(if (null? (cdr L))(car L)
(lastItem (cdr L)))
)
(define (remove N L)
(cond ((eq? N 0) (cdr L))
(else (cons (car L) (remove (- N 1)(cdr L))))))
This would be one way to do it, you need to tell me if it's minimal enough:
(define (f lst)
(define (helper lst rlst half)
(cond
((= half 0 ) null)
((= half 1/2) (list (car lst)))
(else (list (car lst)
(helper (cdr lst) (cdr rlst) (sub1 half))
(car rlst)))))
(helper lst (reverse lst) (/ (length lst) 2)))
testing:
> (f '(1 2 3 4 5))
'(1 (2 (3) 4) 5)
> (f '(1 2 3 4))
'(1 (2 () 3) 4)

Scheme function that deletes member from list and sublists

First off, if anyone can find a question where this has already been answered, let me know. All I can find are functions that remove duplicates.
Anyhow, I am trying to write a scheme function (delete V L) that takes a value and a list as arguments, and removes that value from the list and all its sublists. For example, given the following input:
> (deep-delete 3 '(1 2 3 (4 3) 5 (6 (3 7)) 8))
It would yield:
(1 2 (4) 5 (6 (7)) 8)
So far, this is what I have written, but I know that the if statement (which is to check to see if the element is a sub-list, which implies it too must be operated on) must be placed incorrectly. Also, I cannot wrap my brain around where I should be using cons and where I shouldn't, because I'm still confused about tracking the return values of the recursion. Can someone please take a look and explain what I'm doing wrong?
(define (delete V L)
(if (list? (car L)) (cons (delete V (car L) (cdr L)))
(cond
((null? L) L)
((equal? V (car L)) (delete V (cdr L)))
(else (cons (car L) (delete V (cdr L))))))))
I have a few comments on your code:
First, in your if statement you use (car L) without checking if L is empty.
Also, in line 2 of your code, you do: (delete V (car L) (cdr L)),
but cons takes two arguments, not three. And you forgot to recursively call delete on the cdr.
You wanted:
(cons (delete V (car L)) (delete V (cdr L)))
Why not use a single cond? Since there are several cases, using cond will make the recursive structure of your algorithm more apparent, and errors easier to catch.
See below.
(define (del V L)
(cond ((null? L) L)
((list? (car L))
(cons (del V (car L)) (del V (cdr L))))
((equal? V (car L)) (del V (cdr L)))
(else (cons (car L) (del V (cdr L))))))
This will recursively delete V from L.
(del 3 '(1 2 3 (4 3) 5 (6 (3 7)) 8))
==> (1 2 (4) 5 (6 (7)) 8)
This is quite easy to achieve with folding; here's an example in Racket using foldr:
(define (deep-delete elt lst (test equal?))
(foldr (lambda (e r)
(if (list? e)
(cons (deep-delete elt e test) r)
(if (test elt e) r (cons e r))))
null
lst))
testing
> (deep-delete 3 '(1 2 3 (4 3) 5 (6 (3 7)) 8))
'(1 2 (4) 5 (6 (7)) 8)
This removes subtrees from a tree (including atomic ones):
(define (remove-element needle haystack)
(let rec ((haystack haystack))
(cond
((equal? needle haystack) '())
((not (pair? haystack)) haystack)
((equal? needle (car haystack)) (rec (cdr haystack)))
((equal? needle (cdr haystack)) (cons (rec (car haystack)) '()))
(else (cons (rec (car haystack))
(rec (cdr haystack)))))))
(remove-element 'atom 'atom) ; => ()
(remove-element '(1 2 3) '((1 2 3) 1 2 3)) ; => ()
(remove-element '(1 2 3) '((1 2 3) 4 5 6)) ; => (4 5 6)
(remove-element '(1 2 3) '(3 2 1 2 3)) ; ==> (3 2)
(remove-element '3 '((1 2 3) 1 2 3)) ; ==> ((1 2) 1 2)
(remove-element '(1 2 3) '(1 2 3 4)) ; ==> (1 2 3 4)

Delete element from List in Scheme

I have list in this form
( (1 3) (2 2) (3 1) (4 5) (5 1)))
and I want to delete an item let's say (3 1)
So the result will be
( (1 3) (2 2) (4 5) (5 1)))
I have written something like this and I do not know why it is not running correctly.
(define (deleteItem list item)
(cond
((equal? item (car list)) (cdr list))
(cons (car list)(deleteItem(cdr list) item))))
There's a built-in function for this, it's called remove:
(define lst
'((1 3) (2 2) (3 1) (4 5) (5 1)))
(remove '(3 1) lst)
=> '((1 3) (2 2) (4 5) (5 1))
… But I guess you need to implement it from scratch. Some suggestions for your code:
You should not use list as a parameter name, that'll clash with a built-in function. Let's call it lst instead
You're missing the base case necessary form most list procedures: what happens if the list is empty?
You're also missing the else part in the last condition
With all the above fixes in place, the procedure will work:
(define (deleteItem lst item)
(cond ((null? lst)
'())
((equal? item (car lst))
(cdr lst))
(else
(cons (car lst)
(deleteItem (cdr lst) item)))))
(deleteItem lst '(3 1))
=> '((1 3) (2 2) (4 5) (5 1))
The procedure already exists:
(remove '(3 1) '((1 3) (2 2) (3 1) (4 5) (5 1))))
Otherwise your procedure should look like this:
(define (deleteItem item list)
(cond
((empty? list) '())
((equal? item (car list)) (cdr list))
(else (cons (car list) (deleteItem item (cdr list))))))
You missed:
the base case, (empty? list)
the "else" in the final clause
and you shouldn't use list as a variable name because it shadows the build-in procedure list (but it will work).
1) if consider the input list may be a simple list, or you just want to delete the item in the top-level of a nested list
for example:
delete 2 from (1 2 3 4) will return (1 2 3)
delete 2 from (1 2 3 (2 3) 3 2 4) will return (1 3 (2 3) 3 4)
as we can see the 2nd example above, it just delete the item in the top-level of the nested list, within the inner list, we doesn't change it.
this code should be:
(define (deleteitem list1 item)
( cond
((null? list1) ’())
((equal? (car list1) item) (deleteItem (cdr list1) item))
(else (cons (car list1) (deleteitem (cdr list1) item)))
))
2) if consider the input list may be a nested list
for example:
input list: (1 2 3 (3 2 (2 4 (2 5 6) 2 5 6) 2 4) 2 3 (2 3 4))
and delete the element 2 in the input list
the output list should be: (1 3 (3 (3 (5 6) 5 6) 4) 3 (3 4))
and the code should be:
(define (delete2 list1 item)
( cond
((null? list1) '())
((pair? (car list1)) (con (delete2 (car list1) item) (delete2 (cdr list1) item)))
((equal? (car list1) item) (delete2 (cdr list1) item))
(else (cons (car list1) (delete2 (cdr list1) item)))
))

In Lisp, how do you add a given element to every list inside a given list?

Here's what I have so far:
(defun append-all(x L)
(if (null L)
L
(cons (append (car L) x) (append-all x (cdr L))))
)
)
Output:
(append-all '3 '((1) (2 1) (2)))
((1 . 3) (2 1 . 3) (2 . 3))
Want:
((1 3) (2 1 3) (2 3))
This is a helper function, so the fact that it is a linked list seems to be causing me problems.
Thanks
edit: fixed recursive call
In your code, change this part:
(append (car L) x)
To this:
(append (car L) (list x))
It wasn't working before because append should receive two lists as parameters, not a list and an element.
(defun append-all (item list)
"Appends ITEM to each sublist of LIST"
(flet ((circular-list (item)
(let ((list2 (list item)))
(nconc list2 list2))))
(mapcar #'append
list
(circular-list (list item)))))
If you'd rather not do the recursion yourself, this should also work:
(defun append-all (x L)
(mapcar #'(lambda (l) (append l (list x))) L))
I am learning clisp, but it can work.
(defun append-all (x L)
(flet (
(append-item (alist) (append alist (list x))))
(mapcar #'append-item L)))
(print (append-all '3 '((1) (2 1) (2))))