The use of #: in the program listing below

I'm less than a week into learning DrRacket and one of my books on Racket had a brief routine to plot 2 cycles of a sine wave. It didn't run as written in the book but it was fairly easy to get it to run in DrRacket however, in the listing below I do not understand the last two #: statements and even after reading the DrRacket documentation I do not understand. A simple explanation would be greatly appreciated..

#lang racket
(require plot)
(plot-new-window? #t)
(plot (function sin #:color "Blue")
      #:x-min (* -2 pi) #:x-max {* 2 pi}
      #:title "The Sine Function")
1 Like

i'm not sure to understand well the question... but #: seems to be used to use named parameters and the {* 2 pi} could be (* 2 pi) even if the 2 are working in Racket { } is used in SRFI-105 curly-infix.

See the plot docs and then Keywords in the Reference which also links to the Guide.

You might also want to know how to make your own procedures that accept keyword arguments; see Procedure expressions as a possible starting point, or maybe Notation for documentation.

Thanks for the reply however I may be a little dense (or a tad senile :rofl:) but this usage #:x-min (* -2 pi) #:x-max {* 2 pi) wasn’t obvious like this is (plot (function sin (* -2 pi)(* 2 pi) #:label "y = sin(x)")) . I will go back and re-read the stuff on :# and see if I can see where I missed the boat.

Cheers,

(Code formatting mine, and I switched an erroneous {* 2 pi) for (* 2 pi) even though the original post had {* 2 pi}.)

The docs for how a keyword affects the procedure are in the documentation for that procedure (so plot, I linked above).

I may not be understanding your question, though.

Are you familiar with other programming languages? Python calls these keyword arguments with a plot(x_min=2*pi, …) syntax. Some other languages call these "named arguments."

Other programming languages, FORTRAN 66 using IBM punch cards on mainframe computer. that filled a very large room. When I got my first computer running CP/M with a Z80 running at a blazing 4MHz, I used C-Basic until the initial release of Turbo Pascal which I used in my work for a decade or so, also briefly flirted with Modula2 however around 1970 I switched to FORTH for the remainder of may 52 year career. Since retiring I have been using Mathematica for all of my post retirement consulting work however, it takes the concept of a "High Level Language" to extreme heights. So, for my personal computing needs I looked for something different and after trying a few things things settled on DrRacket which is not all that much like any of my previous experience. However, it's much fun and the DrRacket IDE is great.

2 Likes

On Jan 21, 2025, at 5:57 PM, munroec wrote that "it's much fun and the DrRacket IDE is great.” Thanks for your feedback and I am sure Robby loves to receive this well-deserved compliment in writing.

2 Likes

Thank you all that helped get me by this latest sticking point. BTW I have found that this form of the problem is the one that I'm most comfortable with.

(require plot)
(plot-new-window? #t)
(plot (function cosh #:color "Red")
                     #:x-min (* -2 pi) 
                     #:x-max {* 2 pi}
                     #:title "The  Hyperbolic Cosine (cosh) Function")

If you have a procedure with ten parameters, you probably missed some, but meanwhile at least use keyword arguments.

-- Sort of but not quite Alan Perlis

3 Likes

You're undoubtedly correct and every other language I have experience with that is the method, however the truth is I have not gotten that far in the book that I'm working from to know how to create a named variable! :rofl: Seriously though I do appreciate the comments, they're quite helpful and this is all great fun.

Cheers,

1 Like

Greg,

Is this sorta what you meant or is there as better method?

#lang racket
(define omega (* 2 pi 1000))
(define L 0.12)
(define XL (* omega L))
XL

Thanks again
Cheers,

@munroec One way to understand that Alan Perlis quote ("If you have a procedure with ten parameters, you probably forgot some"): Functions should take fewer parameters, and you should compose them.

He has another quote ~= "100 functions on 1 data structure are better than 20 on 20".

Seems he's a fan defining many, more-specific functions.

Which is good advice. But sometimes it's hard to avoid a function or data structure taking a big "duffel bag" of a dozen values.

[Maybe some real-world spec determines that, and you're modeling it. Or maybe you want to supply a convenience function for people who don't want to learn how to compose a bazillion functions exactly perfectly. Or maybe you want to de-duplicate code from various simple "flavor" functions into a core, private, helper function. Or some other scenario.]


Then you have the classic choice of "by position" or "by name". Do you call the function (or init the data structure) by position: (foo 0 1 2)? With three values, it's not too bad, but if you have a dozen, you're likely make a mistake. So by name is "safer": (foo #:a 0 #:b 1 #:c 2) is clear; (foo #:c 2 #:b 1 #:a 0) is identical (order doesn't matter).

There's also the choice of default values. Often, if you have a dozen parameters, some will have reasonable defaults most of the time (like 0 or #false or whatever). So it can be nice for functions to have optional parameters -- if you don't supply a value, a default value will be used. But these much "safer" as named, keyword arguments.

Finally, it might be OK or preferable for a function to take one or two "obvious" required parameters by-position, and the rest are named and/or optional.


TL;DR: In Racket you can say (define (foo a #:b b #:c [c 42]) ___) to define a function foo that takes

  • a required by-position argument bound to a inside the function
  • a required keyword argument #:b (value bound to b inside the function)
  • an optional keyword argument #:c (c defaulting to 42 if not supplied)

This can be called as (foo "hello"), (foo "hello" #:b "world"), or (foo "hello" #:b "world" #:c 999).

plot is an example of using this technique.

1 Like

Awesome, thank you! : :slightly_smiling_face:

1 Like