guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: List functions


From: Keith Wright
Subject: Re: List functions
Date: Wed, 1 Dec 2010 16:34:00 -0500

> Cc: address@hidden, address@hidden
> From: Hans Aberg <address@hidden>
> 
> I was trying variations like
>        (let ()
>          (define (g x)(lambda (f)(f x)))
>          (map (g 2) '(sin cos)))
> Which gives an error:
>    In expression (f x):
>    Wrong type to apply: sin

Somebody should patch that so that it mentions the
wrong type.  it means

     first argument of apply should be a function
     but it is: sin, which is a symbol

> I'm not sure when to use quote or list, here.
> Quote seems to work when the list is data,
> not a list of functions.

It's the other way around; |quote| is a keyword
that means that the following is data.
If you want to make a list of anything other
than literal data, use |list|.

So '(sin cos) is data: a list of two symbols,
the same as (list 'sin 'cos).

On the other hand |list| is an ordinary variable
so, (list sin cos) is evaluated by evaluating
the three subexpressions, giving a function
that makes lists and two numeric functions of
a numeric vaiable, and then applying the first
function, i.e. list, to the two arguments, ie.
sin and cos, giving a list of functions.

> This normality only has to do with parser grammar
> implementation. If in the evaluation syntax f ...,
> and the binding syntax corresponding in Haskell to
> \... -> f, the two "..." use the same syntax, I can
> eliminate the "\". Then the evaluation syntax (f_1,
> ..., f_k) x becomes available. I could eliminate it
> semnatically or set it to what is common in math, if
> not too complicated.
> 
> >>>> and () x into (() x), but I'm not sure if the
> >>>> lists (f g) and () can be made acting as functions
> >>>> this way.
> >
> > I have not only never seen the "normal syntax" in
> > use here, but I have no guess what it is supposed
> > to mean.  In Scheme "(() x)" means nothing at all.
> > In fact it is so far from meaningful that I can
> > not guess how to fix it.
> 
> One can set the constants to functions that evaluate
> to themselves.  One use would be expressions like
> (1 + f)(x). The () just shows up in the context above.

I didn't really follow that, but in seems that
you want to be able to apply a list of functions
to a single argument, and get a list of the
results of applying each function separately
to the same argument.

guile> 
(define (fmap fs x)
  (if (null? fs)
      '()
      (cons (apply (car fs) (list x))
            (fmap (cdr fs) x) )))

guile> (fmap (list sin cos) 2)
(0.909297426825682 -0.416146836547142)

The only tricky part is that apply takes
a function and a list of arguments,
and so (list x) is a list of one argument.

   -- Keith



reply via email to

[Prev in Thread] Current Thread [Next in Thread]