From: ············@aol.com
Subject: A really easy recursion question with lisp
Date: 
Message-ID: <8q08fv$2e4$1@nnrp1.deja.com>
I have to write three really simple lisp functions that can do the
following. If anybody can help me I would be very thankful.

I need to write a Lisp function replicate which takes two arguments,
the first an expression and the second a non-negative
     integer. It returns a list containing the expression copied the
given number of times.

    (replicate 'a 4)                            ==> (a a a a)
    (replicate '(magic) 0)                      ==> nil
    (replicate '(1 2) 3)                        ==> ((1 2) (1 2) (1 2))

Also, I need to write a Lisp function multiples that returns a list of
the multiply occurring atoms in x, a list of symbolic atoms. The order
     of the elements in the result list is not important.

             (multiples (z b c z d z x b))                 ==> (b z)


Finally, i need to write a Lisp function my-subst that takes three
parameters: the new item, the old item to be changed and the list to be
edited. And the function will return a new version of the list with all
teh occurances of the old item replaced with the new item.

(my-subst 3 1 '(1 2 (1 2 (1 2))))             ==> (3 2 (3 2 (3 2)))



THANK YOU VERY MUCH


Sent via Deja.com http://www.deja.com/
Before you buy.

From: Johan Kullstam
Subject: Re: A really easy recursion question with lisp
Date: 
Message-ID: <m2hf7gtf3b.fsf@euler.axel.nom>
············@aol.com writes:

> I have to write three really simple lisp functions that can do the
> following. If anybody can help me I would be very thankful.

is this a homework exercise by any chance?

> I need to write

  ^
  yes, *you* do.

> a Lisp function replicate which takes two arguments,
> the first an expression and the second a non-negative
>      integer. It returns a list containing the expression copied the
> given number of times.
> 
>     (replicate 'a 4)                            ==> (a a a a)
>     (replicate '(magic) 0)                      ==> nil
>     (replicate '(1 2) 3)                        ==> ((1 2) (1 2) (1
>     2))

i'd use dotimes and push.  why don't you post your guess as to how to
solve this and let us comment?

> Also, I need to write a Lisp function multiples that returns a list of
> the multiply occurring atoms in x, a list of symbolic atoms. The order
>      of the elements in the result list is not important.
> 
>              (multiples (z b c z d z x b))                 ==> (b z)

> Finally, i need to write a Lisp function my-subst that takes three
> parameters: the new item, the old item to be changed and the list to be
> edited. And the function will return a new version of the list with all
> teh occurances of the old item replaced with the new item.
> 
> (my-subst 3 1 '(1 2 (1 2 (1 2))))             ==> (3 2 (3 2 (3 2)))
> 
> 
> 
> THANK YOU VERY MUCH
> 
> 
> Sent via Deja.com http://www.deja.com/
> Before you buy.

-- 
J o h a n  K u l l s t a m
[········@ne.mediaone.net]
Don't Fear the Penguin!
From: Tim Bradshaw
Subject: Re: A really easy recursion question with lisp
Date: 
Message-ID: <ey3n1h8w39s.fsf@cley.com>
* maxoutmaxima  wrote:

>     (replicate 'a 4)                            ==> (a a a a)
>     (replicate '(magic) 0)                      ==> nil
>     (replicate '(1 2) 3)                        ==> ((1 2) (1 2) (1 2))

(defun replicate (a n)
  ((lambda (f)
     (funcall f f n '()))
   (lambda (c n s)
     (if (zerop n)
	 s
	 (funcall c c (- n 1) (cons a s))))))
       
The others are similar.

--tim
From: Lieven Marchand
Subject: Re: A really easy recursion question with lisp
Date: 
Message-ID: <m3u2bguodk.fsf@localhost.localdomain>
············@aol.com writes:

> I have to write three really simple lisp functions that can do the
> following. If anybody can help me I would be very thankful.
> 

I don't guarantee your teacher is going to be very happy with these
answers but here they are:

> I need to write a Lisp function replicate which takes two arguments,
> the first an expression and the second a non-negative
>      integer. It returns a list containing the expression copied the
> given number of times.
> 
>     (replicate 'a 4)                            ==> (a a a a)
>     (replicate '(magic) 0)                      ==> nil
>     (replicate '(1 2) 3)                        ==> ((1 2) (1 2) (1 2))
> 

(defun replicate (item times)
  (make-list times :initial-element item))

> Also, I need to write a Lisp function multiples that returns a list of
> the multiply occurring atoms in x, a list of symbolic atoms. The order
>      of the elements in the result list is not important.
> 
>              (multiples (z b c z d z x b))                 ==> (b z)
> 

(defun multiples (seq)
  (let ((result seq))
    (dolist (item (remove-duplicates seq))
      (setf result (delete item result :count 1)))
    (remove-duplicates result)))

> 
> Finally, i need to write a Lisp function my-subst that takes three
> parameters: the new item, the old item to be changed and the list to be
> edited. And the function will return a new version of the list with all
> teh occurances of the old item replaced with the new item.
> 
> (my-subst 3 1 '(1 2 (1 2 (1 2))))             ==> (3 2 (3 2 (3 2)))
> 

I think you mean a tree instead of a list.

(defun my-subst (new old tree)
  (nsubst new old (copy-tree tree)))

Perhaps you could show us what YOU have already done?

-- 
Lieven Marchand <···@bewoner.dma.be>
Lambda calculus - Call us a mad club
From: Frank A. Adrian
Subject: Re: A really easy recursion question with lisp
Date: 
Message-ID: <X1Qw5.451$Wt3.260243@news.uswest.net>
<············@aol.com> wrote in message ·················@nnrp1.deja.com...
> I have to write three really simple lisp functions that can do the
> following. If anybody can help me I would be very thankful.
>
> I need to write a Lisp function replicate which takes two arguments,
> the first an expression and the second a non-negative
>      integer. It returns a list containing the expression copied the
> given number of times.
>
>     (replicate 'a 4)                            ==> (a a a a)
>     (replicate '(magic) 0)                      ==> nil
>     (replicate '(1 2) 3)                        ==> ((1 2) (1 2) (1 2))
>

Here's a hint.  First write a function that returns 0 copies of the item.
Now assume you already have a function that returns n copies of the item.
How would you write a function - based on the results of the existing
function - that returns n+1 copies.  Now combine the functions so that the
first gets called when you need 0 copies and the second gets called when you
need n copies.

> Also, I need to write a Lisp function multiples that returns a list of
> the multiply occurring atoms in x, a list of symbolic atoms. The order
>      of the elements in the result list is not important.
>
>              (multiples (z b c z d z x b))                 ==> (b z)
>
>
> Finally, i need to write a Lisp function my-subst that takes three
> parameters: the new item, the old item to be changed and the list to be
> edited. And the function will return a new version of the list with all
> teh occurances of the old item replaced with the new item.
>
> (my-subst 3 1 '(1 2 (1 2 (1 2))))             ==> (3 2 (3 2 (3 2)))
>
If you still have problems after trying your own hand at these first, write
again.

faa
From: ············@aol.com
Subject: Re: A really easy recursion question with lisp
Date: 
Message-ID: <8q18ao$533$1@nnrp1.deja.com>
Finally, i need to write a Lisp function my-subst that takes three
parameters: the new item, the old item to be changed and the list to be
edited. And the function will return a new version of the list with all
teh occurances of the old item replaced with the new item.

my-subst 3 1 '(1 2 (1 2 (1 2))))             ==> (3 2 (3 2 (3 2))

Okay, I have come up with for the solution above, But when it goes
through the tree, it only converts stuff on the first level. How would
i modify it to traverse through the entire tree?

(defun my-subst (new old input-list)
	(reverse (my-subst-helper new old input-list nil)))

(defun my-subst-helper (new old input-list result)
		(cond ((null input-list) result)
	        ((eql old (first input-list))
	        (my-subst-helper new old
               	        (rest input-list) (cons new result)))
		        (T (my-subst-helper new old (rest input-list)
                                (cons (first input-list) result)))))





Sent via Deja.com http://www.deja.com/
Before you buy.
From: Hartmann Schaffer
Subject: Re: A really easy recursion question with lisp
Date: 
Message-ID: <8q3657$ha7$1@paradise.nirvananet>
In article <············@nnrp1.deja.com>,  <············@aol.com> wrote:
>Finally, i need to write a Lisp function my-subst that takes three
>parameters: the new item, the old item to be changed and the list to be
>edited. And the function will return a new version of the list with all
>teh occurances of the old item replaced with the new item.
>
>my-subst 3 1 '(1 2 (1 2 (1 2))))             ==> (3 2 (3 2 (3 2))
>
>Okay, I have come up with for the solution above, But when it goes
>through the tree, it only converts stuff on the first level. How would
>i modify it to traverse through the entire tree?

think about the possibilities in case the the first two checks fail:
what can yeo do if the element is not an atom?

>(defun my-subst (new old input-list)
>	(reverse (my-subst-helper new old input-list nil)))
>
>(defun my-subst-helper (new old input-list result)
>		(cond ((null input-list) result)
>	        ((eql old (first input-list))
>	        (my-subst-helper new old
>               	        (rest input-list) (cons new result)))

do the following step only if the element is an atom, and do the
remaining stuff otherwise

>		        (T (my-subst-helper new old (rest input-list)
>                                (cons (first input-list) result)))))

hs
From: Reini Urban
Subject: Re: A really easy recursion question with lisp
Date: 
Message-ID: <39c5fa45.1037948270@judy>
Frank A. Adrian wrote:
>Here's a hint.  First write a function that returns 0 copies of the item.
>Now assume you already have a function that returns n copies of the item.
>How would you write a function - based on the results of the existing
>function - that returns n+1 copies.  Now combine the functions so that the
>first gets called when you need 0 copies and the second gets called when you
>need n copies.

is there really a need to replicate exactly tim's answer? :)

and the more I look at Y the more it like it better than the obvious
solution. obfuscation by lambda. for a lisp-1 it is of course much
easier, esp. for students in their first week:

(defun replicate (a n)  ; resp. (define (replicate a n) in scheme
  ((lambda (f)
     (f f n '()))
   (lambda (c n s)
     (if (zerop n)	; resp. zero? in scheme
	 s
       (c c (- n 1) (cons a s))))))
From: Frank A. Adrian
Subject: Re: A really easy recursion question with lisp
Date: 
Message-ID: <B0Ax5.818$C55.282855@news.uswest.net>
"Reini Urban" <······@xarch.tu-graz.ac.at> wrote in message
························@judy...
> Frank A. Adrian wrote:
> >Here's a hint.  First write a function that returns 0 copies of the item.
> >Now assume you already have a function that returns n copies of the item.
> >How would you write a function - based on the results of the existing
> >function - that returns n+1 copies.  Now combine the functions so that
the
> >first gets called when you need 0 copies and the second gets called when
you
> >need n copies.
>
> is there really a need to replicate exactly tim's answer? :)

I plead the asynchronicity of Usenet and the synchronicity of tim's and my
thoughts.

faa
From: Tim Bradshaw
Subject: Re: A really easy recursion question with lisp
Date: 
Message-ID: <ey3og1lx3yl.fsf@cley.com>
* Reini Urban wrote:

> and the more I look at Y the more it like it better than the obvious
> solution. obfuscation by lambda. for a lisp-1 it is of course much
> easier, esp. for students in their first week:

I've considered prefixing my solutions with a readmacro definition
which lets you write (funcall x ...) as [x ...].

> (defun replicate (a n)  ; resp. (define (replicate a n) in scheme
>   ((lambda (f)
>      (f f n '()))
>    (lambda (c n s)
>      (if (zerop n)	; resp. zero? in scheme
> 	 s
>        (c c (- n 1) (cons a s))))))

There's a term project here, which might even be quite interesting:
write an obfuscator.  At least transform LET into LAMBDA, preferably
transform looping constructs into obscure recursive ones.  Rename
bound variables to single letters.  The end result will be of lasting
utility to c.l.l, as writing these things by hand is a real pain.  Not
to mention it teaches you a lot about Lisp and compilation and stuff.

I can see the posts now: `I have to write lisp functions that can do the
following: (1) convert LET to LAMBDA ...'.

--tim