guile-user
[Top][All Lists]
Advanced

[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 21:26:50 +0100

On 1 Dec 2010, at 20:50, Hans Aberg wrote:

... in standard syntax would be natural to let (f, g)(x) evaluate
to (f(x), g(x)), producing a list of two elements. In Guile, that
would be something involving "map". If I try in Haskell, I can let
(sin, cos)(2) be the same as
 map (g 2) [sin, cos] where g x = \f -> f x
  -> [0.909297426825682,-0.416146836547142]
But when I try that similar constructs in Guile, I get problems with
evaluation.

Works for me

guile> (let ()
       (define (g x)(lambda (f)(f x)))
       (map (g 2) (list sin cos)))

(0.909297426825682 -0.416146836547142)

There are other ways to write it, but that
is the most direct translation of your Haskell
into Scheme.

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

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.

I realized the problem here: quote prevents evaluation in the list, so one gets symbols, not procedures. Using back-quote and comma works:
(let ()
        (define (g x)(lambda (f)(f x)))
        (map (g 2) `(,sin ,cos)))

Anyway, thanks, I now know what to fiddle around with.




reply via email to

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