Page 2 of 3

Re: Replacing elements in a list

Posted: Wed Jun 03, 2009 1:21 am
by Paul Donnelly
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.

Re: Replacing elements in a list

Posted: Wed Jun 03, 2009 5:59 am
by Lispoman
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?

Re: Replacing elements in a list

Posted: Wed Jun 03, 2009 8:36 am
by gugamilare
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 :)

Re: Replacing elements in a list

Posted: Mon Jun 08, 2009 11:43 pm
by Lispoman
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:

Re: Replacing elements in a list

Posted: Tue Jun 09, 2009 12:24 am
by Paul Donnelly
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.

Re: Replacing elements in a list

Posted: Tue Jun 09, 2009 12:28 am
by Lispoman
Thanks. But I need to modify this function with lambda

Re: Replacing elements in a list

Posted: Tue Jun 09, 2009 6:28 am
by Harleqin
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)

Re: Replacing elements in a list

Posted: Tue Jun 09, 2009 12:04 pm
by slobodan.blazeski
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

Re: Replacing elements in a list

Posted: Tue Jun 09, 2009 5:02 pm
by Lispoman
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.

Re: Replacing elements in a list

Posted: Tue Jun 09, 2009 7:57 pm
by Paul Donnelly
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.