How to specify a function parameter and return type as a List in ocaml?

TurtleMan

I am currently teaching myself ocaml for a programming language class and I am trying to figure out how to specify a function parameter and return type as a List.

I have create a program that reads a file char by char stores each char in a List, reverses the list then returns the List.

Current code:

(* 
   Creating a function that will read all the chars 
   in a file passed in from the command argument.
   This function takes a parameter of type List.
   This function will return a List. 
*)

let read_file (char_List : List) : List =
    let char_in = open_in Sys.argv.(1) in   (* Creating a file point/in_channel *)
  try
    while true do
      let c = input_char char_in in     (* Getting char from the file *)
        char_List := c :: !char_List    (* Storing the char in the list *)
    done
  with End_of_file ->
        char_List := List.rev !char_List;   (* End of file was reaching reversing char list *)
        close_in char_in;                   (* Closing the file pointer/in_channel *)
;;

(* Storing the result of read_file to buffer which buffer is of type list *)
let buffer = ref [] in
      read_file(buffer);

      print_string "\nThe length of the buffer is: ";
      print_int (List.length !buffer); (* Printing length of the list *)
      print_string ("\n\n");
      List.iter print_char !buffer;    (* Iterating through the list and print each element *)

If I remove specifying the parameter type and return type of List the code runs as intended. However; I would like to specify the type of the parameter and return type as a List.

How do you specify the function parameter and return type to be a List?

octachron

First, List is a module not a type, so you probably meant list. However, you cannot annotate with only list, because a list is not a type by itself: you do not want to have a list of unknowable things, but a list of elements which have themselves a known type. For instance, in your case, you have a list of characters, which can be written as char list. Similarly, a list of integers will be typed int list.

More precisely, list is not a type by itself but a type constructor which takes as argument a type for the elements of the list and returns a type for the list of such elements.

p.s. : if you are learning OCaml you could try to rewrite your code without using references to get used to a more functional style.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to specify a generic type for a function parameter

From Dev

How to return void in a function in OCaml?

From Dev

function without args and return type in OCaml

From Dev

How to return dynamic type depend on the parameter type in function

From Dev

How to specify function parameter with implicit?

From Dev

How to specify function parameter with implicit?

From Dev

Ocaml: function cannot return complete resulting list

From Dev

How to deduce the return type of a function template base on a "function like" parameter?

From Java

Specify return type in TypeScript arrow function

From Java

How to specify "nullable" return type with type hints

From Dev

How to specify "own type" as return type in Kotlin

From Dev

How to specify "nullable" return type with type hints

From Dev

How to understand type in the function body instead of in the function parameter list

From Dev

How to understand type in the function body instead of in the function parameter list

From Dev

OCaml passing labeled function as parameter / labeled function type equivalence

From Dev

How to make Function with return type CList And read that List in Calling function?

From Dev

Generics - Function return type the same as the parameter type?

From Dev

Specify type of record field in OCaml

From Dev

Specify type of record field in OCaml

From Dev

How to specify a parameter of type Array into a Django Command?

From Dev

Accepting only one variant of sum type as OCaml function parameter

From Dev

OCaml: how to determine type of the function argument?

From Dev

How can I specify a return type for operator[]?

From Java

How to specify a return type if it is behind a private module?

From Dev

How to specify the return type of the method of an abstract class

From Dev

How can I specify a return type for operator[]?

From Dev

How to specify the return type of the method of an abstract class

From Dev

How to specify the parameter in a function can be nil in Swift

From Dev

How can I write a function have a polymorphic return type based on the type argument of its type parameter?

Related Related

  1. 1

    How to specify a generic type for a function parameter

  2. 2

    How to return void in a function in OCaml?

  3. 3

    function without args and return type in OCaml

  4. 4

    How to return dynamic type depend on the parameter type in function

  5. 5

    How to specify function parameter with implicit?

  6. 6

    How to specify function parameter with implicit?

  7. 7

    Ocaml: function cannot return complete resulting list

  8. 8

    How to deduce the return type of a function template base on a "function like" parameter?

  9. 9

    Specify return type in TypeScript arrow function

  10. 10

    How to specify "nullable" return type with type hints

  11. 11

    How to specify "own type" as return type in Kotlin

  12. 12

    How to specify "nullable" return type with type hints

  13. 13

    How to understand type in the function body instead of in the function parameter list

  14. 14

    How to understand type in the function body instead of in the function parameter list

  15. 15

    OCaml passing labeled function as parameter / labeled function type equivalence

  16. 16

    How to make Function with return type CList And read that List in Calling function?

  17. 17

    Generics - Function return type the same as the parameter type?

  18. 18

    Specify type of record field in OCaml

  19. 19

    Specify type of record field in OCaml

  20. 20

    How to specify a parameter of type Array into a Django Command?

  21. 21

    Accepting only one variant of sum type as OCaml function parameter

  22. 22

    OCaml: how to determine type of the function argument?

  23. 23

    How can I specify a return type for operator[]?

  24. 24

    How to specify a return type if it is behind a private module?

  25. 25

    How to specify the return type of the method of an abstract class

  26. 26

    How can I specify a return type for operator[]?

  27. 27

    How to specify the return type of the method of an abstract class

  28. 28

    How to specify the parameter in a function can be nil in Swift

  29. 29

    How can I write a function have a polymorphic return type based on the type argument of its type parameter?

HotTag

Archive