I couldn't resist tinkering with this a bit more.

# ------------------------------------------------------
(de selectN (Lst P)
   (let selectNN
      '((Lst P I)
         (cond
            ((= 'NIL Lst) 'NIL)
            ((P (car Lst) I)
               (cons
                  (car Lst)
                  (selectNN (cdr Lst) P (inc I)) ) )
            (T (selectNN (cdr Lst) P (inc I))) ) )
      (selectNN Lst P 1) ) )
# ------------------------------------------------------

Same code, I just realized Picolisp (= code data) allows me to move the
recursing function inside the initializing one.
The results are the same, but now the recursing function is effectively
private.

: (selectN '(a 0 b 1 c 2 d 3 e 4 f 5 g 6 h 7 i 8 j 9 k 10) '((V I) (=0 (%
(inc I) 2))))
-> (a b c d e f g h i j k)
: (selectN '(a 0 b 1 c 2 d 3 e 4 f 5 g 6 h 7 i 8 j 9 k 10) '((V I) (gt0 (%
(inc I) 2))))
-> (0 1 2 3 4 5 6 7 8 9 10)
: (selectN '(a 0 b 1 c 2 d 3 e 4 f 5 g 6 h 7 i 8 j 9 k 10) '((V I) (sym?
V)))
-> (a b c d e f g h i j k)
: (selectN '(a 0 b 1 c 2 d 3 e 4 f 5 g 6 h 7 i 8 j 9 k 10) '((V I) (num?
V)))
-> (0 1 2 3 4 5 6 7 8 9 10)

/Lindsay



On Mon, Feb 6, 2017 at 5:04 PM, Lindsay John Lawrence <
lawrence.lindsayj...@gmail.com> wrote:

>
> Once I started to get the hang of the basic building blocks, I started
> seeing patterns and ways to build on what I had written before....
> e.g. To me, the next logical development of dropN, pickN is to allow
> providing a predicate function... effectively making it a general purpose
> list filter....
>
> # ---------------------------------------------------------
> (de selectNN (Lst P I)
>   # (println Lst I)
>   (cond
>     ((= '() Lst) '() )
>     ((P (car Lst) I) (cons (car Lst) (selectNN (cdr Lst) P (inc I))))
>     (T (selectNN (cdr Lst) P (inc I))) ) )
>
> (de selectN (Lst P)
>   (selectNN Lst P 1) )
>
> # ---------------------------------------------------------
> Compare selectN to dropN and pickN.
> Now you can write code like this....
> : (selectN '(a 0 b 1 c 2 d 3 e 4 f 5 g 6 h 7 i 8 j 9 k 10) '((V I) (sym?
> V)))
> -> (a b c d e f g h i j k)
> : (selectN '(a 0 b 1 c 2 d 3 e 4 f 5 g 6 h 7 i 8 j 9 k 10) '((V I) (num?
> V)))
> -> (0 1 2 3 4 5 6 7 8 9 10)
> : (selectN '(a 0 b 1 c 2 d 3 e 4 f 5 g 6 h 7 i 8 j 9 k 10) '((V I) (=0 (%
> (inc I) 2))))
> -> (a b c d e f g h i j k)
> : (selectN '(a 0 b 1 c 2 d 3 e 4 f 5 g 6 h 7 i 8 j 9 k 10) '((V I) (gt0 (%
> (inc I) 2))))
> -> (0 1 2 3 4 5 6 7 8 9 10)
>
>
> Lego my Lisp :)
>
> /Lindsay
>
>
>
>
> On Mon, Feb 6, 2017 at 4:18 PM, Lindsay John Lawrence <
> lawrence.lindsayj...@gmail.com> wrote:
>
>> All of those problems are fun to explore... try writing different
>> versions of the selected solutions, iterative vs recursive, using just
>> basic list building block functions vs the wonderfully convenient functions
>> that picolisp provides, etc
>>
>> This particular one piqued my interest enough to try writing another
>> version of 'drop' that is recursive and just uses *car, cdr, cons*...
>>
>> # -----------------------------------------------------------------
>> (de dropNN (Lst N I)
>>    # (println Lst N I (% I N))
>>    (cond
>>       ((= '() Lst) '() )
>>       ((gt0 (% I N)) (cons (car Lst) (dropNN (cdr Lst) N (inc I))))
>>       (T (dropNN (cdr Lst) N (inc I))) ) )
>>
>> (de *dropN *(Lst N)
>>    (dropNN Lst N 1) )
>>
>>
>> : (dropN '(a b c d e f g h i j k) 2)
>> -> (a c e g i k)
>>
>> # -----------------------------------------------------------------
>>
>> Having written that, it is relatively easy to tinker out something
>> similar that 'picks' every Nth element...
>>
>> # -----------------------------------------------------------------
>> (de pickNN (Lst N I)
>>    # (println Lst N I (% I N))
>>    (cond
>>       ((= '() Lst) '() )
>>       ((=0 (% I N)) (cons (car Lst) (pickNN (cdr Lst) N (inc I))) )
>>       (T (pickNN (cdr Lst) N (inc I))) ) )
>>
>> (de *pickN *(Lst N)
>>    (getNN Lst N 1) )
>>
>> : (pickN '(a b c d e f g h i j k) 2)
>> -> (b d f h j)
>>
>> # -----------------------------------------------------------------
>>
>> Lisp is, at least for me, one of the most fun languages to explore in
>> this way.
>>
>> It may seem slow, but I believe the time I have spent playing with basic
>> lisp building blocks, and simple list manipulation problems like this one,
>> is time well spent. The insight gained carries over into so many other
>> problems and their solutions.
>>
>> /Lindsay
>>
>>
>>
>> On Mon, Feb 6, 2017 at 2:51 PM, dean <deangwillia...@gmail.com> wrote:
>>
>>> Oh gosh...I missed that completely...Thanks Lindsay..That explains
>>> everything!
>>> I'm really pleased you told me that because drop looks like a really
>>> useful function.
>>> Best Regards
>>> Dean
>>>
>>> On 6 February 2017 at 22:27, Lindsay John Lawrence <
>>> lawrence.lindsayj...@gmail.com> wrote:
>>>
>>>> P16 (**) Drop every N’th element from a list.
>>>>
>>>> (de drop (Lst N)
>>>>   (make
>>>>     (for (I . X) Lst
>>>>       (unless (=0 (% I N))
>>>>       (link X) ) ) ) )
>>>>
>>>> : (drop ’(a b c d e f g h i k) 3)
>>>> -> (a b d e g h k)
>>>>
>>>> 'drop' is the function given as a solution to the problem.
>>>>
>>>> /Lindsay
>>>>
>>>>
>>>> On Mon, Feb 6, 2017 at 1:24 PM, dean <deangwillia...@gmail.com> wrote:
>>>>
>>>>> Hi Alex
>>>>>    : (filter prog2 (1 a 2 b 3 c) '(T NIL .))
>>>>>    -> (1 2 3)
>>>>>    : (filter prog2 (1 a 2 b 3 c) '(NIL T .))
>>>>>    -> (a b c)
>>>>>
>>>>> Yes the above is exactly what I'm after.
>>>>>
>>>>> I copied this drop example straight from ninety nine.
>>>>> ? P16 (**) Drop every N'th element from a list.
>>>>> : (drop '(a b c d e f g h i k) 3)
>>>>> !? (drop '(a b c d e f g h i k) 3)
>>>>> drop -- Undefined
>>>>>
>>>>> I'm not sure why I'm getting "undefined".
>>>>> I'm using the pil in my picolisp directory.... which looks like
>>>>> this....
>>>>> exec ${0%/*}/bin/picolisp ${0%/*}/lib.l @ext.l "$@"
>>>>> i.e. not the one in /bin which contains /usr,,,, which I think ISN'T
>>>>> local and I think I compiled a local version.
>>>>>
>>>>> Irrespective....thank you very much for your solution.
>>>>>
>>>>> Best Regards Dean
>>>>>
>>>>>
>>>>> On 6 February 2017 at 15:24, Alexander Burger <a...@software-lab.de>
>>>>> wrote:
>>>>>
>>>>>> On Mon, Feb 06, 2017 at 12:25:14PM +0100, Alexander Burger wrote:
>>>>>> > > I'd like to split a list '(txt1 2 txt2 6....
>>>>>> > > into 2 lists
>>>>>> > > '(txt1 txt2...
>>>>>> > > and
>>>>>> > > '(2 6
>>>>>> >
>>>>>> > You could for example filter them:
>>>>>> >
>>>>>> >    (let
>>>>>> >       (Lst '(txt1 2 txt2 6)
>>>>>> >          A (filter sym? Lst)
>>>>>> >          B (filter num? Lst) )
>>>>>> >       ... use A and B ...)
>>>>>>
>>>>>> It is not clear what you need.
>>>>>>
>>>>>>
>>>>>> If, for example, you want every *second* element, there is a nice
>>>>>> trick:
>>>>>>
>>>>>>    : (filter prog2 (1 a 2 b 3 c) '(T NIL .))
>>>>>>    -> (1 2 3)
>>>>>>
>>>>>>    : (filter prog2 (1 a 2 b 3 c) '(NIL T .))
>>>>>>    -> (a b c)
>>>>>>
>>>>>> Similarly, you can extract other sequences if you pass the right
>>>>>> pattern of
>>>>>> NIL's and T's.
>>>>>>
>>>>>> ♪♫ Alex
>>>>>> --
>>>>>> UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
>>>>>>
>>>>>
>>>>>
>>>>
>>>
>>
>

Reply via email to