From: ········@gmail.com
Subject: list extraction from begin postion to end postion.
Date: 
Message-ID: <ae9c1073-63d3-434a-b9b5-f354ef9636f7@s13g2000prd.googlegroups.com>
hi, i'm very very new to learning lisp and really need your help to
writing a recursive function.
I'm trying to write code myself, but it's really hard for me.

What I wand to do is, making function that extract sub-list from
passed list with begin postion and item count.

Function definition is something like this ..

(defunc SubList (list begin cnt)
.....
)


and opeartion result is something like this..
(SubList '(1 2 3 4) 1 2)  returns (2 3)

Because I need to wirte a recursive function, I can't use variable and
functions below.
[Ioop, nth, nthcdr, tail, set, setq, setf, let..]

could you give me a hint ??

From: Marco Antoniotti
Subject: Re: list extraction from begin postion to end postion.
Date: 
Message-ID: <20da7167-b9e3-4059-93c1-9c175f15c93f@2g2000hsn.googlegroups.com>
On Mar 20, 10:20 am, ········@gmail.com wrote:
> hi, i'm very very new to learning lisp and really need your help to
> writing a recursive function.
> I'm trying to write code myself, but it's really hard for me.
>
> What I wand to do is, making function that extract sub-list from
> passed list with begin postion and item count.
>
> Function definition is something like this ..
>
> (defunc SubList (list begin cnt)
> .....
> )
>

It is 'defun' and not 'defunc'.  Secondly, *do not* use StudlyCaps
(unless you have achieved elightment and understanding of the read
tables :) ); call you function sub-list.



> and opeartion result is something like this..
> (SubList '(1 2 3 4) 1 2)  returns (2 3)
>
> Because I need to wirte a recursive function, I can't use variable and
> functions below.
> [Ioop, nth, nthcdr, tail, set, setq, setf, let..]
>
> could you give me a hint ??

Third.  This is already a built in.  It is called 'subseq'.

cl-prompt> (subseq '(1 2 3 4) 1 3)
(2 3)

This is because the last index is exclusive.

Cheers
--
Marco
From: danb
Subject: Re: list extraction from begin postion to end postion.
Date: 
Message-ID: <85ff31a6-d46f-4c70-9ad3-40c39371ae62@u69g2000hse.googlegroups.com>
On Mar 20, 4:20 am, ········@gmail.com wrote:
> hi, i'm very very new to learning lisp and really need
> your help to writing a recursive function.

Welcome to c.l.l :)

> I'm trying to write code myself, but it's really hard for me.

Take your time.  It's a little tricky at first, but you
get used to it eventually.

> Because I need to wirte a recursive function

Is this homework?  Try to do as much of it yourself as
possible.

> could you give me a hint ??

Here are a few general hints about recursive functions
on lists:

* When you first call your function, one of its arguments
  will be your list.
* Normally, you will examine the first element of the
  list, and then call your function again with the rest
  of the list.
* Usually, the first call to your function will combine
  the first list element and the result of your next
  function call in some way that makes sense for your
  problem.  You need to figure out what that is.
* At some point in the list, you will decide that you
  have what you need, and will return a value from your
  function instead of calling it again.
* Sometimes this occurs at the end of the list, and
  sometimes the value is *very* simple[1], for instance
  zero for computing sums and '() (the empty list) for
  constructing lists.
* Although your function examines the end of the list
  last, you should put the code that handles this case
  at the beginning of your function instead of the end.
  This isn't absolutely necessary, but it's good style.
  You'll like it.

Here are some hints specific to your problem:

* One of the universal methods in solving problems is
  to break them down into smaller subproblems.  In your
  case, the first problem is to find the begining of the
  subsequence, and the second problem is to return the
  specified number of elements starting from that point.
* One way to do this might be to define two separate
  functions, one for each phase of the problem.  The
  first function would call itself until it finds the
  beginning of the subsequence, and then it would call the
  other function to collect elements for the subsequence.
* Obviously there's no point in going all the way to the
  end of the full list once you have enough items.

One of the trickiest things about functional programming
is that you can't see the sequence of events right in front
of your eyes.  You have to think carefully about what hap-
pens when you call your function, then it calls itself with
different arguments, and then eventually one of those calls
returns a value, which is combined with the previous value,
etc., until the final result is returned from your initial
call.  You have to work all that stuff out in your head.
Don't worry, though.  It gets easier. :)

--Dan

[1] Specifically, the "seed" value is often the identity
for whatever operation you're using to combine elements.

------------------------------------------------
Dan Bensen
http://www.prairienet.org/~dsb/
From: ········@gmail.com
Subject: Re: list extraction from begin postion to end postion.
Date: 
Message-ID: <29da65fa-1300-4aca-a472-af2a77131cb7@u10g2000prn.googlegroups.com>
Thank you for all.

With all your help, I wrote below code,
Unfortunately, I'm still baby about LISP, I'm not sure that this code
is functionally good for LISP.


-----------------------------------------------------
[1] USER(1): (defun SubList (ListValue BeginPos cnt)


        (defun StartPoint (ListValue BeginPos cnt)
                (cond
                        ((>= (- BeginPos 1) 0) (StartPoint  (cdr
ListValue) (- BeginPos 1)
CountItem ))
                        ( ListValue )
                )
        )


        (cond
                ((>= (- cnt 1) 0)
                        (print (cons (car (StartPoint ListValue
BeginPos cnt))
                                        (SubList (cdr ListValue)
BeginPos (- cnt 1)) ) )
                )
                (NIL)
        )
)
SUBLIST
[1] USER(2): (sublist '(a b c d e f g h) 2 4)


(F)
(E F)
(D E F)
(C D E F)
(C D E F)
[1] USER(3):
-----------------------------------------------------------------------


one more question please..
how can I hide the process of making final result??
I want to show (C D E F) only.


thanks.
From: danb
Subject: Re: list extraction from begin postion to end postion.
Date: 
Message-ID: <6f41381c-94d2-4ea9-85b0-c384510c1dff@m36g2000hse.googlegroups.com>
On Mar 20, 11:25 am, ········@gmail.com wrote:
> how can I hide the process of making final result??
> I want to show (C D E F) only.

The simplest way is to start by defining a function
separately that returns the *first* N items of a list.
Don't print anything, and don't worry about the starting
point.  Just write a function that returns a sublist
starting at the beginning of the full list.

Then, when that function is done, write another function
that finds the starting point in a list, and calls your
first function with the starting point as the list.
Other than that one call, the two functions should be
separate.

--Dan

------------------------------------------------
Dan Bensen
http://www.prairienet.org/~dsb/
From: Thomas A. Russ
Subject: Re: list extraction from begin postion to end postion.
Date: 
Message-ID: <ymi4pb1doji.fsf@blackcat.isi.edu>
········@gmail.com writes:

> Thank you for all.
> 
> With all your help, I wrote below code,
> Unfortunately, I'm still baby about LISP, I'm not sure that this code
> is functionally good for LISP.

Well, for starters, you should have the definition of StartPoint be
outside the function definition for SubList.  There is a way to have
internally defined functions in Lisp (using LABELS), but you'll get to
that later.

> -----------------------------------------------------
> [1] USER(1): (defun SubList (ListValue BeginPos cnt)
> 
> 
>         (defun StartPoint (ListValue BeginPos cnt)
>                 (cond
>                         ((>= (- BeginPos 1) 0) (StartPoint  (cdr
> ListValue) (- BeginPos 1)
> CountItem ))
>                         ( ListValue )
>                 )
>         )
> 
> 
>         (cond
>                 ((>= (- cnt 1) 0)
>                         (print (cons (car (StartPoint ListValue
> BeginPos cnt))
>                                         (SubList (cdr ListValue)
> BeginPos (- cnt 1)) ) )
>                 )
>                 (NIL)
>         )
> )

And since someone else brought up formatting, it would be easier for us
to read and help you with if your code followed the standard lisp
conventions.  The compiler doesn't care, but we human programmers would
like it better if it looked like the code below.  I've also changed your
identifier names to more common Lisp forms.

             
Note that you don't actually use "CNT" in this function.  That is a lot
easier to see now that you have it separated out.

(defun start-point (list-value begin-pos cnt)
  (cond ((>= (- begin-pos 1) 0)
	 (start-point  (cdr list-value) (- begin-pos 1) cnt))
	(list-value)))

START-POINT somewhat accidentally works, but not for the reason you
might think it does.  The second branch of your COND statement uses the
return value as a test.  Now, it is quite possible that you will run off
the end of your list.  What happens if begin-pos > (length list-value) ?
In that case, the value of the test is NIL, so it will fail, but you get
saved by COND returning NIL in that case anyway.  Safer would be to use
the clause "(T list-value)" as the second element of the COND -- but
without the quotes around it.


(defun sublist (list-value being-pos cnt)
  (cond ((>= (- cnt 1) 0)
	 (cons (car (start-point list-value begin-pos cnt))
	       (sublist (cdr list-value) begin-pos (- cnt 1))))
	(NIL)))

Same general comment about the final clause of the COND.  For now, it is
perhaps sufficient to adopt a simple rule that the last clause of any
COND form should look like:

   (cond (...)
         (...)
         (t <some-value>))

where you put <some-value> in as appropriate for your situation.  In the
code above, it would be nil.


> SUBLIST
> [1] USER(2): (sublist '(a b c d e f g h) 2 4)
> 
> 
> (F)
> (E F)
> (D E F)
> (C D E F)
> (C D E F)
> [1] USER(3):
> -----------------------------------------------------------------------
> 
> 
> one more question please..
> how can I hide the process of making final result??
> I want to show (C D E F) only.

Answered by someone else.

Note also, that it helps in building good software to try a number of
different cases, in particular cases that test the limits of your code
and also that exceed the bounds of your expected input.

What happens if you try:

(sublist '(a b c d e f g h) 0 4)
(sublist '(a b c d e f g h) 2 1)
(sublist '(a b c d e f g h) 2 0)
(sublist '(a b c d e f g h) 4 4)
(sublist '(a b c d e f g h) 5 4)
(sublist '(a b c d e f g h) 10 4)



-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Pascal Bourguignon
Subject: Re: list extraction from begin postion to end postion.
Date: 
Message-ID: <878x0duppd.fsf@thalassa.informatimago.com>
········@gmail.com writes:


> [1] USER(1): (defun SubList (ListValue BeginPos cnt)
>
>
>         (defun StartPoint (ListValue BeginPos cnt)
>                 (cond
>                         ((>= (- BeginPos 1) 0) (StartPoint  (cdr
> ListValue) (- BeginPos 1)
> CountItem ))
>                         ( ListValue )
>                 )
>         ) ; <--- Lucky Luke, the lonesome cowboy...
>
>
>         (cond
>                 ((>= (- cnt 1) 0)
>                         (print (cons (car (StartPoint ListValue
> BeginPos cnt))
>                                         (SubList (cdr ListValue)
> BeginPos (- cnt 1)) ) )
>                 )
>                 (NIL)
>         )
> )


Let's use PPRINT to format your code correctly:


(DEFUN SUBLIST (LISTVALUE BEGINPOS COUNTITEM)
 (DEFUN STARTPOINT (LISTVALUE BEGINPOS COUNTITEM)
  (COND ((>= (- BEGINPOS 1) 0)
         (STARTPOINT (CDR LISTVALUE) (- BEGINPOS 1) COUNTITEM))
        (LISTVALUE)))
 (COND
  ((>= (- COUNTITEM 1) 0)
   (PRINT (CONS (CAR (STARTPOINT LISTVALUE BEGINPOS COUNTITEM))
                (SUBLIST (CDR LISTVALUE) BEGINPOS (- COUNTITEM 1)))))
  (NIL)))

This function defines a GLOBAL function named STARTPOINT.  Everytime
it is called!  This is not what you want.  You probably want a local
function, then use FLET or LABELS.  In this case, LABELS, since your
local function is recursive.



Note that:

For all a integer,

      a-1 >= 0 
<=>   0 <= a-1
<=>   1 <= a
<=>   0 < a

So it's much clearer to write: (plusp a) or even (< 0 a) than (>= (- a 1) 0)


Since  you only  have  two branches  in  your COND  it  would be  more
specific, hence clearer to use a  IF, the more so when you write
    (COND (test then) (LIST-VALUE))
which is correct, but not necessarily clear for all the readers.

It means that if LIST-VALUE is true, then return LIST-VALUE, otherwise
(ie. when it's NIL), return NIL.  
Or said otherwise, in all cases, return LIST-VALUE.  Then just say so!

    (IF test
        then
        LIST-VALUE)



(COND (test then) (NIL)) is even worse!  The last clause says that
when it's false, (ie. always) just skip to the default exit, which
returns NIL anyways.  If you want to be extra explicit on the NIL
result of your COND,  you could have written: (cond ... (T NIL)), but
since it has actually only one branch, a mere WHEN would be enough:

    (WHEN test 
      then)

WHEN returns NIL when test is false.


Which gives:

(defun sublist (list-value begin-pos count-item)
  (labels ((start-point (list-value begin-pos count-item)
             (if (plusp begin-pos)
                 (start-point (rest list-value) (1- begin-pos) count-item)
                 list-value)))
    (when (plusp count-item)
      (cons (first (start-point list-value begin-pos count-item))
            (sublist (rest list-value) begin-pos (1- count-item))))))


Now let's see if it works correctly:

C/USER1[168]> (sublist '(a b c d f) 0 0)
NIL                                         ; ok
C/USER1[169]> (sublist '(a b c d f) 0 1)
(A)                                         ; good
C/USER1[170]> (sublist '(a b c d f) 0 2)
(A B)
C/USER1[171]> (sublist '(a b c d f) 0 3)
(A B C)
C/USER1[172]> (sublist '(a b c d f) 0 4)
(A B C D)
C/USER1[173]> (sublist '(a b c d f) 0 5)
(A B C D F)                                 ; so far so good
C/USER1[174]> (sublist '(a b c d f) 0 6)
(A B C D F NIL)                             ; oops!  Was it
C/USER1[175]> (sublist '(a b c d f) 0 7)
(A B C D F NIL NIL)                         ; really what you
C/USER1[176]> (sublist '(a b c d f) 0 8)
(A B C D F NIL NIL NIL)                     ; wanted it to do?



C/USER1[187]> (sublist '(a b c d f) 3 1)
(D)                                         ; ok
C/USER1[188]> (sublist '(a b c d f) 4 1)
(F)                                         ; nice
C/USER1[189]> (sublist '(a b c d f) 5 1)
(NIL)                                       ; ???
C/USER1[190]> (sublist '(a b c d f) 6 1)
(NIL)                                       ; really?
C/USER1[191]> 



Note that amongst the simple cases I asked you to consider:

> For example, 
>
> (sub-list '()        i c) --> ()                                ; [a]
> (sub-list '(1 2 3 4) i 0) --> ()                                ; [b]
> (sub-list '(1 2 3 4) 0 1) --> (1)  = (list (first '(1 2 3 4)))    ; [c]

the first one answers to the question of what to do when you have an
empty list.  You could answer it differently, but you have to ask
yourself the question and answer it.  Is the result you get really
what you _intended_?



> one more question please..
> how can I hide the process of making final result??

By avoiding printing them.



Here is my solution.


C/USER1[197]> (defun sublist (list start length)
                (cond
                  ((endp list)    '())
                  ((zerop length) '())
                  ((plusp start) (sublist (rest list) (1- start) length))
                  (t (cons (first list) (sublist (rest list) 0 (1- length))))))
SUBLIST
C/USER1[198]> (loop :for i :from 0 :to 6
                    :do (loop :for j :from 0 :to 6
                              :do (format t "~S --> ~S~%"
                                          `(sublist '(a b c d e) ,i ,j)
                                           (sublist '(a b c d e) i j))))
(SUBLIST '(A B C D E) 0 0) --> NIL
(SUBLIST '(A B C D E) 0 1) --> (A)
(SUBLIST '(A B C D E) 0 2) --> (A B)
(SUBLIST '(A B C D E) 0 3) --> (A B C)
(SUBLIST '(A B C D E) 0 4) --> (A B C D)
(SUBLIST '(A B C D E) 0 5) --> (A B C D E)
(SUBLIST '(A B C D E) 0 6) --> (A B C D E)
(SUBLIST '(A B C D E) 1 0) --> NIL
(SUBLIST '(A B C D E) 1 1) --> (B)
(SUBLIST '(A B C D E) 1 2) --> (B C)
(SUBLIST '(A B C D E) 1 3) --> (B C D)
(SUBLIST '(A B C D E) 1 4) --> (B C D E)
(SUBLIST '(A B C D E) 1 5) --> (B C D E)
(SUBLIST '(A B C D E) 1 6) --> (B C D E)
(SUBLIST '(A B C D E) 2 0) --> NIL
(SUBLIST '(A B C D E) 2 1) --> (C)
(SUBLIST '(A B C D E) 2 2) --> (C D)
(SUBLIST '(A B C D E) 2 3) --> (C D E)
(SUBLIST '(A B C D E) 2 4) --> (C D E)
(SUBLIST '(A B C D E) 2 5) --> (C D E)
(SUBLIST '(A B C D E) 2 6) --> (C D E)
(SUBLIST '(A B C D E) 3 0) --> NIL
(SUBLIST '(A B C D E) 3 1) --> (D)
(SUBLIST '(A B C D E) 3 2) --> (D E)
(SUBLIST '(A B C D E) 3 3) --> (D E)
(SUBLIST '(A B C D E) 3 4) --> (D E)
(SUBLIST '(A B C D E) 3 5) --> (D E)
(SUBLIST '(A B C D E) 3 6) --> (D E)
(SUBLIST '(A B C D E) 4 0) --> NIL
(SUBLIST '(A B C D E) 4 1) --> (E)
(SUBLIST '(A B C D E) 4 2) --> (E)
(SUBLIST '(A B C D E) 4 3) --> (E)
(SUBLIST '(A B C D E) 4 4) --> (E)
(SUBLIST '(A B C D E) 4 5) --> (E)
(SUBLIST '(A B C D E) 4 6) --> (E)
(SUBLIST '(A B C D E) 5 0) --> NIL
(SUBLIST '(A B C D E) 5 1) --> NIL
(SUBLIST '(A B C D E) 5 2) --> NIL
(SUBLIST '(A B C D E) 5 3) --> NIL
(SUBLIST '(A B C D E) 5 4) --> NIL
(SUBLIST '(A B C D E) 5 5) --> NIL
(SUBLIST '(A B C D E) 5 6) --> NIL
(SUBLIST '(A B C D E) 6 0) --> NIL
(SUBLIST '(A B C D E) 6 1) --> NIL
(SUBLIST '(A B C D E) 6 2) --> NIL
(SUBLIST '(A B C D E) 6 3) --> NIL
(SUBLIST '(A B C D E) 6 4) --> NIL
(SUBLIST '(A B C D E) 6 5) --> NIL
(SUBLIST '(A B C D E) 6 6) --> NIL
NIL
C/USER1[199]> 

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

PLEASE NOTE: Some quantum physics theories suggest that when the
consumer is not directly observing this product, it may cease to
exist or will exist only in a vague and undetermined state.
From: Thomas F. Burdick
Subject: Re: list extraction from begin postion to end postion.
Date: 
Message-ID: <16862356-fb86-41b0-b79f-f42769ed7d48@u10g2000prn.googlegroups.com>
On Mar 20, 10:20 am, ········@gmail.com wrote:
> Function definition is something like this ..
>
> (defunc SubList (list begin cnt)
> .....
> )
>
> and opeartion result is something like this..
> (SubList '(1 2 3 4) 1 2)  returns (2 3)
>
> Because I need to wirte a recursive function, I can't use variable and
> functions below.
> [Ioop, nth, nthcdr, tail, set, setq, setf, let..]
>
> could you give me a hint ??

I'm always itching to help people with their homework, so I thought
I'd take a scratch at it.  I hope it's not rash.  Professionally I
program Lisp applications, so this is applicably applicative.  I
wasn't sure if reverse was
allowed, so I didn't use it.  Have fun!

(defun SubList (list begin cnt)
 ((lambda (b)
    ((lambda (f)
       ((lambda (l)
          ((lambda (m)
             (funcall f f list
                      (funcall l l b cnt
                               (funcall l l b begin (not (funcall
m))))
                      (not (funcall m))
                      (funcall l l b begin (not (funcall m)))))
           ((lambda (funcall lambda) (lambda () (funcall funcall
lambda lambda)))
            (lambda (funcall lambda) (lambda () (funcall funcall
lambda lambda)))
            (lambda (funcall lambda) (lambda () (funcall funcall
lambda lambda))))))
        (lambda (l i c e)
          (lambda ()
            (funcall
             (funcall
              (funcall
               (funcall i (lambda () (zerop c)))
               (lambda () e))
              (lambda () (lambda (lambda) lambda)))
             (lambda () (funcall l l i (1- c) e)))))))
     (lambda (f l e a s)
       (cond
         ((funcall s) (funcall f f (cdr l) (funcall e) a (funcall s)))
         ((and (null e) (null l)) a)
         ((null e) (funcall f f (cdr l) e (cons (car l) a) s))
         ((funcall e) (funcall f f (cdr l) (funcall e) (cons (car l)
a) s))
         (t (funcall f f a (funcall e) (funcall e) s))))))
  (lambda (b)
    (lambda (u)
      (lambda (g)
        (lambda (s)
          (cond ((funcall b) (funcall u)) ((funcall g) (funcall
s)))))))))

I hope Google Groups didn't mess that up.

CLL regulars : Should I be proud or ashamed that that only took a few
minutes to write?  I'm thinking ashamed...
From: vanekl
Subject: Re: list extraction from begin postion to end postion.
Date: 
Message-ID: <frucio$d8u$1@aioe.org>
Thomas F. Burdick wrote:
> On Mar 20, 10:20 am, ········@gmail.com wrote:
>> Function definition is something like this ..
>>
>> (defunc SubList (list begin cnt)
>> .....
>> )
>>
>> and opeartion result is something like this..
>> (SubList '(1 2 3 4) 1 2)  returns (2 3)
>>
>> Because I need to wirte a recursive function, I can't use variable and
>> functions below.
>> [Ioop, nth, nthcdr, tail, set, setq, setf, let..]
>>
>> could you give me a hint ??
> 
> I'm always itching to help people with their homework, so I thought
> I'd take a scratch at it.  I hope it's not rash.  Professionally I
> program Lisp applications, so this is applicably applicative.  I
> wasn't sure if reverse was
> allowed, so I didn't use it.  Have fun!
> 
> (defun SubList (list begin cnt)
>  ((lambda (b)
>     ((lambda (f)
>        ((lambda (l)
>           ((lambda (m)
>              (funcall f f list
>                       (funcall l l b cnt
>                                (funcall l l b begin (not (funcall
> m))))
>                       (not (funcall m))
>                       (funcall l l b begin (not (funcall m)))))
>            ((lambda (funcall lambda) (lambda () (funcall funcall
> lambda lambda)))
>             (lambda (funcall lambda) (lambda () (funcall funcall
> lambda lambda)))
>             (lambda (funcall lambda) (lambda () (funcall funcall
> lambda lambda))))))
>         (lambda (l i c e)
>           (lambda ()
>             (funcall
>              (funcall
>               (funcall
>                (funcall i (lambda () (zerop c)))
>                (lambda () e))
>               (lambda () (lambda (lambda) lambda)))
>              (lambda () (funcall l l i (1- c) e)))))))
>      (lambda (f l e a s)
>        (cond
>          ((funcall s) (funcall f f (cdr l) (funcall e) a (funcall s)))
>          ((and (null e) (null l)) a)
>          ((null e) (funcall f f (cdr l) e (cons (car l) a) s))
>          ((funcall e) (funcall f f (cdr l) (funcall e) (cons (car l)
> a) s))
>          (t (funcall f f a (funcall e) (funcall e) s))))))
>   (lambda (b)
>     (lambda (u)
>       (lambda (g)
>         (lambda (s)
>           (cond ((funcall b) (funcall u)) ((funcall g) (funcall
> s)))))))))
> 
> I hope Google Groups didn't mess that up.
> 
> CLL regulars : Should I be proud or ashamed that that only took a few
> minutes to write?  I'm thinking ashamed...


Teach me, Master.
:)
From: Pascal J. Bourguignon
Subject: Re: list extraction from begin postion to end postion.
Date: 
Message-ID: <7chcf1vg96.fsf@pbourguignon.anevia.com>
········@gmail.com writes:

> hi, i'm very very new to learning lisp and really need your help to
> writing a recursive function.
> I'm trying to write code myself, but it's really hard for me.
>
> What I wand to do is, making function that extract sub-list from
> passed list with begin postion and item count.
>
> Function definition is something like this ..
>
> (defunc SubList (list begin cnt)
> .....
> )
>
>
> and opeartion result is something like this..
> (SubList '(1 2 3 4) 1 2)  returns (2 3)
>
> Because I need to wirte a recursive function, I can't use variable and
> functions below.
> [Ioop, nth, nthcdr, tail, set, setq, setf, let..]
>
> could you give me a hint ??

Try to enumerate some simple cases, and then try to define more
complex cases in function of simplier cases.  "Simplier case" means
that you reduce the data, eg, you call sub-list recursively with (cdr
list) instead of list, or with (1- index) instead of index, etc.


For example, 

(sub-list '()        i c) --> ()                                ; [a]
(sub-list '(1 2 3 4) i 0) --> ()                                ; [b]
(sub-list '(1 2 3 4) 0 1) --> (1)  = (list (first '(1 2 3 4)))    ; [c]


So, now you can define (sub-list '(1 2 3 4) 1 1) = (sub-list (rest '(1 2 3 4)) (1- 1) 1)
and since (1- 1) = 0, we're back to case [c] above.

Then you can write your recursive function as:

(defun sub-list (list index length)
   (cond
      ( ...simple-case...    ...simple-result...)
      ...
      ( ...complex-case...  ...recursive-expression...)
      ...))


Like:

(defun sub-list (list index length)
   (cond
      ((endp list)           '())
      ( ...simple-case...    ...simple-result...)
      ...
      ((plusp index)         (sub-list (rest list) (1- index) length))
      ( ...complex-case...  ...recursive-expression...)
      ...))


-- 
__Pascal Bourguignon__
From: Alan Crowe
Subject: Re: list extraction from begin postion to end postion.
Date: 
Message-ID: <863aqlegvo.fsf@cawtech.freeserve.co.uk>
········@gmail.com writes:

> hi, i'm very very new to learning lisp and really need your help to
> writing a recursive function.
> I'm trying to write code myself, but it's really hard for me.
> 
> What I wand to do is, making function that extract sub-list from
> passed list with begin postion and item count.
> 
> Function definition is something like this ..
> 
> (defunc SubList (list begin cnt)
> .....
> )
> 
> 
> and opeartion result is something like this..
> (SubList '(1 2 3 4) 1 2)  returns (2 3)
> 
> Because I need to wirte a recursive function, I can't use variable and
> functions below.
> [Ioop, nth, nthcdr, tail, set, setq, setf, let..]
> 
> could you give me a hint ??

Put on your Dijkstra robe, and sit in your arm chair, with a
cup of tea, and write on a pad of paper, made from dead
tree, using a pen with real liquid ink. This is mathematics
not computation :-)

(sublist (a b c d e f g) 3 2) = (sublist (b c d e f g) 2 2)
                              = (sublist (c d e f g) 1 2)
                              = (sublist (d e f g) 0 2)
                              = (d . (sublist (e f g) 0 1))
                              = (d e . (sublist (f g) 0 0))
                              = (d e . nil)
                              = (d e)

compare this to peano arithmetic

(- 7 3) = (- 6 2)
        = (- 5 1)
        = (- 4 0)
        = 4

Recursive functions of symbolic expressions are not that
hard. The trouble starts when you go back to your computer
terminal and find that the REPL is much more finicky than
your pad of paper. You need to add operators, quote, cond,
cons,... and get all the syntax exactly correct. That is not
hard either. It is doing two things at once that kills you,
that is always harder than doing each separately.

That said, the mathematics of recursive functions of
symbolic expressions and the craft of programming are
mutually illuminating, but you have to be willing to play.

CL-USER> (defun count-them-off (list start)
           (unless (endp list)
             (print (cons (first list) start))
             (count-them-off (rest list)(+ start 1))))

CL-USER> (count-them-off '(a b c) 10)

(A . 10) 
(B . 11) 
(C . 12) 

NIL

The goal directed grind of the problem sheet is your
enemy. You need to make time for curiousity driven play
until you see what is going on in the very simplest cases.
From: Jason
Subject: Re: list extraction from begin postion to end postion.
Date: 
Message-ID: <27cfbdae-0205-48af-a449-5de6045c95b1@s37g2000prg.googlegroups.com>
On Mar 20, 5:09 am, Alan Crowe <····@cawtech.freeserve.co.uk> wrote:
> ········@gmail.com writes:
> > hi, i'm very very new to learning lisp and really need your help to
> > writing a recursive function.
> > I'm trying to write code myself, but it's really hard for me.
>
> > What I wand to do is, making function that extract sub-list from
> > passed list with begin postion and item count.
>
> > Function definition is something like this ..
>
> > (defunc SubList (list begin cnt)
> > .....
> > )
>
> > and opeartion result is something like this..
> > (SubList '(1 2 3 4) 1 2)  returns (2 3)
>
> > Because I need to wirte a recursive function, I can't use variable and
> > functions below.
> > [Ioop, nth, nthcdr, tail, set, setq, setf, let..]
>
> > could you give me a hint ??
>
> Put on your Dijkstra robe, and sit in your arm chair, with a
> cup of tea, and write on a pad of paper, made from dead
> tree, using a pen with real liquid ink. This is mathematics
> not computation :-)
>
> (sublist (a b c d e f g) 3 2) = (sublist (b c d e f g) 2 2)
>                               = (sublist (c d e f g) 1 2)
>                               = (sublist (d e f g) 0 2)
>                               = (d . (sublist (e f g) 0 1))
>                               = (d e . (sublist (f g) 0 0))
>                               = (d e . nil)
>                               = (d e)
>
> compare this to peano arithmetic
>
> (- 7 3) = (- 6 2)
>         = (- 5 1)
>         = (- 4 0)
>         = 4
>
> Recursive functions of symbolic expressions are not that
> hard. The trouble starts when you go back to your computer
> terminal and find that the REPL is much more finicky than
> your pad of paper. You need to add operators, quote, cond,
> cons,... and get all the syntax exactly correct. That is not
> hard either. It is doing two things at once that kills you,
> that is always harder than doing each separately.
>
> That said, the mathematics of recursive functions of
> symbolic expressions and the craft of programming are
> mutually illuminating, but you have to be willing to play.
>
> CL-USER> (defun count-them-off (list start)
>            (unless (endp list)
>              (print (cons (first list) start))
>              (count-them-off (rest list)(+ start 1))))
>
> CL-USER> (count-them-off '(a b c) 10)
>
> (A . 10)
> (B . 11)
> (C . 12)
>
> NIL
>
> The goal directed grind of the problem sheet is your
> enemy. You need to make time for curiousity driven play
> until you see what is going on in the very simplest cases.

Excellent post!
From: Jason
Subject: Re: list extraction from begin postion to end postion.
Date: 
Message-ID: <a41cc966-dcdf-4c3d-adf0-e96eeeb5c622@s37g2000prg.googlegroups.com>
On Mar 20, 2:20 am, ········@gmail.com wrote:
> hi, i'm very very new to learning lisp and really need your help to
> writing a recursive function.
> I'm trying to write code myself, but it's really hard for me.
>
> What I wand to do is, making function that extract sub-list from
> passed list with begin postion and item count.
>
> Function definition is something like this ..
>
> (defunc SubList (list begin cnt)
> .....
> )
>
> and opeartion result is something like this..
> (SubList '(1 2 3 4) 1 2)  returns (2 3)
>
> Because I need to wirte a recursive function, I can't use variable and
> functions below.
> [Ioop, nth, nthcdr, tail, set, setq, setf, let..]
>
> could you give me a hint ??

Write a function that takes a list and an index and returns the
elements of the list that occur on and after that index.

Write a function that takes a list and an index and returns the
elements of the list that occur before that index.

Combine the two.

Now make it better.

-Jason
From: John Thingstad
Subject: Re: list extraction from begin postion to end postion.
Date: 
Message-ID: <op.t8bul6y3ut4oq5@pandora.alfanett.no>
P� Thu, 20 Mar 2008 17:21:12 +0100, skrev <········@gmail.com>:

> Thank you for all.
>
> With all your help, I wrote below code,
> Unfortunately, I'm still baby about LISP, I'm not sure that this code
> is functionally good for LISP.
>
> -----------------------------------------------------
> [1] USER(1): (defun SubList (ListValue BeginPos CountItem )
>
> 	(defun StartPoint (ListValue BeginPos CountItem )
> 		(cond
> 			((>= (- BeginPos 1) 0) (StartPoint  (cdr ListValue) (- BeginPos 1)
> CountItem ))
> 			( ListValue )
> 		)
> 	)
>
> 	(cond
> 		((>= (- CountItem 1) 0)
> 			(print (cons (car (StartPoint ListValue BeginPos CountItem))
> 					(SubList (cdr ListValue) BeginPos (- CountItem 1)) ) )
> 		)
> 		(NIL)
> 	)
> )
> MYSUBLIST
> [1] USER(2): (mysublist '(a b c d e f g h) 2 4)
>
> (F)
> (E F)
> (D E F)
> (C D E F)
> (C D E F)
> [1] USER(3):
> -----------------------------------------------------------------------
>
> one more question please..
> how can I hide the process of making final result??
> I want to show (C D E F) only.
>
> thanks.

doh.. Don't print the process?
That is delete the 'print' and the (NIL) and print the result instead.  
(print (mysublist...))
Also your parenthesises and indentation are all wrong.. Just put them at  
the end of the expression not on separate lines. Let the editor do the  
indentation..

--------------
John Thingstad