Discussion:
help : call function with args
amnesia
2018-04-03 20:43:09 UTC
Permalink
To view the message, please use an HTML compatible email viewer.
Mark H Weaver
2018-04-03 22:40:12 UTC
Permalink
Post by amnesia
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
t***@tuxteam.de
2018-04-04 07:27:02 UTC
Permalink
Post by amnesia
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
calcium
2018-04-04 08:01:24 UTC
Permalink
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.
calcium
2018-04-04 07:43:18 UTC
Permalink
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.
t***@tuxteam.de
2018-04-04 14:39:56 UTC
Permalink
Post by calcium
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 :-)
Post by calcium
The question was how to do what I intended with this code (sorry I don’t
(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
calcium
2018-04-05 08:16:59 UTC
Permalink
Post by t***@tuxteam.de
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))
Thomas Morley
2018-04-05 08:54:16 UTC
Permalink
Post by calcium
Post by t***@tuxteam.de
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
calcium
2018-04-05 09:42:08 UTC
Permalink
Post by Thomas Morley
Not sure what you aim at.
(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

Loading...