[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: List functions
From: |
Hans Aberg |
Subject: |
Re: List functions |
Date: |
Wed, 1 Dec 2010 23:43:45 +0100 |
On 1 Dec 2010, at 22:34, Keith Wright wrote:
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
Yes, I realized this.
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).
Strictly speaking, they are, as you noted, symbols. Quote turns off
further evaluation. If one should have a more normal evaluation model
of tuples, perhaps one should use back-quote and commas (see below).
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.
I build lists directly using the Guile C functions. However, I
encountered an ambiguity in my implementation which has to do with the
(x) = x property of tuples: both x y and (x, y) parsed as Guile (x y)
= (list x y).
If one writes just (x_1, ..., x_k), what Guile object should one get?
(x_1 ... x_k) may cause later evaluation, '(x_1 ... x_k) turns off
evaluation of the arguments. So perhaps one should construct
`(,x_1 ... ,x_k).
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.
Yes, and the first item of the list should not treat the following
items as its arguments for an evaluation. It is a different data
object than the Guile evaluation list.
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.
Thank you. I will try some different possibilities.
- List functions, Hans Aberg, 2010/12/01
- Re: List functions, Joel James Adamson, 2010/12/01
- Re: List functions, Hans Aberg, 2010/12/01
- Re: List functions, Keith Wright, 2010/12/01
- Re: List functions, Hans Aberg, 2010/12/01
- Re: List functions, Hans Aberg, 2010/12/01
- Re: List functions, Keith Wright, 2010/12/01
- Re: List functions, Hans Aberg, 2010/12/01
- Re: List functions,
Hans Aberg <=
- Re: List functions, Hans Aberg, 2010/12/03
- Re: List functions, Andy Wingo, 2010/12/01
- Re: List functions, Marco Maggi, 2010/12/02