Can i get alpha and boolean type from a match in ocaml?How to create a new object instance from a TypeWhat is the difference between bool and Boolean types in C#Is it possible to have a Ocaml function that accepts only integer lists?Is there a Boolean data type in Microsoft SQL Server like there is in MySQL?ocaml any types matchingGetting internal value of a type OCamlRecord type pattern matching in OcamlHow to check if type is BooleanWhat is the correct way of writing typeof(Xml) for C# .NET?Grabbing a dictionary given just a few keys and values
ESTA declined to the US
How do these cubesats' whip antennas work?
How do I say "Outdoor Pre-show"
"How do you solve a problem like Maria?"
Premier League simulation
How is the return type of a ternary operator determined?
How to realistically deal with a shield user?
Unexpected route on a flight from USA to Europe
Is it allowed and safe to carry a passenger / non-pilot in the front seat of a small general aviation airplane?
Is Odin inconsistent about the powers of Mjolnir?
What word can be used to describe a bug in a movie?
Journalctl shows lots of foreign addresses, what are they and what do they mean?
Was Richard I's imprisonment by Leopold of Austria justified?
Double blind peer review when paper cites author's GitHub repo for code
Traveling from Germany to other countries by train?
Capacitors with a "/" on schematic
Why should I "believe in" weak solutions to PDEs?
Did Apollo leave poop on the moon?
4-dimensional Knight's Tour
What are these mathematical groups in U.S. universities?
How do I get the =LEFT function in excel, to also take the number zero as the first number?
Why do proponents of guns oppose gun competency tests?
What is a Casino Word™?
What is the German idiom or expression for when someone is being hypocritical against their own teachings?
Can i get alpha and boolean type from a match in ocaml?
How to create a new object instance from a TypeWhat is the difference between bool and Boolean types in C#Is it possible to have a Ocaml function that accepts only integer lists?Is there a Boolean data type in Microsoft SQL Server like there is in MySQL?ocaml any types matchingGetting internal value of a type OCamlRecord type pattern matching in OcamlHow to check if type is BooleanWhat is the correct way of writing typeof(Xml) for C# .NET?Grabbing a dictionary given just a few keys and values
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm doing a recursive propositional calculator but I have a problem using a dictionary, I'm using pair list like a dictionary and my own function lookup to search for a value in the dictionary.
Now lookup returns an alpha type, but in my main function (calcPrep) the match must return a bool, I don't see a problem whit the syntaxis, at the end that alpha type can be a bool...
If I change
| '('::variable::')'::_ -> lookup (variable, dictionary)
to
| '('::variable::')'::_ -> true
the match works, but obviously, I didn't get the value of the variable.
I'm thinking of use maps as a dictionary instead pair list, but it's more complex to me...
Here is my code until now
let rec lookup x l =
match l with
| [] -> raise Not_found
| (key, value)::rest ->
if key = x then
value
else
lookup x rest
let rec calcPrep charList dictionary=
let n = List.length charList in
match charList with
| '('::variable::')'::_ -> lookup (variable, dictionary)
| '('::'!'::_ -> not (calcPrep(sublist 2 (n-1) charList) dictionary)
| _ ->
let pos = getPositionOperator charList 0 0 in
let operator = (List.nth charList pos) in
match operator with
| '&' -> (calcPrep (sublist 1 (pos - 1) charList) dictionary) && (calcPrep (sublist (pos + 1) (n - 2) charList) dictionary)
| '|' -> (calcPrep (sublist 1 (pos - 1) charList) dictionary) && (calcPrep (sublist (pos + 1) (n - 2) charList) dictionary)
| _ -> failwith ("Incorrect operator: " ^ String.make 1 (List.nth charList (pos - 1)))
types match ocaml
add a comment |
I'm doing a recursive propositional calculator but I have a problem using a dictionary, I'm using pair list like a dictionary and my own function lookup to search for a value in the dictionary.
Now lookup returns an alpha type, but in my main function (calcPrep) the match must return a bool, I don't see a problem whit the syntaxis, at the end that alpha type can be a bool...
If I change
| '('::variable::')'::_ -> lookup (variable, dictionary)
to
| '('::variable::')'::_ -> true
the match works, but obviously, I didn't get the value of the variable.
I'm thinking of use maps as a dictionary instead pair list, but it's more complex to me...
Here is my code until now
let rec lookup x l =
match l with
| [] -> raise Not_found
| (key, value)::rest ->
if key = x then
value
else
lookup x rest
let rec calcPrep charList dictionary=
let n = List.length charList in
match charList with
| '('::variable::')'::_ -> lookup (variable, dictionary)
| '('::'!'::_ -> not (calcPrep(sublist 2 (n-1) charList) dictionary)
| _ ->
let pos = getPositionOperator charList 0 0 in
let operator = (List.nth charList pos) in
match operator with
| '&' -> (calcPrep (sublist 1 (pos - 1) charList) dictionary) && (calcPrep (sublist (pos + 1) (n - 2) charList) dictionary)
| '|' -> (calcPrep (sublist 1 (pos - 1) charList) dictionary) && (calcPrep (sublist (pos + 1) (n - 2) charList) dictionary)
| _ -> failwith ("Incorrect operator: " ^ String.make 1 (List.nth charList (pos - 1)))
types match ocaml
The error at the console is: This expression has type ((char * 'a) * 'b) list -> 'b but an expression was expected of type bool
– Farengar
Mar 27 at 5:54
I see at least one obvious problem with the syntax: you apply lookup to a tuple aslookup (variable, dictionary)but according to its definition it expects 2 params (value and a list), so it should be applied aslookup variable dictionary. Next thing is thatnot xexpectsxto bebool. Is your calcPrep intended to return bool always? If so, did you try to specify its type explicitly to help the typechecker? (P.S. I can't check your code in toplevel because it lacks the definitions ofsublistandgetPositionOperator;sublistis more or less obvious, the second one is not...)
– Konstantin Strukov
Mar 27 at 8:39
lol, I'm so sleepy :c yep it was the problem thanks <3
– Farengar
Mar 27 at 8:52
add a comment |
I'm doing a recursive propositional calculator but I have a problem using a dictionary, I'm using pair list like a dictionary and my own function lookup to search for a value in the dictionary.
Now lookup returns an alpha type, but in my main function (calcPrep) the match must return a bool, I don't see a problem whit the syntaxis, at the end that alpha type can be a bool...
If I change
| '('::variable::')'::_ -> lookup (variable, dictionary)
to
| '('::variable::')'::_ -> true
the match works, but obviously, I didn't get the value of the variable.
I'm thinking of use maps as a dictionary instead pair list, but it's more complex to me...
Here is my code until now
let rec lookup x l =
match l with
| [] -> raise Not_found
| (key, value)::rest ->
if key = x then
value
else
lookup x rest
let rec calcPrep charList dictionary=
let n = List.length charList in
match charList with
| '('::variable::')'::_ -> lookup (variable, dictionary)
| '('::'!'::_ -> not (calcPrep(sublist 2 (n-1) charList) dictionary)
| _ ->
let pos = getPositionOperator charList 0 0 in
let operator = (List.nth charList pos) in
match operator with
| '&' -> (calcPrep (sublist 1 (pos - 1) charList) dictionary) && (calcPrep (sublist (pos + 1) (n - 2) charList) dictionary)
| '|' -> (calcPrep (sublist 1 (pos - 1) charList) dictionary) && (calcPrep (sublist (pos + 1) (n - 2) charList) dictionary)
| _ -> failwith ("Incorrect operator: " ^ String.make 1 (List.nth charList (pos - 1)))
types match ocaml
I'm doing a recursive propositional calculator but I have a problem using a dictionary, I'm using pair list like a dictionary and my own function lookup to search for a value in the dictionary.
Now lookup returns an alpha type, but in my main function (calcPrep) the match must return a bool, I don't see a problem whit the syntaxis, at the end that alpha type can be a bool...
If I change
| '('::variable::')'::_ -> lookup (variable, dictionary)
to
| '('::variable::')'::_ -> true
the match works, but obviously, I didn't get the value of the variable.
I'm thinking of use maps as a dictionary instead pair list, but it's more complex to me...
Here is my code until now
let rec lookup x l =
match l with
| [] -> raise Not_found
| (key, value)::rest ->
if key = x then
value
else
lookup x rest
let rec calcPrep charList dictionary=
let n = List.length charList in
match charList with
| '('::variable::')'::_ -> lookup (variable, dictionary)
| '('::'!'::_ -> not (calcPrep(sublist 2 (n-1) charList) dictionary)
| _ ->
let pos = getPositionOperator charList 0 0 in
let operator = (List.nth charList pos) in
match operator with
| '&' -> (calcPrep (sublist 1 (pos - 1) charList) dictionary) && (calcPrep (sublist (pos + 1) (n - 2) charList) dictionary)
| '|' -> (calcPrep (sublist 1 (pos - 1) charList) dictionary) && (calcPrep (sublist (pos + 1) (n - 2) charList) dictionary)
| _ -> failwith ("Incorrect operator: " ^ String.make 1 (List.nth charList (pos - 1)))
types match ocaml
types match ocaml
asked Mar 27 at 5:52
FarengarFarengar
257 bronze badges
257 bronze badges
The error at the console is: This expression has type ((char * 'a) * 'b) list -> 'b but an expression was expected of type bool
– Farengar
Mar 27 at 5:54
I see at least one obvious problem with the syntax: you apply lookup to a tuple aslookup (variable, dictionary)but according to its definition it expects 2 params (value and a list), so it should be applied aslookup variable dictionary. Next thing is thatnot xexpectsxto bebool. Is your calcPrep intended to return bool always? If so, did you try to specify its type explicitly to help the typechecker? (P.S. I can't check your code in toplevel because it lacks the definitions ofsublistandgetPositionOperator;sublistis more or less obvious, the second one is not...)
– Konstantin Strukov
Mar 27 at 8:39
lol, I'm so sleepy :c yep it was the problem thanks <3
– Farengar
Mar 27 at 8:52
add a comment |
The error at the console is: This expression has type ((char * 'a) * 'b) list -> 'b but an expression was expected of type bool
– Farengar
Mar 27 at 5:54
I see at least one obvious problem with the syntax: you apply lookup to a tuple aslookup (variable, dictionary)but according to its definition it expects 2 params (value and a list), so it should be applied aslookup variable dictionary. Next thing is thatnot xexpectsxto bebool. Is your calcPrep intended to return bool always? If so, did you try to specify its type explicitly to help the typechecker? (P.S. I can't check your code in toplevel because it lacks the definitions ofsublistandgetPositionOperator;sublistis more or less obvious, the second one is not...)
– Konstantin Strukov
Mar 27 at 8:39
lol, I'm so sleepy :c yep it was the problem thanks <3
– Farengar
Mar 27 at 8:52
The error at the console is: This expression has type ((char * 'a) * 'b) list -> 'b but an expression was expected of type bool
– Farengar
Mar 27 at 5:54
The error at the console is: This expression has type ((char * 'a) * 'b) list -> 'b but an expression was expected of type bool
– Farengar
Mar 27 at 5:54
I see at least one obvious problem with the syntax: you apply lookup to a tuple as
lookup (variable, dictionary) but according to its definition it expects 2 params (value and a list), so it should be applied as lookup variable dictionary. Next thing is that not x expects x to be bool. Is your calcPrep intended to return bool always? If so, did you try to specify its type explicitly to help the typechecker? (P.S. I can't check your code in toplevel because it lacks the definitions of sublist and getPositionOperator; sublist is more or less obvious, the second one is not...)– Konstantin Strukov
Mar 27 at 8:39
I see at least one obvious problem with the syntax: you apply lookup to a tuple as
lookup (variable, dictionary) but according to its definition it expects 2 params (value and a list), so it should be applied as lookup variable dictionary. Next thing is that not x expects x to be bool. Is your calcPrep intended to return bool always? If so, did you try to specify its type explicitly to help the typechecker? (P.S. I can't check your code in toplevel because it lacks the definitions of sublist and getPositionOperator; sublist is more or less obvious, the second one is not...)– Konstantin Strukov
Mar 27 at 8:39
lol, I'm so sleepy :c yep it was the problem thanks <3
– Farengar
Mar 27 at 8:52
lol, I'm so sleepy :c yep it was the problem thanks <3
– Farengar
Mar 27 at 8:52
add a comment |
1 Answer
1
active
oldest
votes
First of all, alpha, aka 'a, doesn't mean "anything", it is a type variable that should be considered in a context. For example, in the type of a function,
val find: ('k,'a) list -> 'k -> 'a
we have two type variables 'k and 'a, with the 'k denoting the type of keys, and 'a denoting the type of values. Therefore, when you apply this function to some list, e.g., find [1, "one"; 2, "two"] the 'k variable will be instantiated to int and 'a to string, and the type of this expression will be int -> string.
Second, a function that checks whether a particular key or element is present in a container is usually called mem. It could be easily implemented via the find function, that search for the element, e.g.,
let mem ls k = try ignore (find ls k); false with Not_found -> true
This function will have type ('k,'a) list -> 'k -> bool.
In the modern OCaml, it is prefered to use a more explicit 'a option type as the return type of the find function, instead of throwing an exception, since the former appears in the type of the function and is harder to miss. So if you have a find function returning 'a option, then the implementation will look like this
let mem ls ks = match find ls k with None -> false | Some _ -> true
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55370613%2fcan-i-get-alpha-and-boolean-type-from-a-match-in-ocaml%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
First of all, alpha, aka 'a, doesn't mean "anything", it is a type variable that should be considered in a context. For example, in the type of a function,
val find: ('k,'a) list -> 'k -> 'a
we have two type variables 'k and 'a, with the 'k denoting the type of keys, and 'a denoting the type of values. Therefore, when you apply this function to some list, e.g., find [1, "one"; 2, "two"] the 'k variable will be instantiated to int and 'a to string, and the type of this expression will be int -> string.
Second, a function that checks whether a particular key or element is present in a container is usually called mem. It could be easily implemented via the find function, that search for the element, e.g.,
let mem ls k = try ignore (find ls k); false with Not_found -> true
This function will have type ('k,'a) list -> 'k -> bool.
In the modern OCaml, it is prefered to use a more explicit 'a option type as the return type of the find function, instead of throwing an exception, since the former appears in the type of the function and is harder to miss. So if you have a find function returning 'a option, then the implementation will look like this
let mem ls ks = match find ls k with None -> false | Some _ -> true
add a comment |
First of all, alpha, aka 'a, doesn't mean "anything", it is a type variable that should be considered in a context. For example, in the type of a function,
val find: ('k,'a) list -> 'k -> 'a
we have two type variables 'k and 'a, with the 'k denoting the type of keys, and 'a denoting the type of values. Therefore, when you apply this function to some list, e.g., find [1, "one"; 2, "two"] the 'k variable will be instantiated to int and 'a to string, and the type of this expression will be int -> string.
Second, a function that checks whether a particular key or element is present in a container is usually called mem. It could be easily implemented via the find function, that search for the element, e.g.,
let mem ls k = try ignore (find ls k); false with Not_found -> true
This function will have type ('k,'a) list -> 'k -> bool.
In the modern OCaml, it is prefered to use a more explicit 'a option type as the return type of the find function, instead of throwing an exception, since the former appears in the type of the function and is harder to miss. So if you have a find function returning 'a option, then the implementation will look like this
let mem ls ks = match find ls k with None -> false | Some _ -> true
add a comment |
First of all, alpha, aka 'a, doesn't mean "anything", it is a type variable that should be considered in a context. For example, in the type of a function,
val find: ('k,'a) list -> 'k -> 'a
we have two type variables 'k and 'a, with the 'k denoting the type of keys, and 'a denoting the type of values. Therefore, when you apply this function to some list, e.g., find [1, "one"; 2, "two"] the 'k variable will be instantiated to int and 'a to string, and the type of this expression will be int -> string.
Second, a function that checks whether a particular key or element is present in a container is usually called mem. It could be easily implemented via the find function, that search for the element, e.g.,
let mem ls k = try ignore (find ls k); false with Not_found -> true
This function will have type ('k,'a) list -> 'k -> bool.
In the modern OCaml, it is prefered to use a more explicit 'a option type as the return type of the find function, instead of throwing an exception, since the former appears in the type of the function and is harder to miss. So if you have a find function returning 'a option, then the implementation will look like this
let mem ls ks = match find ls k with None -> false | Some _ -> true
First of all, alpha, aka 'a, doesn't mean "anything", it is a type variable that should be considered in a context. For example, in the type of a function,
val find: ('k,'a) list -> 'k -> 'a
we have two type variables 'k and 'a, with the 'k denoting the type of keys, and 'a denoting the type of values. Therefore, when you apply this function to some list, e.g., find [1, "one"; 2, "two"] the 'k variable will be instantiated to int and 'a to string, and the type of this expression will be int -> string.
Second, a function that checks whether a particular key or element is present in a container is usually called mem. It could be easily implemented via the find function, that search for the element, e.g.,
let mem ls k = try ignore (find ls k); false with Not_found -> true
This function will have type ('k,'a) list -> 'k -> bool.
In the modern OCaml, it is prefered to use a more explicit 'a option type as the return type of the find function, instead of throwing an exception, since the former appears in the type of the function and is harder to miss. So if you have a find function returning 'a option, then the implementation will look like this
let mem ls ks = match find ls k with None -> false | Some _ -> true
answered Mar 27 at 12:58
ivgivg
23.9k2 gold badges18 silver badges45 bronze badges
23.9k2 gold badges18 silver badges45 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55370613%2fcan-i-get-alpha-and-boolean-type-from-a-match-in-ocaml%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The error at the console is: This expression has type ((char * 'a) * 'b) list -> 'b but an expression was expected of type bool
– Farengar
Mar 27 at 5:54
I see at least one obvious problem with the syntax: you apply lookup to a tuple as
lookup (variable, dictionary)but according to its definition it expects 2 params (value and a list), so it should be applied aslookup variable dictionary. Next thing is thatnot xexpectsxto bebool. Is your calcPrep intended to return bool always? If so, did you try to specify its type explicitly to help the typechecker? (P.S. I can't check your code in toplevel because it lacks the definitions ofsublistandgetPositionOperator;sublistis more or less obvious, the second one is not...)– Konstantin Strukov
Mar 27 at 8:39
lol, I'm so sleepy :c yep it was the problem thanks <3
– Farengar
Mar 27 at 8:52