all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* lambda inside a let or letrec
@ 2010-06-10  5:34 bolega
  2010-06-10  5:37 ` bolega
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: bolega @ 2010-06-10  5:34 UTC (permalink / raw
  To: help-gnu-emacs

My apologies in advance to comp.lang.scheme and comp.lang.lisp.

I am trying to run a certain syntax inside emacs lisp.

I know basically how let works

(let (list of pairs of var value) (function))

This is like a lambda function call , only the order is different.

But the novelty i saw reading a book on common lisp or scheme is this
and i failed to run in emacs. plz tell what modifications are needed
and i know they are different.

(   (lambda (n) (+ 1 n)) 3)  ;;; works in emacs

(let
((a (lambda (n) (+ 1 n))) (b 3)) (a b))   ;;; does NOT work in emacs

basically we are trying to use / abuse the let in that in the pair we
define a equal to a lambda. Then another pair where a value of b is
defined.

next, we want a to operate on b.

Why does it fail ?

The scheme/lisp book/paper where it was seen (forgot) used letrec.

Can someone enlighten me how set! and let can be used to formulate
recursion when the let has no recursion built in it ?

thanks a lot.
cheers






^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: lambda inside a let or letrec
  2010-06-10  5:34 lambda inside a let or letrec bolega
@ 2010-06-10  5:37 ` bolega
  2010-06-10  6:18   ` bolega
  2010-06-10  5:37 ` bolega
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 7+ messages in thread
From: bolega @ 2010-06-10  5:37 UTC (permalink / raw
  To: help-gnu-emacs

let me write it more clearly with indents as follows :

(let
((a (lambda (n) (+ 1 n)))
 (b 3))
(a b))

On Jun 9, 10:34 pm, bolega <gnuist...@gmail.com> wrote:
> My apologies in advance to comp.lang.scheme and comp.lang.lisp.
>
> I am trying to run a certain syntax inside emacs lisp.
>
> I know basically how let works
>
> (let (list of pairs of var value) (function))
>
> This is like a lambda function call , only the order is different.
>
> But the novelty i saw reading a book on common lisp or scheme is this
> and i failed to run in emacs. plz tell what modifications are needed
> and i know they are different.
>
> (   (lambda (n) (+ 1 n)) 3)  ;;; works in emacs
>
> (let
> ((a (lambda (n) (+ 1 n))) (b 3)) (a b))   ;;; does NOT work in emacs
>
> basically we are trying to use / abuse the let in that in the pair we
> define a equal to a lambda. Then another pair where a value of b is
> defined.
>
> next, we want a to operate on b.
>
> Why does it fail ?
>
> The scheme/lisp book/paper where it was seen (forgot) used letrec.
>
> Can someone enlighten me how set! and let can be used to formulate
> recursion when the let has no recursion built in it ?
>
> thanks a lot.
> cheers



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: lambda inside a let or letrec
  2010-06-10  5:34 lambda inside a let or letrec bolega
  2010-06-10  5:37 ` bolega
@ 2010-06-10  5:37 ` bolega
  2010-06-10  7:22 ` Helmut Eller
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: bolega @ 2010-06-10  5:37 UTC (permalink / raw
  To: help-gnu-emacs

let me write it more clearly with indents as follows :

(let
((a (lambda (n) (+ 1 n)))
 (b 3))
(a b))

On Jun 9, 10:34 pm, bolega <gnuist...@gmail.com> wrote:
> My apologies in advance to comp.lang.scheme and comp.lang.lisp.
>
> I am trying to run a certain syntax inside emacs lisp.
>
> I know basically how let works
>
> (let (list of pairs of var value) (function))
>
> This is like a lambda function call , only the order is different.
>
> But the novelty i saw reading a book on common lisp or scheme is this
> and i failed to run in emacs. plz tell what modifications are needed
> and i know they are different.
>
> (   (lambda (n) (+ 1 n)) 3)  ;;; works in emacs
>
> (let
> ((a (lambda (n) (+ 1 n))) (b 3)) (a b))   ;;; does NOT work in emacs
>
> basically we are trying to use / abuse the let in that in the pair we
> define a equal to a lambda. Then another pair where a value of b is
> defined.
>
> next, we want a to operate on b.
>
> Why does it fail ?
>
> The scheme/lisp book/paper where it was seen (forgot) used letrec.
>
> Can someone enlighten me how set! and let can be used to formulate
> recursion when the let has no recursion built in it ?
>
> thanks a lot.
> cheers



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: lambda inside a let or letrec
  2010-06-10  5:37 ` bolega
@ 2010-06-10  6:18   ` bolega
  0 siblings, 0 replies; 7+ messages in thread
From: bolega @ 2010-06-10  6:18 UTC (permalink / raw
  To: help-gnu-emacs

On Jun 9, 10:37 pm, bolega <gnuist...@gmail.com> wrote:
> let me write it more clearly with indents as follows :
>
> (let
> ((a (lambda (n) (+ 1 n)))
>  (b 3))
> (a b))
>
> On Jun 9, 10:34 pm, bolega <gnuist...@gmail.com> wrote:
>
> > My apologies in advance to comp.lang.scheme and comp.lang.lisp.
>
> > I am trying to run a certain syntax inside emacs lisp.
>
> > I know basically how let works
>
> > (let (list of pairs of var value) (function))
>
> > This is like a lambda function call , only the order is different.
>
> > But the novelty i saw reading a book on common lisp or scheme is this
> > and i failed to run in emacs. plz tell what modifications are needed
> > and i know they are different.
>
> > (   (lambda (n) (+ 1 n)) 3)  ;;; works in emacs
>
> > (let
> > ((a (lambda (n) (+ 1 n))) (b 3)) (a b))   ;;; does NOT work in emacs
>
> > basically we are trying to use / abuse the let in that in the pair we
> > define a equal to a lambda. Then another pair where a value of b is
> > defined.
>
> > next, we want a to operate on b.
>
> > Why does it fail ?
>
> > The scheme/lisp book/paper where it was seen (forgot) used letrec.
>
> > Can someone enlighten me how set! and let can be used to formulate
> > recursion when the let has no recursion built in it ?
>
> > thanks a lot.
> > cheers

The following works where I have replaced the association of lambda to
a  _by_  the association of lambda function call to a, ie a numerical
value.

(let
((a ((lambda (n) (+ 1 n)) 5))
 (b 3))
(+ a b))

I am sure I am making some trivial mistake


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: lambda inside a let or letrec
  2010-06-10  5:34 lambda inside a let or letrec bolega
  2010-06-10  5:37 ` bolega
  2010-06-10  5:37 ` bolega
@ 2010-06-10  7:22 ` Helmut Eller
  2010-06-10 14:28 ` Pascal J. Bourguignon
  2010-06-11  0:42 ` bolega
  4 siblings, 0 replies; 7+ messages in thread
From: Helmut Eller @ 2010-06-10  7:22 UTC (permalink / raw
  To: help-gnu-emacs

* bolega [2010-06-10 07:34+0200] writes:

> My apologies in advance to comp.lang.scheme and comp.lang.lisp.
>
> I am trying to run a certain syntax inside emacs lisp.
>
> I know basically how let works
>
> (let (list of pairs of var value) (function))
>
> This is like a lambda function call , only the order is different.
>
> But the novelty i saw reading a book on common lisp or scheme is this
> and i failed to run in emacs. plz tell what modifications are needed
> and i know they are different.
>
> (   (lambda (n) (+ 1 n)) 3)  ;;; works in emacs
>
> (let
> ((a (lambda (n) (+ 1 n))) (b 3)) (a b))   ;;; does NOT work in emacs
>
> basically we are trying to use / abuse the let in that in the pair we
> define a equal to a lambda. Then another pair where a value of b is
> defined.
>
> next, we want a to operate on b.
>
> Why does it fail ?

The first element of a call like (FUN ARG0 ARG1 ...), that is FUN, is
special.  FUN must be one of:

1) be the name of function (like "+")
2) the name of a macro (like "save-excursion")
3) the name of a special form (like "let", or "while")
4) a lambda form (e.g ((lambda (x) (+ x x) 1)))

So if you write the (a b) the evaluation rules for a are different as
the rules for b.  In your example "a" is a variable but not a function.
In techical terms one would say that a is bound in the variable
namespace but not in the function namespace.

The simplest solution to call a variable is: (funcall a b)

> The scheme/lisp book/paper where it was seen (forgot) used letrec.

It was probably a Scheme book, because the rules are different in this
regard in Scheme.  In Scheme a call (FUN ARG0 ...) is essentially
the same as (funcall FUN ARG0 ...) in Lisp

> Can someone enlighten me how set! and let can be used to formulate
> recursion when the let has no recursion built in it ?

Well, in Emacs Lisp it's easy:

(let ((fac (lambda (x) (if (zerop x) 1 (* x (funcall fac (1- x)))))))
  (funcall fac 10))

that works because Emacs uses dynamic scoping, so you don't need setq.

Helmut


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: lambda inside a let or letrec
  2010-06-10  5:34 lambda inside a let or letrec bolega
                   ` (2 preceding siblings ...)
  2010-06-10  7:22 ` Helmut Eller
@ 2010-06-10 14:28 ` Pascal J. Bourguignon
  2010-06-11  0:42 ` bolega
  4 siblings, 0 replies; 7+ messages in thread
From: Pascal J. Bourguignon @ 2010-06-10 14:28 UTC (permalink / raw
  To: help-gnu-emacs

bolega <gnuist006@gmail.com> writes:

> My apologies in advance to comp.lang.scheme and comp.lang.lisp.
>
> I am trying to run a certain syntax inside emacs lisp.
>
> I know basically how let works
>
> (let (list of pairs of var value) (function))
>
> This is like a lambda function call , only the order is different.
>
> But the novelty i saw reading a book on common lisp or scheme is this
> and i failed to run in emacs. plz tell what modifications are needed
> and i know they are different.
>
> (   (lambda (n) (+ 1 n)) 3)  ;;; works in emacs
>
> (let
> ((a (lambda (n) (+ 1 n))) (b 3)) (a b))   ;;; does NOT work in emacs
>
> basically we are trying to use / abuse the let in that in the pair we
> define a equal to a lambda. Then another pair where a value of b is
> defined.
>
> next, we want a to operate on b.
>
> Why does it fail ?
>
> The scheme/lisp book/paper where it was seen (forgot) used letrec.
>
> Can someone enlighten me how set! and let can be used to formulate
> recursion when the let has no recursion built in it ?

As indicated by David, it's because scheme (and eg. LeLisp) is a
Lisp-1 while emacs lisp and Common Lisp are Lisp-2.



In a Lisp-1 there's a single slot for both values and functions,
functions are just values like others, and a symbol has only one
value, which may be a function or something else.

In this case, both LET and the function application refer the same
unique value of the symbol.

This has the advantage of being simple conceptually, and to be able to
write with a simplier syntax function code of higher order.




In a Lisp-2 there are two slots, one for a value (which may be a
function too) and one for a function (which may only be unbound or a
function).

In this case, depending on the context, the value slot XOR the
function slot is used.  LET binds the value slot, the symbol by itself
evaluates to the contents of its value slots (when it's a special
variable).  On the other hand DEFUN and function application refer the
function slot.   This has the advantage of reducing a lot of collision
problems in writing unhygienic macros, and to offer a double meaning
that matches more closely natural languages.  In a Lisp-2 you can write:
 
   (let ((buffalo (get-a-bull)))
     (flet ((buffalo (what) (do-it what)))
        (buffalo buffalo)))

and have (do-it (get-a-bull)) executed.

On the other hand, if you want to use a value as a function, you have
to go thru APPLY or FUNCALL:

   (let ((fun (lambda (x) (be-happy-with x))))
     (funcall fun 'foot))


In a Lisp-1 you would have to write:

   (let ((buffalo-as-noun (get-a-bull))
         (buffalo-as-verb (lambda (what) (do-it what))))
     (buffalo-as-verb buffalo-as-noon))




Now, IIRC, Pascal Costanza has published in cll a few years ago a
macro that will allow you to write Lisp-1 function applications in a
Lisp-2, so that when you need locally to call a lot of functions
stored in value slots you may write it like in scheme.  
Try to groups.google cll for Pascal Costanza Lisp-1 macro.




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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: lambda inside a let or letrec
  2010-06-10  5:34 lambda inside a let or letrec bolega
                   ` (3 preceding siblings ...)
  2010-06-10 14:28 ` Pascal J. Bourguignon
@ 2010-06-11  0:42 ` bolega
  4 siblings, 0 replies; 7+ messages in thread
From: bolega @ 2010-06-11  0:42 UTC (permalink / raw
  To: help-gnu-emacs

thank you so much to all who replied. I gave max star rating in google
to all replies so others can find it easily


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2010-06-11  0:42 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-06-10  5:34 lambda inside a let or letrec bolega
2010-06-10  5:37 ` bolega
2010-06-10  6:18   ` bolega
2010-06-10  5:37 ` bolega
2010-06-10  7:22 ` Helmut Eller
2010-06-10 14:28 ` Pascal J. Bourguignon
2010-06-11  0:42 ` bolega

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.