unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: calcium <calcium@disroot.org>
To: Thomas Morley <thomasmorley65@gmail.com>
Cc: guile-user@gnu.org
Subject: Re: help : call function with args
Date: Thu, 5 Apr 2018 11:42:08 +0200	[thread overview]
Message-ID: <a3dfa49e-1d53-b649-eaae-1a2bda5b1025@disroot.org> (raw)
In-Reply-To: <CABsfGyVV6g1T3FvdjM8y-AfmnKkvSXO0YK0SMHeeA7VqdzD_fA@mail.gmail.com>


> 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



  reply	other threads:[~2018-04-05  9:42 UTC|newest]

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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a3dfa49e-1d53-b649-eaae-1a2bda5b1025@disroot.org \
    --to=calcium@disroot.org \
    --cc=guile-user@gnu.org \
    --cc=thomasmorley65@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).