From: His kennyness on
Pascal Costanza wrote:
> On 28/04/2010 13:19, Zadirion wrote:
>> I'm new to functional programming, just barely getting the hang of it,
>> I hope.
>> So from what I understand, side-effects are not desirable when writing
>> functional code. This means setq and variables in general should be
>> avoided.
>>
>> But suppose I have a list and I need its length not once, but twice or
>> more inside my function. If i'm not supposed to use a variable to
>> store the length of the list, how am I supposed to reuse the result of
>> that computation (the length determining)?
>>
>> (setq len (length mylist))
>> (concatenate 'list (subseq 0 (/ len 2)) (subseq (+ (/ len 2) 1) len))
>>
>> How am i supposed to achieve this without using the len variable? Or
>> am I incorrectly understanding what functional programming is all
>> about? (I come from a c++ background so imperative programming is all
>> I know for now) I could call length each time where needed, but that
>> is obviously very inefficient and unnecessary.
>
> (let* ((len (length mylist))
> (half-len (/ len 2)))
> (concatenate 'list (subseq 0 half-len) (subseq (+ half-len 1) len)))
>
> ...or something like that. Binding local variables is not considered as
> using side effects.

No but it is considered evil and taxed heavily by Mr. Graham.

But to the OP: Lisp is a multi-paradigm language designed for real work,
damn the religious wars. Functional is a great productivity thing so use
it where you can, bind variables where that is faster.

If you are in fact curious about pure functional programming go check
out one of those, and their proponents will help you with needing the
length twice. i honestly do not know what they do about that.

kt
From: Mariano Montone on
His kennyness escribi�:
> Captain Obvious wrote:
>> Z> I'm new to functional programming, just barely getting the hang of it,
>> Z> I hope.
>>
>> I'm sorry to disappoint you, but Lisp is not really a functional
>> programming language.
>> But it supports functional programming to some extent, ok...
>
> God, you are worse than I thought. Could you just go away or shut up for
> ten years and learn something before speaking here again?
>
> In Lisp, every form returns a value. Case closed. Thank you for playing.
>
> kt

CL-USER> (defun foo ()
(values))
FOO
CL-USER> (foo)
; No value
CL-USER>
From: Norbert_Paul on
Mariano Montone wrote:
> CL-USER> (defun foo ()
> (values))
> FOO
> CL-USER> (foo)
> ; No value
CL-USER> (let ((x (foo))) x)
NIL
CL-USER> ;)
; No value
CL-USER>
From: grucidipo on
On 28 abr, 13:19, Zadirion <zadir...(a)gmail.com> wrote:
> I'm new to functional programming, just barely getting the hang of it,
> I hope.
> So from what I understand, side-effects are not desirable when writing
> functional code. This means setq and variables in general should be
> avoided.
>
> But suppose I have a list and I need its length not once, but twice or
> more inside my function. If i'm not supposed to use a variable to
> store the length of the list, how am I supposed to reuse the result of
> that computation (the length determining)?
>
> (setq len (length mylist))
> (concatenate 'list (subseq 0 (/ len 2)) (subseq (+ (/ len 2) 1) len))
>
> How am i supposed to achieve this without using the len variable? Or
> am I incorrectly understanding what functional programming is all
> about? (I come from a c++ background so imperative programming is all
> I know for now) I could call length each time where needed, but that
> is obviously very inefficient and unnecessary.
>
> Many thanks,
> Gabriel

In functional programming, for example in Haskell, if you bind l to be
a list then l can't change, also the functions are pure, that is
to the same input correspond the same output, so when you compute the
length of l the compiler can memorize the result and optimize the
code.

So functional programming take advantage of first you can't modify the
binding of a variable and only use pure functions (non pure require
a monad, a difficul concept to grasp for a C programmer.
From: His kennyness on
Mariano Montone wrote:
> His kennyness escribi�:
>> Captain Obvious wrote:
>>> Z> I'm new to functional programming, just barely getting the hang of it,
>>> Z> I hope.
>>>
>>> I'm sorry to disappoint you, but Lisp is not really a functional
>>> programming language.
>>> But it supports functional programming to some extent, ok...
>> God, you are worse than I thought. Could you just go away or shut up for
>> ten years and learn something before speaking here again?
>>
>> In Lisp, every form returns a value. Case closed. Thank you for playing.
>>
>> kt
>
> CL-USER> (defun foo ()
> (values))
> FOO
> CL-USER> (foo)
> ; No value
> CL-USER>


I can always count on some newsgroup genius to point out the one
non-exception exception. It is a non-exception because there /is/ a
value if you are looking for one, namely nil. "; No value" is just a
choice of the author of your repl. My repl's author simply shows a new
prompt, which gets the edge for elegance in my book.

kt