* map, for-each
@ 2010-08-26 11:17 Eric J. Van der Velden
2010-08-26 12:16 ` Paul Emsley
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Eric J. Van der Velden @ 2010-08-26 11:17 UTC (permalink / raw)
To: guile-user
[-- Attachment #1: Type: text/plain, Size: 303 bytes --]
Hello,
I don't understand what the manual says about when there are more then two
arguments to map or for-each.
With two arguments, the last one must be a list, so OK is
(for-each display '(1 3))
But the following are ERR,
(for-each display 1 3)
(for-each display '(1 3) '(1 3))
Thanks,
Eric J.
[-- Attachment #2: Type: text/html, Size: 385 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: map, for-each
2010-08-26 11:17 map, for-each Eric J. Van der Velden
@ 2010-08-26 12:16 ` Paul Emsley
2010-08-26 12:19 ` Thien-Thi Nguyen
2010-08-28 19:35 ` Andy Wingo
2 siblings, 0 replies; 4+ messages in thread
From: Paul Emsley @ 2010-08-26 12:16 UTC (permalink / raw)
To: guile-user
On 26/08/10 12:17, Eric J. Van der Velden wrote:
> Hello,
>
> I don't understand what the manual says about when there are more then
> two arguments to map or for-each.
>
> With two arguments, the last one must be a list, so OK is
>
> (for-each display '(1 3))
>
> But the following are ERR,
>
> (for-each display 1 3)
>
> (for-each display '(1 3) '(1 3))
The function, in this case `display', needs to be able to take 2 args,
for display it's the variable and the port:
(for-each display '(1 3) (list (current-output-port) (current-output-port)))
also consider
(map + '(1 3) '(1 3))
Paul.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: map, for-each
2010-08-26 11:17 map, for-each Eric J. Van der Velden
2010-08-26 12:16 ` Paul Emsley
@ 2010-08-26 12:19 ` Thien-Thi Nguyen
2010-08-28 19:35 ` Andy Wingo
2 siblings, 0 replies; 4+ messages in thread
From: Thien-Thi Nguyen @ 2010-08-26 12:19 UTC (permalink / raw)
To: Eric J. Van der Velden; +Cc: guile-user
() "Eric J. Van der Velden" <ericjvandervelden@gmail.com>
() Thu, 26 Aug 2010 13:17:38 +0200
But the following are ERR,
(for-each display 1 3) ; A
(for-each display '(1 3) '(1 3)) ; B
The arity of the proc (specifically, the "required" portion)
must concord with the number of lists passed as arg2... to
‘for-each’. In this case, the arity of ‘display’ is 1.
In A, there are two errors:
- arg2... are not lists
- (count arg2...) => 2, which is not 1
In B, there is only one error:
- (count arg2...) => 2, which is not 1
If you:
(define (display2 a b)
(display a) (display #\space) (display b) (newline))
then substituting it for ‘display’ in A produces only
one error, and in B none.
Actually, the above is a simplification: ‘display’ accepts
an optional second arg, a port to send its output to, so one
could say that B has an additional error: arg3 is not a
list of output ports (this is a type error in some schools).
For example, this does not produce an error:
(define CO (current-output-port))
(for-each display '(1 3) (list CO CO)) ; C
For C, (count arg2...) => 2, which is not 1, but that's ok.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: map, for-each
2010-08-26 11:17 map, for-each Eric J. Van der Velden
2010-08-26 12:16 ` Paul Emsley
2010-08-26 12:19 ` Thien-Thi Nguyen
@ 2010-08-28 19:35 ` Andy Wingo
2 siblings, 0 replies; 4+ messages in thread
From: Andy Wingo @ 2010-08-28 19:35 UTC (permalink / raw)
To: Eric J. Van der Velden; +Cc: guile-user
On Thu 26 Aug 2010 04:17, "Eric J. Van der Velden" <ericjvandervelden@gmail.com> writes:
> I don't understand what the manual says about when there are more then two arguments to map or for-each.
>
> With two arguments, the last one must be a list, so OK is
>
> (for-each display '(1 3))
Consider:
(map + '(1 3))
= (list (+ 1) (+ 3))
= (list 1 3)
=> (1 3)
(map + '(1 3) '(5 7))
= (list (+ 1 5) (+ 3 7))
= (list 6 10)
=> (6 10)
After the function, all args need to be lists. The `display' example you
gave, even if you had given for-each lists, still doesn't make sense:
(for-each display '(1 3) '(5 7))
= (begin (display 1 5) (display 3 7))
=> ERROR: in (display 1 5): 5 is not a port
Hope that helps,
Andy
--
http://wingolog.org/
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2010-08-28 19:35 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-08-26 11:17 map, for-each Eric J. Van der Velden
2010-08-26 12:16 ` Paul Emsley
2010-08-26 12:19 ` Thien-Thi Nguyen
2010-08-28 19:35 ` Andy Wingo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).