Quantcast
Browsing all 527 articles
Browse latest View live

Why a named function is still displayed as anonymous in utop?

(I am learning the OCaml and this may be a naive question.)I use below code to define a named function a:utop # let a = fun x -> x+1;;val a : int -> int = <fun>Note that the leading val a...

View Article


Using Str module in OCaml top level?

I tried two commands to load Str Module in OCaml top level.The first command gives me the error "Cannot find file Str.cmo".I then tried to use the file i was using in top level with the second command....

View Article


Make a function return itself after doing some work

let log x = print_int x; log ;;log 111 222;;I am expecting log to print 111 and return itself and then print 222 on the second call but it does not work as expected, I am getting an error message...

View Article

Why is the variable "hd" unbound here?

let f x = match x with ((2, 4)::xr) -> 42 | [(1, y); (_, 3); (_, 4)] -> 5 | [(x, _); (u, w)] -> u + x | [(44, 11); (12, 3)] -> 42 | (x::xr) -> fst (hd xr)I've tried running f[(2, 4)] for...

View Article

How could we use a value instead of a name/identifier in let expression?

I saw below code:let () = assert (f input1 = output1)The assert expression returns a value of type unit, which has only one possible value displayed as ().My understanding is, the let definition should...

View Article


How to make sense of "let () ="

In OCaml, how to make sense of a definition like:let () = print_endline "hello world"Let-definitions in ocaml should look like let x = e but above () is not a variable. So what happens there?I think it...

View Article

This expression has type unit but an expression was expected of type 'a...

I am trying to write a simple executable code in OCaml below.open Printfopen Lwtopen Cohttpopen Cohttp_lwt_unixopen Yojsonlet () = let ip = "8.8.8.8" in let key = "" in let uri = Uri.of_string...

View Article

Position of an element in a list

What is the fastest way to get the position of an element in a list of element in OCaml?I know how to get the "nth" position of an element in a list but I would like to know how to get the position of...

View Article


Combining two lists alternately

Write a function that combines the two lists provided. Items in the output list are to alternate. You must not use library functions (e.g. "length", "reverse", "append") of complexity greater than...

View Article


Filtering an array in OCaml

I have an array of integers where I'm treating 0 in a special way. I need to manipulate the array so that the output is an array of the same size with all the zeros after all the non-zero values. For...

View Article

Separate a list in OCaml

So I have a list that I'm trying to separate into two lists, such that all the even indices from the original list make up the first list, and the odd indices make up the second list. Here is my...

View Article

Why the number of function parameters doesn't match the function definition?

In below snippet, the recursive function lookup has only one parameter.How could it later be applied with two parameters k and t?let rec lookup k = function (* only 1 parameter k *)| [] -> None|...

View Article

How to count the number of consecutive occurrences in a list of any element...

In OCaml, suppose I have a string list as follows:let ls : string list = ["A"; "A"; "B"; "B"; "A"; "Y"; "Y"; "Y"] ;;I'm having trouble writing a function that calculates how many times an element...

View Article


Why two let expressions lead to compile error?

I have a let.ml containing below 2 lines:let x = 1 in x + 1let y = 1 in y + 1When compile them with ocamlc, it says below error:File "let.ml", line 2, characters 10-12:2 | let y = 1 in y + 1 ^^Error:...

View Article

Image may be NSFW.
Clik here to view.

Type info display mismatch in OCaml with zarith library

Why is the type info in the red box not: 'a -> 'a -> 'a -> 'a -> 'a?I think all the parameters and the return value should be of the same type, and utop does confirm that they are all of...

View Article


Insert function for a matrix in OCaml is not working

class virtual ['a] matrix_game m pf = object val matrix : 'a array array = m val rows = Array.length m val cols = Array.length m.(0) val print_function : 'a -> unit = pf method print = for i = (-1)...

View Article

What would be the simplest analogue in OCaml, of Python's enumerate function?

In Python enumerate works as follows:a_list = ['a', 'b', 'c']for i, x in enumerate(a_list): print(i, x)The output will be:0 a1 b2 cThus, enumerate actually returns a generator (pretty much a lazy...

View Article


Functional Breadth First Search

Functional depth first search is lovely in directed acyclic graphs.In graphs with cycles however, how do we avoid infinite recursion? In a procedural language I would mark nodes as I hit them, but...

View Article

Using the OCaml reverse application operator |> to construct a function...

I often find myself using the |> operator when constructing functions to pass as parameters to methods like List.find. For example, I have this code snippet:let parse_test_header (header : string...

View Article

ocaml begin/end pattern matching

Haven't been able to find much online documentation regarding begin/end in ocaml. I have two different pattern matches in the same function (which I want to be independent of each other), but vscode is...

View Article
Browsing all 527 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>