"ocaml_beginners"::[] passing on optional arguments

View: New views
7 Messages — Rating Filter:   Alert me  

"ocaml_beginners"::[] passing on optional arguments

by Erick Matsen-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello Ocaml folk---


Let's say that we have a function with an optional argument like this:

# let george ?nickname lastname =
  "George "
    ^(match nickname with
      | Some x -> x^" "
      | None -> "")
  ^lastname;;
val george : ?nickname:string -> string -> string = <fun>

I'd like to pass on that optional argument as follows, such that "statement" has
an optional argument as well.

# let statement ?nickname lastname rest =
    (george ~nickname lastname)^" "^rest;;

However, this of course has the wrong type:

Error: This expression has type 'a option
       but an expression was expected of type string

When we specify nickname with ~nickname, the type should be string, not string
option.

Is there a slick way around this? One can certainly do

let statement ?nickname lastname rest =
  match nickname with
  | Some nick ->
      (george ~nickname:nick lastname)^" "^rest
  | None -> (george lastname)^" "^rest

which works, but lacks elegance.


Thanks,

Erick

"ocaml_beginners"::[] Re: passing on optional arguments

by Sebastien Mondet :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi

you can simply reuse the '?' to tell ocaml that you want to pass the "option":


# let george ?nickname lastname =
 "George "
   ^(match nickname with
     | Some x -> x^" "
     | None -> "")
 ^lastname;;          
val george : ?nickname:string -> string -> string = <fun>

# let statement ?nickname lastname rest =
     (george ?nickname lastname)^" "^rest;;
val statement : ?nickname:string -> string -> string -> string = <fun>


Sebastien Mondet
http://seb.mondet.org





--- In ocaml_beginners@..., Erick Matsen <ematsen@...> wrote:

>
> Hello Ocaml folk---
>
>
> Let's say that we have a function with an optional argument like this:
>
> # let george ?nickname lastname =
>   "George "
>     ^(match nickname with
>       | Some x -> x^" "
>       | None -> "")
>   ^lastname;;
> val george : ?nickname:string -> string -> string = <fun>
>
> I'd like to pass on that optional argument as follows, such that "statement" has
> an optional argument as well.
>
> # let statement ?nickname lastname rest =
>     (george ~nickname lastname)^" "^rest;;
>
> However, this of course has the wrong type:
>
> Error: This expression has type 'a option
>        but an expression was expected of type string
>
> When we specify nickname with ~nickname, the type should be string, not string
> option.
>
> Is there a slick way around this? One can certainly do
>
> let statement ?nickname lastname rest =
>   match nickname with
>   | Some nick ->
>       (george ~nickname:nick lastname)^" "^rest
>   | None -> (george lastname)^" "^rest
>
> which works, but lacks elegance.
>
>
> Thanks,
>
> Erick
>



Re: "ocaml_beginners"::[] passing on optional arguments

by Lukasz Stafiniak :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Oct 21, 2009 at 2:31 PM, Erick Matsen <ematsen@...> wrote:

>
>
>
> Hello Ocaml folk---
>
> Let's say that we have a function with an optional argument like this:
>
> # let george ?nickname lastname =
> "George "
> ^(match nickname with
> | Some x -> x^" "
> | None -> "")
> ^lastname;;
> val george : ?nickname:string -> string -> string = <fun>
>
> I'd like to pass on that optional argument as follows, such that "statement" has
> an optional argument as well.
>
> # let statement ?nickname lastname rest =
> (george ~nickname lastname)^" "^rest;;
>
> However, this of course has the wrong type:
>
> Error: This expression has type 'a option
> but an expression was expected of type string
>
> When we specify nickname with ~nickname, the type should be string, not string
> option.
>
> Is there a slick way around this?

Sure.

let statement ?nickname lastname rest =
george ?nickname lastname ^ " " ^ rest

Re: "ocaml_beginners"::[] passing on optional arguments

by Richard Jones-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Oct 21, 2009 at 05:31:48AM -0700, Erick Matsen wrote:
> Let's say that we have a function with an optional argument like this:
>
> # let george ?nickname lastname =
>   "George "
>     ^(match nickname with
>       | Some x -> x^" "
>       | None -> "")
>   ^lastname;;
> val george : ?nickname:string -> string -> string = <fun>

As others have said already, ?nickname.

I wrote a bit about labelled and optional arguments here:

  http://ocaml-tutorial.org/labels
    "Labelled and optional arguments to functions"

They are quite idiosyncratic, but there are only a few variations that
you really use in everyday code.

The main thing to remember about labels is that ~foo is an
abbreviation for ~foo:foo.

And about optional arguments: Calling a function "f ?foo ()" is
shorthand for calling the function with like this:

  match foo with
  | None -> f ()                    [1]
  | Some foo -> f ~foo:foo ()

Rich.

[1] Kind of: what happens in reality is that a foo parameter is always
passed to f.  It's either None or Some _, and f contains some hidden
code to handle both cases.

--
Richard Jones
Red Hat

Re: "ocaml_beginners"::[] passing on optional arguments

by Erick Matsen-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Lukasz--


Thank you!


Erick


On Wed, Oct 21, 2009 at 5:38 AM, Lukasz Stafiniak <lukstafi@...>wrote:

>
>
> On Wed, Oct 21, 2009 at 2:31 PM, Erick Matsen <ematsen@...<ematsen%40gmail.com>>
> wrote:
> >
> >
> >
> > Hello Ocaml folk---
> >
> > Let's say that we have a function with an optional argument like this:
> >
> > # let george ?nickname lastname =
> > "George "
> > ^(match nickname with
> > | Some x -> x^" "
> > | None -> "")
> > ^lastname;;
> > val george : ?nickname:string -> string -> string = <fun>
> >
> > I'd like to pass on that optional argument as follows, such that
> "statement" has
> > an optional argument as well.
> >
> > # let statement ?nickname lastname rest =
> > (george ~nickname lastname)^" "^rest;;
> >
> > However, this of course has the wrong type:
> >
> > Error: This expression has type 'a option
> > but an expression was expected of type string
> >
> > When we specify nickname with ~nickname, the type should be string, not
> string
> > option.
> >
> > Is there a slick way around this?
>
> Sure.
>
>
> let statement ?nickname lastname rest =
> george ?nickname lastname ^ " " ^ rest
>  
>


[Non-text portions of this message have been removed]


Re: "ocaml_beginners"::[] Re: passing on optional arguments

by Erick Matsen-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Sebastien---


Thank you! Works great.


Erick

On Wed, Oct 21, 2009 at 5:37 AM, sebmondet <sebastien.mondet@...>wrote:

>
>
>
> Hi
>
> you can simply reuse the '?' to tell ocaml that you want to pass the
> "option":
>
> # let george ?nickname lastname =
> "George "
> ^(match nickname with
> | Some x -> x^" "
> | None -> "")
> ^lastname;;
> val george : ?nickname:string -> string -> string = <fun>
>
> # let statement ?nickname lastname rest =
> (george ?nickname lastname)^" "^rest;;
> val statement : ?nickname:string -> string -> string -> string = <fun>
>
> Sebastien Mondet
> http://seb.mondet.org
>
> --- In ocaml_beginners@... <ocaml_beginners%40yahoogroups.com>,
> Erick Matsen <ematsen@...> wrote:
> >
> > Hello Ocaml folk---
> >
> >
> > Let's say that we have a function with an optional argument like this:
> >
> > # let george ?nickname lastname =
> > "George "
> > ^(match nickname with
> > | Some x -> x^" "
> > | None -> "")
> > ^lastname;;
> > val george : ?nickname:string -> string -> string = <fun>
> >
> > I'd like to pass on that optional argument as follows, such that
> "statement" has
> > an optional argument as well.
> >
> > # let statement ?nickname lastname rest =
> > (george ~nickname lastname)^" "^rest;;
> >
> > However, this of course has the wrong type:
> >
> > Error: This expression has type 'a option
> > but an expression was expected of type string
> >
> > When we specify nickname with ~nickname, the type should be string, not
> string
> > option.
> >
> > Is there a slick way around this? One can certainly do
> >
> > let statement ?nickname lastname rest =
> > match nickname with
> > | Some nick ->
> > (george ~nickname:nick lastname)^" "^rest
> > | None -> (george lastname)^" "^rest
> >
> > which works, but lacks elegance.
> >
> >
> > Thanks,
> >
> > Erick
> >
>
>  
>


[Non-text portions of this message have been removed]


Re: "ocaml_beginners"::[] passing on optional arguments

by Erick Matsen-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Rich--


I'm a bit slow getting back to you, but I wanted to say thanks for the help.
I really appreciate it.


Erick


On Wed, Oct 21, 2009 at 5:59 AM, Richard Jones <rich@...> wrote:

>
>
> On Wed, Oct 21, 2009 at 05:31:48AM -0700, Erick Matsen wrote:
> > Let's say that we have a function with an optional argument like this:
> >
> > # let george ?nickname lastname =
> > "George "
> > ^(match nickname with
> > | Some x -> x^" "
> > | None -> "")
> > ^lastname;;
> > val george : ?nickname:string -> string -> string = <fun>
>
> As others have said already, ?nickname.
>
> I wrote a bit about labelled and optional arguments here:
>
> http://ocaml-tutorial.org/labels
> "Labelled and optional arguments to functions"
>
> They are quite idiosyncratic, but there are only a few variations that
> you really use in everyday code.
>
> The main thing to remember about labels is that ~foo is an
> abbreviation for ~foo:foo.
>
> And about optional arguments: Calling a function "f ?foo ()" is
> shorthand for calling the function with like this:
>
> match foo with
> | None -> f () [1]
> | Some foo -> f ~foo:foo ()
>
> Rich.
>
> [1] Kind of: what happens in reality is that a foo parameter is always
> passed to f. It's either None or Some _, and f contains some hidden
> code to handle both cases.
>
> --
> Richard Jones
> Red Hat
>  
>


[Non-text portions of this message have been removed]