From: Stormkid
Subject: Help with LISP function
Date: 
Message-ID: <3fb9bf60$0$102$8f4e7992@newsreader.goldengate.net>
Hi Group, I'm in a programming class and I'm really having a hard time
understanding the semantics and syntax of LISP the function I'm trying to
write is below and was wondering if anyone in this group could point me in
the right direction.
Thanks Stormkid

;f() one_list combines two list in to one list
(defun one_list (list1 list2)
  (if (null list1)
    ()
    (MEMBER (first list1) list2)
    (one_list (rest list1) list2)
    (CONS (first list1)
    (one_list (rest list1) list2))
  )
)

From: Kenny Tilton
Subject: Re: Help with LISP function
Date: 
Message-ID: <Vgjub.117879$Gq.16037020@twister.nyc.rr.com>
Stormkid wrote:
> Hi Group, I'm in a programming class and I'm really having a hard time
> understanding the semantics and syntax of LISP the function I'm trying to
> write is below and was wondering if anyone in this group could point me in
> the right direction.
> Thanks Stormkid
> 
> ;f() one_list combines two list in to one list
> (defun one_list (list1 list2)
>   (if (null list1)
>     ()
>     (MEMBER (first list1) list2)
>     (one_list (rest list1) list2)
>     (CONS (first list1)
>     (one_list (rest list1) list2))
>   )
> )

Stormy, this is very bizarre. I could type about ten keystrokes and have 
this working. But what you have here is effectively a weird combo of IF 
and COND, between which you segue midstream as if there were some 
IF-ELSE-COND keyword.

Use IF all the way down, or use COND from the get-go, then I think 
you'll be fine -- the code I see you trying to write is fine.

kenny

-- 
http://tilton-technology.com

Why Lisp? http://alu.cliki.net/RtL%20Highlight%20Film

Your Project Here! http://alu.cliki.net/Industry%20Application
From: Peter Seibel
Subject: Re: Help with LISP function
Date: 
Message-ID: <m34qx2goic.fsf@javamonkey.com>
"Stormkid" <·······@goldengate.net> writes:

> Hi Group, I'm in a programming class and I'm really having a hard time
> understanding the semantics and syntax of LISP the function I'm trying to
> write is below and was wondering if anyone in this group could point me in
> the right direction.
> Thanks Stormkid
> 
> ;f() one_list combines two list in to one list
> (defun one_list (list1 list2)
>   (if (null list1)
>     ()
>     (MEMBER (first list1) list2)
>     (one_list (rest list1) list2)
>     (CONS (first list1)
>     (one_list (rest list1) list2))
>   )
> )

For starters, the form of an IF expression is:

  (if test then-form else-form)

What you have doesn't follow that pattern since you have three (poorly
indented) forms in the place where the else-form goes. I'm guessing
that you're trying to do is to do one thing when (null list1) is true,
one thing when (member (first list1) list2) is true and a third thing
otherwise. In that case you need to use nested IF clauses or perhaps
COND which is designed for expressing multi-way conditionals.

-Peter

-- 
Peter Seibel                                      ·····@javamonkey.com

         Lisp is the red pill. -- John Fraser, comp.lang.lisp
From: Joe Marshall
Subject: Re: Help with LISP function
Date: 
Message-ID: <zneurpj6.fsf@comcast.net>
"Stormkid" <·······@goldengate.net> writes:

> Hi Group, I'm in a programming class and I'm really having a hard time
> understanding the semantics and syntax of LISP the function I'm trying to
> write is below and was wondering if anyone in this group could point me in
> the right direction.
> Thanks Stormkid
>
> ;f() one_list combines two list in to one list
> (defun one_list (list1 list2)
>   (if (null list1)
>     ()
>     (MEMBER (first list1) list2)
>     (one_list (rest list1) list2)
>     (CONS (first list1)
>     (one_list (rest list1) list2))
>   )
> )

Close, but you want COND, not IF.
Try thinking of COND like this:

(cond (<if> (null list1) <then> '())
      (<else if> (member (first list1) list2)
             <then> (one-list (rest list1) list2))
      ...etc....

now take out the thought bubbles:

(cond ((null list1) <then> '())
      ((member (first list1) list2) (one-list (rest list1) list2))
      ...etc....


-- 
~jrm
From: Fred Gilham
Subject: Re: Help with LISP function
Date: 
Message-ID: <u7d6bp7bk8.fsf@snapdragon.csl.sri.com>
Apart from comments others have made, seems like the following is wrong:

> (defun one_list (list1 list2)
>   (if (null list1)
>     ()
>     <etc.>

Don't you want the following?

(defun one_list (list1 list2)
  (if (null list1)
      list2
      <etc.>

That is, if list1 is empty, the result of combining them into one list
is list2.


Anyway, if you convert to cond like others suggested, it'd look
something like this:

(defun one-list (list1 list2)
  (cond ((null list1)
	 list2)
        ((MEMBER (first list1) list2)
         (one-list (rest list1) list2))
        (t
         (CONS (first list1) (one-list (rest list1) list2)))))

Note the indentation, and lack of trailing parens, which are
considered by most (but not all) experienced lisp programmers to be
poor style.  Note also the use of the name one-list instead of
one_list, since there's no point in hitting the shift key
unnecessarily --- very few experienced lisp programmers use
underscores in the names they choose.

-- 
Fred Gilham                     ······@csl.sri.com
"In the 20th century, more citizens were killed by their own
governments than by foreign enemies....totalitarianism first of all
regards its own people as the enemy." --- Arnold Beichman
From: Alan Crowe
Subject: Re: Help with LISP function
Date: 
Message-ID: <86u1505euj.fsf@cawtech.freeserve.co.uk>
I've see several good answers to your specific question.
Here is a more general hint about learning strategy: Use the REPL,
the Read-Eval-Print-Loop. Revert to childhood and play. For
example

* (if t 1 2)
1

* (if nil 1 2)
2

* (cond (t 1)
	(t 2)
	(t 3))
1

* (cond (nil 1)
	(t 2)
	(t 3))
2

* (cond (nil 1)
	(nil 2)
	(t 3))
3

* (cond (nil 1)
	(nil 2)
	(nil 3))
NIL

* (defparameter *test-list* '(a b d e))
*TEST-LIST*

* (if (member 'a *test-list*) *test-list* (cons 'a *test-list*))
(A B D E)

* (if (member 'c *test-list*) *test-list* (cons 'c *test-list*))
(C A B D E)

All sorts of confusions can miraculously evapourate in the
course of 5 minutes play. For example:

* (let ((a 5)(b 7))
    (print '(cons a b))
    (print (cons a b))
    (print (cons 'a 'b))
    (print (car '(cons a b)))
    (print (car (cons a b))))

(CONS A B) 
(5 . 7) 
(A . B) 
CONS 
5 
5

The REPL will make learning LISP easy, if you let it.

Alan Crowe