if (p:string list) = c (is the only element)

Posted on In QA

if (p:string list) = [c] then (divide p1 c)
showing unbound value c.
i want to equalise (if one element in p which is (c:anything)) and use that variable

let p = ["ocaml"]
let f s = match s with
  | [c] -> print_endline c
  | _ -> print_endline "ops"
f p

It will print:

ocaml

If you like Scala:

scala> def f(s: List[String]): Unit = {
     | s match {
     | case List(c) => print(c)
     | case _ => print("ops")
     | }
     | }
f: (s: List[String])Unit

scala> val p = List("Scala")
p: List[java.lang.String] = List(Scala)

scala> f(p)
Scala

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *