Re: Guile 2 birthday potluck

2013-01-11 Thread Ludovic Courtès
Hi!

Limbo Peng  skribis:

> BTW: did you guys have a hack-potluck last year?

Yes, just not under this name.  ;-)  Check
http://www.gnu.org/software/guile/news.html under 2012-02-16.

Happy hacking!

Ludo’.



call-with-values and primitives

2013-01-11 Thread bromley
Hello.

Why does it return -1? Could anyone explain?
(Comments are mine.)

(call-with-values (lambda () (values 4 5))
  (lambda (a b) b)) ; a is 4 and b is 5; return 5
⇒ 5

(call-with-values * -)
⇒ -1

More:

(call-with-values + +)
0

(call-with-values + -)
0

(call-with-values - -)
ERROR: Wrong number of arguments to -
ABORT: (wrong-number-of-args)

https://gnu.org/software/guile/manual/guile.html#Multiple-Values






Re: http-post

2013-01-11 Thread Andy Wingo
On Tue 17 Apr 2012 05:34, gregory benison  writes:

> "More helper procedures for the other common HTTP verbs would be a
> good addition to this module.  Send your code to ."
>
> So, I say to guile-user, "here is my code".

Thanks!  I used it as a base for a patch I just pushed.  Guile now has
client wrappers for all HTTP verbs except CONNECT.  Appending the
documentation.  A websocket implementation would be nice.

> I think the next steps after this patch are to:
> - be able to work with a greater variety of encodings;
> - since form data of the type "key1=value1&key2=value2..." is so
> common in POST bodies, accept post data as key-value pairs represented
> by an alist (which would be coerced into a bytevector automatically).

Yes this would be good.  Want to make a patch?

Regards,

Andy

File: guile.info,  Node: Web Client,  Next: Web Server,  Prev: Responses,  Up: 
Web

7.3.8 Web Client


`(web client)' provides a simple, synchronous HTTP client, built on the
lower-level HTTP, request, and response modules.

 -- Scheme Procedure: open-socket-for-uri uri
 Return an open input/output port for a connection to URI.

 -- Scheme Procedure: http-get uri keyword-arg...
 -- Scheme Procedure: http-head uri keyword-arg...
 -- Scheme Procedure: http-post uri keyword-arg...
 -- Scheme Procedure: http-put uri keyword-arg...
 -- Scheme Procedure: http-delete uri keyword-arg...
 -- Scheme Procedure: http-trace uri keyword-arg...
 -- Scheme Procedure: http-options uri keyword-arg...
 Connect to the server corresponding to URI and make a request over
 HTTP, using the appropriate method (`GET', `HEAD', etc.).

 All of these procedures have the same prototype: a URI followed by
 an optional sequence of keyword arguments.  These keyword
 arguments allow you to modify the requests in various ways, for
 example attaching a body to the request, or setting specific
 headers.  The following table lists the keyword arguments and
 their default values.

`#:body #f'

`#:port (open-socket-for-uri URI)]'

`#:version '(1 . 1)'

`#:keep-alive? #f'

`#:headers '()'

`#:decode-body? #t'

`#:streaming? #f'

 If you already have a port open, pass it as PORT.  Otherwise, a
 connection will be opened to the server corresponding to URI.  Any
 extra headers in the alist HEADERS will be added to the request.

 If BODY is not #f, a message body will also be sent with the HTTP
 request.  If BODY is a string, it is encoded according to the
 content-type in HEADERS, defaulting to UTF-8.  Otherwise BODY
 should be a bytevector, or `#f' for no body.  Although a message
 body may be sent with any request, usually only `POST' and `PUT'
 requests have bodies.

 If DECODE-BODY? is true, as is the default, the body of the
 response will be decoded to string, if it is a textual
 content-type.  Otherwise it will be returned as a bytevector.

 However, if STREAMING? is true, instead of eagerly reading the
 response body from the server, this function only reads off the
 headers.  The response body will be returned as a port on which
 the data may be read.

 Unless KEEP-ALIVE? is true, the port will be closed after the full
 response body has been read.

 Returns two values: the response read from the server, and the
 response body as a string, bytevector, #f value, or as a port (if
 STREAMING? is true).

   `http-get' is useful for making one-off requests to web sites.  If
you are writing a web spider or some other client that needs to handle a
number of requests in parallel, it's better to build an event-driven URL
fetcher, similar in structure to the web server (*note Web Server::).

   Another option, good but not as performant, would be to use threads,
possibly via par-map or futures.


-- 
http://wingolog.org/



Re: call-with-values and primitives

2013-01-11 Thread Ian Price
brom...@lavabit.com writes:

> Hello.
>
> Why does it return -1? Could anyone explain?
> (Comments are mine.)
The general principle is to try and give sensible results when +,*,etc
are called with a number of arguments other than 2. By thinking them
through at this level you can, in theory, write more general code and
have it "just work". Not only does it apply to various functions, but it
is generally considered a sensible macro design principle too, as in the
case of, among others, 'and' and 'or'. 

>
> (call-with-values (lambda () (values 4 5))
>   (lambda (a b) b)) ; a is 4 and b is 5; return 5
> ⇒ 5
>
> (call-with-values * -)
> ⇒ -1
If you call * with 0 arguments, it returns one value, the number 1. This
makes some sense since 1 is the identity for multiplication.
i.e. (* 1 x) = x = (* x 1) for all x

If you call - with one argument, it negates that argument, as if you had
done (- 0 x).

Putting both of these together, (call-with-values * -) is equivalent to (- (*))

scheme@(guile−user)> (*)
$2 = 1
scheme@(guile−user)> (- 1)
$3 = −1
scheme@(guile−user)> (- (*))
$4 = −1

> (call-with-values + +)
> 0

Similarly to multiplication, if you call (+) with one argument, you get
the identity for +, which is 0. This expression then, is equivalent to
(+ (+)) = (+ 0) = 0

> (call-with-values + -)
> 0
Similar to the above situation, you can think of this as being
equivalent to (- (+)). Negating 0, naturally enough, gives you 0.

scheme@(guile−user)> (+)
$5 = 0
scheme@(guile−user)> (- (+))
$6 = 0

> (call-with-values - -)
> ERROR: Wrong number of arguments to -
While we extend / and - to have sensible results for the one argument
case (reciprocal and negation), and we extend it for more than two
arguments, there is not an identity for / or - [0], so we mark the zero
argument case as errors for both.

scheme@(guile−user)> (/)
ERROR: In procedure /:
ERROR: Wrong number of arguments to /
scheme@(guile−user)> (-)
ERROR: In procedure −:
ERROR: Wrong number of arguments to −

[0] Well, more correctly, they have right identities, of 1 and 0
respectively, but no left ones
-- 
Ian Price -- shift-reset.com

"Programming is like pinball. The reward for doing it well is
the opportunity to do it again" - from "The Wizardy Compiled"



Re: call-with-values and primitives

2013-01-11 Thread Andy Wingo
On Fri 11 Jan 2013 15:23, brom...@lavabit.com writes:

> (call-with-values * -)
> ⇒ -1

So it's like:

  (call-with-values producer consumer)

The producer is called with no arguments, then the results of that call
are passed to the consumer.

So first `*' is called with no arguments, like this:

  (*)

Calling `*' always returns just one value, so in this case it's
equivalent to binding a single value, so the original expression is
completely equivalent to:

  (let ((tmp (*)))
(- tmp))

Reducing this further:

  => (let ((tmp 1))
   (- tmp))

  => (- 1)

  => -1

See the R5RS for more, including the definition of (*) and (+).

> (call-with-values + +)
> 0

  (let ((tmp (+)))
(+ tmp))

> (call-with-values + -)
> 0

  (let ((tmp (+)))
(- tmp))

> (call-with-values - -)

  (let ((tmp (-)))
(- tmp))

> ERROR: Wrong number of arguments to -
> ABORT: (wrong-number-of-args)

(-) has no sensible answer.

Happy hacking,

Andy
-- 
http://wingolog.org/



Re: Readline support in Cygwin

2013-01-11 Thread Akop Pogosian
On Wed, Jan 9, 2013 at 3:57 PM, Ludovic Courtès  wrote:
> Hi!
>
> Akop Pogosian  skribis:
>
>> # ERROR: In procedure load-extension:
>> # ERROR: In procedure dynamic-link: file: "libguilereadline-v-18",
>> message: "The specified module could not be found."
>
> [...]
>
>> # $ ls software/packages/guile-2.0.7/lib/
>> # guile   libguile-2.0.la  libguilereadline-v-18.la
>> # libguile-2.0.a  libguilereadline-v-18.a  pkgconfig
>
> As you can see, you only have a static version of libguilereadline,
> which is why ‘dynamic-link’ fails.

Indeed. I am really confused about why there are no shared libraries
in /usr/lib under cygwin. However, they are installed in /usr/bin
under strange names such as /usr/bin/cygreadline6.dll

Is guile build system supposed to pick up shared libraries in those
locations or do I need to do something special?