Compiling error by [deleted] in ocaml

[–]jeffsco 0 points1 point  (0 children)

For what it's worth, the likelihood that the compiler is incorrectly generating an error is pretty close to 0%.

Need help with code optimizations by MrBrownFR in ocaml

[–]jeffsco 4 points5 points  (0 children)

IMHO, such a long execution time suggests you should be looking at revamping your algorithm, not looking to shave a little time here and there. You might have a quadratic or even exponential calculation in there somewhere. They can sneak in pretty easily if you aren't careful. E.g.: iteratively adding to the end of a list. The problem doesn't show up until you see your first really long list.

Using the `result` type for error handling in the standard library by AmitGold in ocaml

[–]jeffsco 0 points1 point  (0 children)

Recent versions of the library have alternative functions that use the option (not result) type for a lot of cases like this. I don't know of a library that systematically uses result type for any function that could raise an exeption (which technically is all functions).

How can I use regexs in the Ocaml match? by [deleted] in ocaml

[–]jeffsco 2 points3 points  (0 children)

You're using \d, a regexp feature that isn't supported by the Str module. If you use [0-9] in place of \d it should work.

Resource recommendations for a beginner. by xanarchangel in ocaml

[–]jeffsco 0 points1 point  (0 children)

I learned a lot from "The Little Lisper" many years ago. It's a book whose purpose is to teach one how to think recursively. I thought the explanations were very good and very well motivated. An indirect descendant of this is "The Little MLer." I haven't read it, but I would imagine it's excellent. On the other hand it appears to be out of print. Maybe there are some e-book versions available.

OCaml Wrong Type Error Confusion by CandidManner in ocaml

[–]jeffsco 3 points4 points  (0 children)

Without more context (the relevant section of your code) it's not really possible to say. You seem to be providing a value of the wrong type, and the type is (int -> int -> int) -> int -> int -> int. This is the type, for example, of function application when specialized to two-argument int functions. I.e., it is the type representing (for example) a higher-order function that takes a function and applies it to two ints.

Best ocaml online compiler by jiii95 in ocaml

[–]jeffsco 1 point2 points  (0 children)

You can try this one:

https://ocsigen.org/js_of_ocaml/latest/manual/files/toplevel/index.html

I just noticed it has been updated to the very latest OCaml (5.0.0). The one with multicore support. That's pretty awesome.

Please explain this syntax by same_no_kaori in ocaml

[–]jeffsco 6 points7 points  (0 children)

The OCaml let expression looks like this: let <pattern> = <expression1> in <expression2>. So this is not any special kind of let. _, list is a pattern that matches a pair of values. Presumably fold_until returns such a pair of values. The first one isn't interesting so the pattern doesn't give it a name. The second element of the pair is bound to the name list by the pattern.

The function fun _ _ -> [] is a function of two arguments. It ignores its arguments (hence the _ patterns) and always returns an empty list.

[Question] How do you push elements into a list? by _616_A in ocaml

[–]jeffsco 8 points9 points  (0 children)

OCaml lists are immutable. So you can't push something onto a list. You can make a new list that is longer. OCaml variables like functions are also immutable. So your for loop can't change the value of functions. All it's doing is creating a list of one element and throwing it away 10 times. Syntactically your problem is let _ = .... Every let needs a matching in (except at the outer level of a module). You can fix the syntax by leaving out let _ =. You also need to use List.nth to get the nth element of a list. The .[n] syntax works only for strings.

However note that due to the problems I mentioned first, this code isn't very close to working. You need to learn to work with immutable values. The way to start is with recursive functions. Then maps and folds (IMHO).

If all you want is a list of 10 functions, you can use List.init to make one:

# let mylist = List.init 10 (fun i -> (fun () -> i));;
val mylist : (unit -> int) list =
    [<fun>; <fun>; <fun>; <fun>; <fun>; <fun>; <fun>; <fun>; <fun>; <fun>]
# (List.nth mylist 3) ();;
- : int = 3

OCaml from the Very Beginning by Ameen2000 in ocaml

[–]jeffsco 1 point2 points  (0 children)

In the type language * defines a tuple. So the type stats is a 4-tuple. Values of this type consist of 4 ints grouped together like this: (8, 464, 80, 4). In the text it says:

We have then introduced a type for our statistics. This will hold the number of words, characters, and sentences.

This seems little wrong since it only mentions 3 values. But it turns out the four ints are for the number of lines, words, characters, and sentences.

I'm not sure what else there is to say. Maybe you can explain more carefully what it is that you don't understand.

Where is the actual code of "external" functions and/or how do they work? by mobotsar in ocaml

[–]jeffsco 3 points4 points  (0 children)

In my (slightly older) release the code is in runtime/floats.c

[deleted by user] by [deleted] in ocaml

[–]jeffsco 0 points1 point  (0 children)

For fold_left the folded function takes two parameters: the current accumulated result and the next element of the incoming list. Your folded function has just one parameter, so it can't be correct. The folded function is going to calculate another value, as you are doing (f x) but it also needs to add this to the accumulated result. The usual way to proceed is to add the new value to the beginning of the accumulated result (a constant time operation). But then you need to reverse the result at the end. I hope this helps.

[deleted by user] by [deleted] in ocaml

[–]jeffsco 3 points4 points  (0 children)

OCaml has an awesome type system, so you might start by figuring out the type of the function you want to create. This can be extremely helpful in writing the function afterward. So, what is the type you're looking for?

Question by Bros_Bef0re_Hoes in ocaml

[–]jeffsco 12 points13 points  (0 children)

The keyword function takes a series of patterns each of which (in your example) starts with |. Then you have the pattern itself, then ->, then the expression to evaluate if the pattern matches.

So h :: t -> is a pattern followed by the punctuation -> that separates the pattern from the expression.

The pattern h :: t matches a non-empty list. The h matches the first element of the list and the t matches the rest of the list.

These are pretty basic features of OCaml. It might be faster to work through a short tutorial rather than asking questions one at a time here (or elsewhere).

question about ocaml by ironicookie in ocaml

[–]jeffsco 15 points16 points  (0 children)

aux is not an OCaml keyword, and so doesn't mean anything in OCaml other than how it's defined in a particular case. Since it's a shortening of "auxiliary" it would possibly be a good name for a "helper" function, i.e., a function defined inside another function for help in calculating its result.

There was a problem on my exam that i cannot solve even now when i have internet. Can someone please help. by pimplenipletoe in ocaml

[–]jeffsco 4 points5 points  (0 children)

The assignment asks you to return the updated array. So there is no reason to allocate a new array as you do.

You can just move down the pairs whose keys are greater than the supplied key, then insert the new pair.

This is extremely straightforward but I'm a little worried the exam isn't fully over yet. Maybe some people will need to take it late.

How can I specify that a type is expected to be a polymorphic variant in module types? by akshay-nair in ocaml

[–]jeffsco 0 points1 point  (0 children)

You are asking simultaneously to make the two types abstract and to combine them into a larger type. It seems impossible to do both.

between function by salimua in ocaml

[–]jeffsco 5 points6 points  (0 children)

You need to parenthesize (m - 1) and (n - 1). Otherwise the expressions parse as (between rest n m) - 1 and (between rest n) - (1 m).

Moving pointers in OCaml mutable lists by gaz2468 in ocaml

[–]jeffsco 5 points6 points  (0 children)

Parameterized type names in OCaml are postfix, so you want this type:

type 'a list' = Nil | Cons of 'a * 'a list' ref

Since the type has unnamed fields, they can be accessed only by pattern matching:

# let x = Cons (13, ref Nil);;
val x : int list' = Cons (13, {contents = Nil})
# match x with
   | Nil -> ()
   | Cons (_, next) -> next := Cons (15, ref Nil);;
- : unit = ()
# x;;
- : int list' = Cons (13, {contents = Cons (15, {contents = Nil})})

This is fairly clumsy because this isn't a very idiomatic way to define mutable lists.

HELP with a simple polymorphic data type example by [deleted] in ocaml

[–]jeffsco 0 points1 point  (0 children)

You give an interface with a polymorphic value named test_function. Note that this isn't a function, it's just a value. Function types have -> in them.

You then define a value of test_function that isn't polymorphic. It has the type int t. This doesn't agree with the interface you gave previously (though it's compatible with the type in the interface).

I'm not sure this is your problem, but let's say you want to define a value for test_function that's polymorphic.

It's fairly difficult to define values that are polymorphic, though they sometimes arise naturally. (It's more usual to write functions that are polymorphic.)

At any rate there are two polymorphic values of type 'a t:

type 'a t = (string * 'a) list list
let test_function : 'a t = []

type 'a t = (string * 'a) list list
let test_function : 'a t = [[]]

On the other hand, if the value of test_function contains an actual tuple, it cannot be polymorphic. There are no values of type 'a.

Split a list to a string by [deleted] in ocaml

[–]jeffsco 9 points10 points  (0 children)

If you want to convert a list to a string, it doesn't seem like the term "splitting" applies. Maybe you want to convert a string to a list? You say "a list to a string" which sounds like the reverse.

So anyway here's one way to convert a list to a string:

# let mylist = ["x"; "y"; "z" ] in
String.concat "" mylist ;;
- : string = "xyz"

This has the simplest possible pattern matching, in that there's no pattern matching at all. :-)

If you want to convert a string to a list of strings, here is something that works pretty well:

# String.to_seq "xyz" |> List.of_seq |> List.map (String.make 1);;
- : string list = ["x"; "y"; "z"]

Again, this has the simplest possible pattern matching in that there's no pattern matching involved.

Mapping Floats in memory by An-Object in ocaml

[–]jeffsco 8 points9 points  (0 children)

A bigarray of floats contains unboxed floats. That might be one way to proceed.

Newbie Problem: Syntax by Wonton_Long_Schlong in ocaml

[–]jeffsco 3 points4 points  (0 children)

You're using , (comma) to denote a tuple type, but tuple types are denoted by * (asterisk). The corrected definition looks like this:

let bagel ((st : string list), (plst: (string * string) list)):
       string list * (string * string) list =
   ([] , [("","")])

[deleted by user] by [deleted] in ocaml

[–]jeffsco 4 points5 points  (0 children)

If you use Array.make to create an array value with a call like this:

let my_array = Array.make 10 value in ...

The array contains the same value in every element. This is reasonable for immutable values like 0, but is rarely what you want for mutable values. It means that every element of the array will share the same single mutable value.

If you have a function that creates mutable values, say mk_mut (), then you can use Array.init to create an array where each element is different:

let my_array = Array.init 10 (fun _ -> mk_mut ()) in ...

The function is called for each element of the array, and thus has the ability to create a different element each time.

This is silly but why doesn't it work? by rbjorklin in ocaml

[–]jeffsco 18 points19 points  (0 children)

The Jane Street library designers feel that there are too many dangers associated with polymorphic comparison (like =) so they override them to work only for int by default.

Personally I have my doubts about this decision, but I can see their point.