function
Declaration of functions inside a module OCaml
So, i'am learning OCaml step by step and i've recently met the great world of "module langage" in OCaml. Here is my problem: module type TEST = sig val addend : 'a -> list end module Test : TEST = struct let addend (val,lol) = val::lol end when i try to use this inside the interpretor it tells me that i have a syntax error ... I know that's this kind of anoying question asked by this guy who's a noob and thinks stack overflow is here to rewrite is poorly optimised code but still, after hours spend searching for a solution i've almost gived up at this point... OCaml is cool, but why is it so hard.
First, in the definition of the type of your module : you have to specify the type of element of your list. You cannot use list alone. The following code will compile : module type TEST = sig val addend : 'a -> 'a list end;; But will not do what you want as you want to define later a function that takes a element, a list and puts this element in this list. So the signature of such function is : 'a -> 'a list -> 'a list So the type of your module shall be : module type TEST = sig val addend : 'a -> 'a list -> 'a list end;; And the implementation : module Test : TEST = struct let addend a l = a::l end ;; Please note that in your code, you may be influenced by C like language where you pass the arguments in parenthesis. In Ocaml, parenthesis are used for tuples ; this means that your implementation was different from the signature.
You get a syntax error becaue val is a keyword. You cannot use it as an identifier.
Related Links
in SAS proc fcmp how to get 2 numbers returned
How to input checkbox when used ng-table in Angular 2?
Coq: `Function … with` syntax
Why do they call them Thunks?
How can I assign an output to a function call in python?
Coq: Defining a function by pattern matching on the arity of its argument
Calling functions from other contracts solidity
How to write own List.map function in F#
Python3 Cannot concatenate error? How to fix?
Passing random type as parameter swift
Where is the mistake in this replacing function in Haskell?
TypeScript Mapped Type Key/Values
Haskell Instances required for definition
ERROR - Inferred type is not general enough
Avoid Subquery returned more than 1 value error in a table valued function
F# Finding lowest outcome of a function for all elements in a Sequence