guile-user
[Top][All Lists]

## Re: List functions

 From: Hans Aberg Subject: Re: List functions Date: Fri, 3 Dec 2010 16:06:03 +0100

```On 1 Dec 2010, at 22:34, Keith Wright wrote:

```
```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)
```
```
```
If I extend your function to one of pairs and non-pairs as well, then () becomes naturally a constant:
```(define (fmap fs x)
(if (null? fs)
'()
(if (not (pair? fs))
(apply fs (list x))
(cons (fmap (car fs) x)
(fmap (cdr fs) x)))))

Then
(fmap sqrt 2)
--> 1.4142135623731
(fmap (list sin cos) 2)
--> (0.909297426825682 -0.416146836547142)
(fmap (cons sin cos) 2)
--> (0.909297426825682 . -0.416146836547142)

But also
(fmap (list) 2)
--> ()

```
So this acts essentially as an extension of the evaluator, if all expressions (f x1 ...) are replaced by (fmap f x1 ...).
```

```