Replacing elements in a list

Discussion of Common Lisp

Re: Replacing elements in a list

Postby Paul Donnelly » Wed Jun 03, 2009 1:21 am

Lispoman wrote:
Code: Select all
(defun add (number)
  (intern (format nil "*~a" number)))

(defun myfunc (x)
  (cond ((null x) nil)
        ((< (car x) 0) (cons (add (car x)) (myfunc (cdr x))))
        (T (cons (car x) (myfunc (cdr x))))))

Why so recursive?
Lispoman wrote:Now the question can lisp differ even and odd numbers? And how can the function above be modified to use with lambda?

You can use EVENP or ODDP as you prefer, or you can use MOD, as you could in any language. I don't know what you mean by modifying it to use with lambda.
Paul Donnelly
 
Posts: 148
Joined: Wed Jul 30, 2008 11:26 pm

Re: Replacing elements in a list

Postby Lispoman » Wed Jun 03, 2009 5:59 am

Thanks again! Solved.

Ive just ried to run this code

Code: Select all
(defun add (number)
  (intern (format nil "*~a" number)))


in muLisp. but it retur error. I think it does not support format command. Is there a way to replace format by other code to get it work in muLisp?
Lispoman
 
Posts: 12
Joined: Mon Jun 01, 2009 6:09 pm

Re: Replacing elements in a list

Postby gugamilare » Wed Jun 03, 2009 8:36 am

Lispoman wrote:in muLisp. but it retur error. I think it does not support format command. Is there a way to replace format by other code to get it work in muLisp?


I don't know how to do this in muLisp.

But isn't muLisp a bit... old? It predates ANSI CL, doesn't it? You should consider porting your code to CL. There is a member of Lisp-br (you can find him in comp.lang.lisp as namekuseijin) that said about 2 months ago that he was creating / created a script in scheme that transforms muLisp code into CL code (what a mess). I don't know how efficient it works, since in muLisp it is very common to do

Code: Select all
(setq foo 30)


to create a global variable, while this is not the CL way to do it, although most times it will work. But now I am understanding the reason for your code to look so odd - at least to me, as I am used to CL :)
gugamilare
 
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil

Re: Replacing elements in a list

Postby Lispoman » Mon Jun 08, 2009 11:43 pm

After reading some philosophial and prectical books about lisp. :o After understanding some purposes of lambda functions and calls. :!: Guys! I still can't done my lamda call. I've modified a previous code :idea:
Now it looks like this
Code: Select all
(defun myfunc (x)
   (cond ((null x) nil)
      ((< (car x) 0) (cons (intern (format nil "*~a" (car x))) (myfunc (cdr x))))
      (T (cons (car x) (myfunc (cdr x))))))


Ive tried to figure a variant with lambda. I think it should be something like this

Code: Select all
(defun myfunc1 (x)
        (let ((lam (lambda () (myfunc (cdr x))))
            (cond ((null x) nil)
              ((evenp (car x)) (cons (funcall lam)))
               (T (cons (car x) (funcall lam))))

But its still just abstract thoughts. :roll:
Lispoman
 
Posts: 12
Joined: Mon Jun 01, 2009 6:09 pm

Re: Replacing elements in a list

Postby Paul Donnelly » Tue Jun 09, 2009 12:24 am

Lispoman wrote:
Code: Select all
(defun myfunc1 (x)
        (let ((lam (lambda () (myfunc (cdr x))))
            (cond ((null x) nil)
              ((evenp (car x)) (cons (funcall lam)))
               (T (cons (car x) (funcall lam))))

This is nuts. Try using lambda in a problem where it's actually applicable, and you'll have more luck.
Paul Donnelly
 
Posts: 148
Joined: Wed Jul 30, 2008 11:26 pm

Re: Replacing elements in a list

Postby Lispoman » Tue Jun 09, 2009 12:28 am

Thanks. But I need to modify this function with lambda
Lispoman
 
Posts: 12
Joined: Mon Jun 01, 2009 6:09 pm

Re: Replacing elements in a list

Postby Harleqin » Tue Jun 09, 2009 6:28 am

I think that your problem is that you don't know what you want to do.

What I read out of your last posts is that you want to apply some function to all members of a list. This function can be either pre-defined or created "on the fly" (by using LAMBDA). There is a built-in function MAPCAR (look it up at the HyperSpec) to do exactly this.

The functions you want to use seem to be something like:

Code: Select all
(defun mark-negative (x)
  (if (minusp x)
      (intern (format nil "*~a" x))
      x))


If you want to create this function anonymously with LAMBDA, the first line becomes "(lambda (x)", naturally.

You use MAPCAR like this:

Code: Select all
(mapcar #'mark-negative input-list)
;; or
(mapcar (lambda (x)
          ;; ...
          )
        input-list)
"Just throw more hardware at it" is the root of all evil.
Svante
Harleqin
 
Posts: 71
Joined: Wed Dec 17, 2008 5:18 am
Location: Bonn, Germany

Re: Replacing elements in a list

Postby slobodan.blazeski » Tue Jun 09, 2009 12:04 pm

Yeea! Solved. Nice working.

Code: Select all
(defun add (number)
(intern (format nil "*~a" number)))

(defun myfunc (x)
(cond ((null x) nil)
((< (car x) 0) (cons (add (car x)) (myfunc (cdr x))))
(T (cons (car x) (myfunc (cdr x))))))


Thanks

Now the question can lisp differ even and odd numbers? And how can the function above be modified to use with lambda?

Try oddp and evenp.
What exactly do you want to be modified? If you want to remove add definition just replace it with (lambda (number) (intern (format nil "*~a" number)))

bobi
slobodan.blazeski
 
Posts: 9
Joined: Tue May 26, 2009 8:27 am

Re: Replacing elements in a list

Postby Lispoman » Tue Jun 09, 2009 5:02 pm

Thanks but I've already combined two functions in a single one
Code: Select all
(defun myfunc (x)
   (cond ((null x) nil)
      ((< (car x) 0) (cons (intern (format nil "*~a" (car x))) (myfunc (cdr x))))
      (T (cons (car x) (myfunc (cdr x))))))


And now I need to modify this function with lambda.
Lispoman
 
Posts: 12
Joined: Mon Jun 01, 2009 6:09 pm

Re: Replacing elements in a list

Postby Paul Donnelly » Tue Jun 09, 2009 7:57 pm

Lispoman wrote:Thanks but I've already combined two functions in a single one
Code: Select all
(defun myfunc (x)
   (cond ((null x) nil)
      ((< (car x) 0) (cons (intern (format nil "*~a" (car x))) (myfunc (cdr x))))
      (T (cons (car x) (myfunc (cdr x))))))


And now I need to modify this function with lambda.

Why? Is this homework? I think you may have misunderstood the assignment.
Paul Donnelly
 
Posts: 148
Joined: Wed Jul 30, 2008 11:26 pm

PreviousNext

Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 3 guests