unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* help : call function with args
@ 2018-04-03 20:43 amnesia
  2018-04-03 22:40 ` Mark H Weaver
  2018-04-04  7:27 ` tomas
  0 siblings, 2 replies; 9+ messages in thread
From: amnesia @ 2018-04-03 20:43 UTC (permalink / raw)
  To: guile-user

To view the message, please use an HTML compatible email viewer.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: help : call function with args
  2018-04-03 20:43 help : call function with args amnesia
@ 2018-04-03 22:40 ` Mark H Weaver
  2018-04-04  7:27 ` tomas
  1 sibling, 0 replies; 9+ messages in thread
From: Mark H Weaver @ 2018-04-03 22:40 UTC (permalink / raw)
  To: guile-user

amnesia <user@nil.lst> writes:
> To view the message, please use an HTML compatible email viewer.

This single line was the only data in your email.  There was no HTML
MIME part, although even if there was, I would not attempt to read it.
I will only read plain text emails on this list.

     Thanks,
       Mark



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: help : call function with args
  2018-04-03 20:43 help : call function with args amnesia
  2018-04-03 22:40 ` Mark H Weaver
@ 2018-04-04  7:27 ` tomas
  1 sibling, 0 replies; 9+ messages in thread
From: tomas @ 2018-04-04  7:27 UTC (permalink / raw)
  To: amnesia; +Cc: guile-user

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tue, Apr 03, 2018 at 08:43:09PM +0000, amnesia wrote:
> To view the message, please use an HTML compatible email viewer.

Please teach your mailer to send plain-text mails (at least
as an alternative). Or change your mail provider altogether.

An anonymous mail service is swell and all, but they should
at least stick to mail conventions. Otherwise they are just
poisoning the well.

The above line is the only thing we in the mailing list can
see from your mail (the HTML part has obviously been scrubbed).

This can't work :-)

Cheers
- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlrEfkYACgkQBcgs9XrR2kbjXgCfRF5jfxw8/HY0Lycyz80Fs7ar
9h4AnAnhulJb3Evrs7O3lNNaZE+upflx
=KDsd
-----END PGP SIGNATURE-----



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: help : call function with args
@ 2018-04-04  7:43 calcium
  2018-04-04 14:39 ` tomas
  0 siblings, 1 reply; 9+ messages in thread
From: calcium @ 2018-04-04  7:43 UTC (permalink / raw)
  To: guile-user

Sorry, I tried to use a disposable email web service, but it didn’t
work, so I just created a email account.


The question was how to do what I intended with this code (sorry I don’t
know how to express this with words) :


(define (f a b c d)

(+ a b c d))


(f 1 (values 2 3) 4)


;;so that I can return multiples args inside a cond or an if statement
calling a function like:


(f 1

(if #t (values 2 3)

(values 4 5))

6)


;;and if this is not possible, which way would be the best to write this
sort of things ?, would it be using internal define so that I only have
to write the different values of the arguments inside the new shortened
function?.



And while I am asking, is there a way to use the default value of an
optional key, but using the key :


(define* (rice #:key (color 'white) (weight 10))

(list color weight))

(define* (lst-of-rice #:key (color 'white) (weight 10))

(list (rice #:color color #:weight weight)

(rice #:color color #:weight weight)))


;;sot that instead of writing in lst-of-rice (color ‘white), write
(color UseSameAsKeyofRice), or something that use the default value of
rice, if no value is given in lst-of-rice, and use the value of
lst-of-rice when we use is key.


Thanks, and sorry for the first email.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: help : call function with args
       [not found] <1f5de71a-c5aa-b850-023e-96fdd471dab5@disroot.org>
@ 2018-04-04  8:01 ` calcium
  0 siblings, 0 replies; 9+ messages in thread
From: calcium @ 2018-04-04  8:01 UTC (permalink / raw)
  To: guile-user

i am very sorry, because i did twice the mistake (and i don't know if
saying sorry in a mail list is another mistake), i had forgotten that
thunderbird use html formatting by default, again, i am sorry, and if it
is possible, just delete them.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: help : call function with args
  2018-04-04  7:43 calcium
@ 2018-04-04 14:39 ` tomas
  0 siblings, 0 replies; 9+ messages in thread
From: tomas @ 2018-04-04 14:39 UTC (permalink / raw)
  To: guile-user

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wed, Apr 04, 2018 at 09:43:18AM +0200, calcium wrote:
> Sorry, I tried to use a disposable email web service, but it didn’t
> work, so I just created a email account.

No problem. And despite your second mail, this one does arrive
as plain text: all seems well :-)

> The question was how to do what I intended with this code (sorry I don’t
> know how to express this with words) :
> 
> 
> (define (f a b c d)
> 
> (+ a b c d))
> 
> 
> (f 1 (values 2 3) 4)

Hm. I don't think "values" does what you expect it to do.
(Please, more seasoned Schemers help out if I'm talking
nonsense)

Thing is that the "slot" (which I'll represent as "#") in

  (f 1 # 4)

just expects *one* value. And multiple value returns (as
returned by "values" are built to only yield one value when
expected to do so (the first one), so your code will "see"

  (f 1 2 4)

and complain that you're "holding" f "wrong". If you want
to build a function taking multiple values you'll have to
use "call-with-values".

I think you'll find it easier to reason first about an f
which takes as many args as your conditionals produce, so
you won't be struggling to splice your "bunch of values"
into a bigger argument list.

For example (using list here, but could use multiple values
too):

  (define weather 'fine)

  (define (mood)
    (if (eq weather 'fine)
        (list "very" "good")
      (list "pretty" "bad")))

  (apply string-append (mood))

  => "verygood"

  (set! weather 'so-so)

  (apply string-append (mood))

  => "prettybad"

Is that the direction you are trying to take?

Cheers
- -- t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlrE47wACgkQBcgs9XrR2kYPBwCfTxPUCELmJz67+luFK4e0s2il
ExsAnjdUCxW1atS5ojsv+Rqm9XkBO+aI
=sWuA
-----END PGP SIGNATURE-----



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: help : call function with args
@ 2018-04-05  8:16 calcium
  2018-04-05  8:54 ` Thomas Morley
  0 siblings, 1 reply; 9+ messages in thread
From: calcium @ 2018-04-05  8:16 UTC (permalink / raw)
  To: guile-user

> I think you'll find it easier to reason first about an f
> which takes as many args as your conditionals produce, so
> you won't be struggling to splice your "bunch of values"
> into a bigger argument list.

yes, thank you tomas,
i stopped trying to return multiples values, and instead call the
functions with the right numbers of args, and using the cond statement
to change the values of args.

(define (f a b c d)
  (+ a b c d))

(let ((a 1)
      (b #f)
      (c #f)
      (d 10))
  (cond (#t (set! b 2)
            (set! c 3))
        (else
         (set! b 6)
         (set! c 10)))
  (f a b c d))



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: help : call function with args
  2018-04-05  8:16 calcium
@ 2018-04-05  8:54 ` Thomas Morley
  2018-04-05  9:42   ` calcium
  0 siblings, 1 reply; 9+ messages in thread
From: Thomas Morley @ 2018-04-05  8:54 UTC (permalink / raw)
  To: calcium; +Cc: guile-user

2018-04-05 10:16 GMT+02:00 calcium <calcium@disroot.org>:
>> I think you'll find it easier to reason first about an f
>> which takes as many args as your conditionals produce, so
>> you won't be struggling to splice your "bunch of values"
>> into a bigger argument list.
>
> yes, thank you tomas,
> i stopped trying to return multiples values, and instead call the
> functions with the right numbers of args, and using the cond statement
> to change the values of args.
>
> (define (f a b c d)
>   (+ a b c d))
>
> (let ((a 1)
>       (b #f)
>       (c #f)
>       (d 10))
>   (cond (#t (set! b 2)
>             (set! c 3))
>         (else
>          (set! b 6)
>          (set! c 10)))
>   (f a b c d))
>

Not sure what you aim at.
Though, above seems to be easier with:

(define (f . rest) (apply + rest))

(f)
-> 0
(f 1 2 3)
-> 6
(f 1 2 3 4 5 6 7 8 9 10)
-> 55

Cheers,
  Harm



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: help : call function with args
  2018-04-05  8:54 ` Thomas Morley
@ 2018-04-05  9:42   ` calcium
  0 siblings, 0 replies; 9+ messages in thread
From: calcium @ 2018-04-05  9:42 UTC (permalink / raw)
  To: Thomas Morley; +Cc: guile-user


> Not sure what you aim at.
> Though, above seems to be easier with:
> 
> (define (f . rest) (apply + rest))
> 
> (f)
> -> 0
> (f 1 2 3)
> -> 6
> (f 1 2 3 4 5 6 7 8 9 10)
> -> 55
> 
> Cheers,
>   Harm
> 

yes, it easier for this function, but when we have functions with
different "types" like the one bellow , i find it nice to only call once
the function, instead of copy-past the common args and then giving the
different args for each cond branch.

Although, the example that i give, it way more elegant and readable the
second than the first, but i believes that sometimes the first can be
more compact an elegant than the second one.

(define lst
  (list 1 3 5 9 10 2 1 2 20 4 9 15))


;;sort the list so that the biggest element is the first element
;;and the rest without a particular order.

;;write once the function

(let sort ((preLst (cdr lst))
           (postLst '())
           (biggest (car lst)))
  (cond ((nil? preLst)
         (cons biggest postLst))
        (else (let ((post (cons (car preLst) postLst))
                    (big biggest))
                (cond ((> (car preLst) biggest)
                       (set! big (car preLst))
                       (set! post (cons biggest postLst))))
                (sort (cdr preLst) post big)))))

;; instead of writing

(let sort ((preLst (cdr lst))
           (postLst '())
           (biggest (car lst)))
  (cond ((nil? preLst)
         (cons biggest postLst))
        (else (cond ((> (car preLst) biggest)
                     (sort (cdr preLst) ;; the common arg
                           (cons biggest postLst)
                           (car preLst)))
                    (else
                     (sort (cdr preLst) ;;the common arg
                           (cons (car preLst) postLst)
                           biggest))))))
Cheers,
 Calcium



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2018-04-05  9:42 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-04-03 20:43 help : call function with args amnesia
2018-04-03 22:40 ` Mark H Weaver
2018-04-04  7:27 ` tomas
  -- strict thread matches above, loose matches on Subject: below --
2018-04-04  7:43 calcium
2018-04-04 14:39 ` tomas
     [not found] <1f5de71a-c5aa-b850-023e-96fdd471dab5@disroot.org>
2018-04-04  8:01 ` calcium
2018-04-05  8:16 calcium
2018-04-05  8:54 ` Thomas Morley
2018-04-05  9:42   ` calcium

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).