Error Syntax Error Expected Ocaml
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company ocaml syntax error pattern expected Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs
Ocaml Syntax Error Operator Expected
Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers,
Syntax Error Expected End Of Line But Found Identifier
just like you, helping each other. Join them; it only takes a minute: Sign up OCaml syntax error in function up vote 2 down vote favorite I have to create a function which will display each element
Syntax Error Expected After This Token
from a set of strings. I did the following: module S = Set.Make(String);; module P = Pervasives;; let write x = ( P.print_string("{"); let first = true; S.iter (fun str -> (if first then () else P.print_string(","); P.print_string(str))) x; P.print_string("}"); P.print_newline );; ^ At the end of the program (where I placed that sign) it appears I have an error: Syntax error: operator expected. Please help me solve this. function syntax syntax-error ocaml share|improve syntax error expected ident for declaration name got literal this question edited Oct 27 '13 at 18:17 Pascal Cuoq 58.5k5101194 asked Oct 27 '13 at 17:16 Andrew 1313 add a comment| 2 Answers 2 active oldest votes up vote 3 down vote accepted I believe your syntactic problem is with let. Except in top-level code (outermost level of a module), let must be followed by in. There are many other problems with this code, but maybe this will let you find the next problem :-) A few notes: Variables in OCaml are immutable. So your variable named first will always be true. You can't change it. This (seemingly minor) point is one of the keys to functional programming. You don't need to reference the Pervasives module by name. That's why it's called "pervasive". You can just say print_string by itself. Your last call to print_newline isn't a call. This expression just evaluates to the function itself. (You need to give it an argument if you want to call the function.) share|improve this answer answered Oct 27 '13 at 17:27 Jeffrey Scofield 35.3k22449 Thank you! I just started using oCaml at my university so I am a total outsider. Is there any way I can execute a block of code only at the first element of the set? –Andrew Oct 27 '13 at 18:02 One way that's
10:202015-12-11 18:57ReporterfuruseAssigned ToPrioritynormalSeveritytextReproducibilityalwaysStatusclosedResolutionsuspendedPlatformOSOS VersionProduct Version4.02.1Target Version4.03.0+dev / syntax error expected something like a name or a unicode delimited identifier +beta1Fixed in VersionSummary0006629: Cryptic syntax error message: operator syntax error expected global temporary expectedDescription(1,2,3,) is rejected by a cryptic message "operator expected".TagsNo tags attached.Attached Files syntax error expected something between the word Relationships child of0005068acknowledgedocamlc/camlp4 should give better error messages for syntax errors Relationships Notes (0012440) furuse (reporter) 2014-10-27 10:20 http://stackoverflow.com/questions/19620952/ocaml-syntax-error-in-function # (1,2,3,);; Characters 7-8: (1,2,3,);; ^ Error: Syntax error: operator expected. # (0012466) doligez (administrator) 2014-10-29 17:38 It does say "syntax error", and it does point to the right place... (0015136) frisch (developer) 2015-12-11 18:57 Marking as suspended. The switch http://caml.inria.fr/mantis/view.php?id=6629 to a Menhir-based parser might allow better error messages "for free". And concrete patches to improve on the current situation are of course welcome. Notes Issue History Date Modified Username Field Change 2014-10-27 10:20 furuse New Issue 2014-10-27 10:20 furuse Note Added: 0012440 2014-10-29 17:38 doligez Note Added: 0012466 2014-10-29 17:38 doligez Severity major => text 2014-10-29 17:38 doligez Status new => acknowledged 2015-01-10 00:20 doligez Target Version => 4.02.3+dev 2015-05-24 16:46 gasche Relationship added child of 0005068 2015-07-10 17:42 doligez Target Version 4.02.3+dev => 4.03.0+dev / +beta1 2015-12-11 18:57 frisch Note Added: 0015136 2015-12-11 18:57 frisch Status acknowledged => closed 2015-12-11 18:57 frisch Resolution open => suspended Issue History Copyright © 2000 - 2011 MantisBT Group
matching is actually even more useful with algebraic data types. In functional programming, an algebraic data type is a kind of composite type, i.e. a type formed by combining other types. Two common classes of algebraic type https://haifengl.wordpress.com/2014/07/07/ocaml-algebraic-data-types/ are Cartesian product types, i.e. tuples and records, and sum types (also called variants). Tuples A https://github.com/ocaml/oloop/issues/30 tuple is a collection of values, called fields, of arbitrary types. # let t = (1, "ocaml");; val t : int * string = (1, "ocaml") To create a tuple, we write multiple values in order between (optional) parentheses and separated by commas. On the other hand, the corresponding type is written as an ordered sequence of n types, separated by syntax error *. We usually work with tuple values directly and let the compiler infer the type. Of course, we can explicitly define a tuple type. # type mytype = int * string;; type mytype = int * string The way to get values back out of a tuple is by pattern matching: # let (x, y) = t in x;; - : int = 1 # let (x, y) = t in y;; - : string = syntax error expected "ocaml" The structure (x, y) is a pattern to match the fields of tuple. This is a perfect example that patterns are used to select data structures of a given shape, and bind identifiers to components of the data structure. Because it is a very common operation, OCaml has the helper functions fst and snd to return the first and second field of a pair. # fst t;; - : int = 1 # snd t;; - : string = "ocaml" We can easily implement these functions: # let fst (x, _) = x;; val fst : 'a * 'b -> 'a =
Sign in Pricing Blog Support Search GitHub This repository Watch 5 Star 11 Fork 0 ocaml/oloop Code Issues 10 Pull requests 0 Projects 0 Pulse Graphs New issue report syntax error even with camlp4o #30 Open agarwal opened this Issue May 26, 2015 · 0 comments Projects None yet Labels None yet Milestone No milestone Assignees No one assigned 1 participant OCaml member agarwal commented May 26, 2015 We get different errors if #camlp4o is issued or not. IIUC, we are not catching the correct error when camlp4 is used. Without camlp4o: $ cat a.ml let x-plus-y = x + y;; $ oloop a.ml (* part 0 *) # let x-plus-y = x + y;; Error: Syntax error With camlp4o: $ cat a.ml #use "topfind";; #camlp4o;; let x-plus-y = x + y;; $ oloop a.ml -silent-directives (* part 0 *) # #use "topfind";; # #camlp4o;; # let x-plus-y = x + y;; Pervasives.Exit Note the specific error also differs in OCaml's toplevel: $ ocaml -noinit OCaml version 4.02.1 # let x-plus-y = x + y;; Error: Syntax error versus: $ ocaml -noinit OCaml version 4.02.1 # #use "topfind";; # #camlp4o;; # let x-plus-y = x + y;; Error: Parse error: [fun_binding] expected after [ipatt] (in [let_binding]) Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment Contact GitHub API Training Shop Blog About © 2016 GitHub, Inc. Terms Privacy Security Status Help You can't perform that action at this time. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.