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;








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)))









share|improve this question
























  • 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












  • lol, I'm so sleepy :c yep it was the problem thanks <3

    – Farengar
    Mar 27 at 8:52

















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)))









share|improve this question
























  • 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












  • lol, I'm so sleepy :c yep it was the problem thanks <3

    – Farengar
    Mar 27 at 8:52













0












0








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)))









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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

















  • 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












  • 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












1 Answer
1






active

oldest

votes


















0














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





share|improve this answer
























    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
    );



    );













    draft saved

    draft discarded


















    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









    0














    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





    share|improve this answer





























      0














      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





      share|improve this answer



























        0












        0








        0







        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





        share|improve this answer













        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 27 at 12:58









        ivgivg

        23.9k2 gold badges18 silver badges45 bronze badges




        23.9k2 gold badges18 silver badges45 bronze badges





















            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.



















            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해