Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow default arguments #131

Merged
merged 9 commits into from
Apr 10, 2023
Merged

Allow default arguments #131

merged 9 commits into from
Apr 10, 2023

Conversation

skx
Copy link
Owner

@skx skx commented Apr 9, 2023

We typically define a function like this:

 (set! hello1 (fn* (name)
   (print "Hello %s" name)))

Here we say there is a parameter "name" which must be supplied, and that is later used.

If we instead say that parameters can be lists we can allow the first value to be the name, and the second a default value if nothing is actually passed, like so:

 (set! hello2 (fn* ( (name "World") )
    (print "Hello %s"  name)))

These could be used like so:

 (hello1 "Steve")  ; "Hello Steve"

 (hello2 "Steve")  ; "Hello Steve"
 (hello2)          ; "Hello World"

I've not yet experimented with supplying defaults in the non-final arguments, because that could screw things up. But for simple cases like the one above it seems to work.

Once complete this will close #130, however I need to add test-cases..

skx added 9 commits April 9, 2023 19:26
We typically define a function like this:

     (set! hello1 (fn* (name)
       (print "Hello %s" name)))

Here we say there is a parameter "name" which must be supplied, and that
is later used.

If we instead say that parameters can be lists we can allow the first
value to be the name, and the second a default value if nothing is
actually passed, like so:

     (set! hello2 (fn* ( (name "World") )
        (print "Hello %s"  name)))

These could be used like so:

     (hello1 "Steve")  ; "Hello Steve"

     (hello2 "Steve")  ; "Hello Steve"
     (hello2)          ; "Hello World"

I've not yet experimented with supplying defaults in the non-final
arguments, because that could screw things up.  But for simple cases
like the one above it seems to work.

Once complete this will close #130, however I need to add test-cases..
Inc/Dec increase/decrease a value by one, but now we add a parameter
which controls how much to add/sub, and make it default to 1.

This means:

   (inc 1 )   ; 2
   (inc 1 19) ; 20

Similarly dec:

   (dec 10 )   ; 9
   (dec 10 2)  ; 8
@skx skx merged commit 51c9f7b into master Apr 10, 2023
@skx skx deleted the 130-defaults branch April 10, 2023 05:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow default arguments
1 participant