<Anarchos>
lst is a tuple because you match it with (_,[]) whose type is 'a * b list
<Anarchos>
so it is a tuple
<guanohhh>
Anarchos: sorry, I totally didn't understand the code I was writing, and now I do
<Anarchos>
guanohhh you can understand type better in annotating your code : for each expression you are not sure of the type, write instead (e: guessed_type) where e is the expression you want to verify the type, and guessed_type is a valid type expression as written by the toplevel
<Anarchos>
example : let f a b = (a:int) + (b:int)
<guanohhh>
yeah, that's for the explicit signature definition, right? I was trying to find examples on google but I wasn't finding any obvious answers about how it works in general
<Anarchos>
let (f: int -> int -> int) a b = a+b
<guanohhh>
Anarchos: thanks!
<Anarchos>
sorry, i mistyped again : let (f: int -> int -> int) = fun a b -> a+b;;
* Anarchos
just tried in toplevel)
Tuplanolla has quit [Quit: Leaving.]
wonko7 has quit [Ping timeout: 272 seconds]
<d_bot>
<EduardoRFS> someone knows an x86_64 in memory assembler for OCaml? I don't want to call `as`
<d_bot>
<EduardoRFS> it's for a latency sensitive application
Jesin has quit [Quit: Leaving]
Jesin has joined #ocaml
_whitelogger has joined #ocaml
Anarchos has quit [Excess Flood]
Jeanne-Kamikaze has quit [Quit: Leaving]
Jeanne-Kamikaze has joined #ocaml
mxns has joined #ocaml
amiloradovsky has quit [Remote host closed the connection]
ygrek has quit [Remote host closed the connection]
Haudegen has quit [Ping timeout: 240 seconds]
nullcone has joined #ocaml
vicfred has quit [Quit: Leaving]
Jeanne-Kamikaze has quit [Quit: Leaving]
waleee-cl has quit [Quit: Connection closed for inactivity]
<guanohhh>
also I don't understand how to access functions that are declared `let square = ...`, how am I supposed to access the arguments passed to it?
<Jeanne-Kamikaze>
You mean like let square = fun x -> x*x?
<Jeanne-Kamikaze>
What's wrong with the paste?
<d_bot>
<Deadrat> @guanohhh
<d_bot>
<Deadrat> the code you linked is essentially
<d_bot>
<Deadrat> ```ocaml
<d_bot>
<Deadrat> let rec less_tr_3 e lst to_return =
<d_bot>
<Deadrat> match lst with
<d_bot>
<Deadrat> | [] -> List.rev(to_return)
<d_bot>
<Deadrat> | x::y -> if x < e then less_tr_3 e y x::to_return
<d_bot>
<Deadrat> else less_tr_3 e y to_return
<d_bot>
<Deadrat> ;;
<d_bot>
<Deadrat> let less_tr e lst =
<d_bot>
<Deadrat> less_tr_3 e lst []
<d_bot>
<Deadrat> ```
<d_bot>
<Deadrat> you just create a internal recursive function and delegate to it when you call less_tr
<guanohhh>
yeah, because I need to make it tail recursive, which means adding an accumulator, but keeping the original function signature
<d_bot>
<Deadrat> except that its not tail recursive
<guanohhh>
that's the error I am getting
<guanohhh>
what do you mean it isn't tail recursive?
<Jeanne-Kamikaze>
Parenthesis around ::?
<d_bot>
<Deadrat> first variant in if clause isn't recursive
<guanohhh>
Jeanne-Kamikaze: you are right! thank you!
<Jeanne-Kamikaze>
You might as well do: x::y -> less_tr_3 e y (if x < e then x::to_return else to_return)
defdom0 has quit [Quit: defdom0]
<guanohhh>
Jeanne-Kamikaze: thank you!
defdom0 has joined #ocaml
defdom0 has quit [Client Quit]
<guanohhh>
wait, that returns a function, it doesn't return the result...
<Jeanne-Kamikaze>
And it is tail recursive? There's nothing going on after the recursive call.
<guanohhh>
what is the difference in ocaml between a function is `let funct a b = ...` and `let funct(a,b) = ...`?
<Jeanne-Kamikaze>
The first one "takes two arguments" (inasmuch as curried functions take more than one argument). The latter takes a single argument that is a 2-tuple.
<Jeanne-Kamikaze>
If you type them in utop you'll see the function signatures and see the difference.
<guanohhh>
oh, right, I remember reading how `let funct a b = ...` is equivalent to `let funct a = let funct b ...`, right?
<guanohhh>
thank you
<guanohhh>
but it is tail recursive right?
<Jeanne-Kamikaze>
Yes, it is.
<Jeanne-Kamikaze>
Yes. let add a b = a + b is semantically equivalent to let add = fun a -> fun b -> a + b
<guanohhh>
Jeanne-Kamikaze: thank you!
<guanohhh>
I would think it would be easy to switch between them
<guanohhh>
but I am supposed to flatten a list of lists without using recursion or helper functions besides map, fold left/right, so I don't know how to do that. kind of hit a brick wall.
DanC has joined #ocaml
<octachron>
Then you have to replace your flatten function with either a map or a fold. Do you think that flatten is a fold or a map?
<guanohhh>
I think of map as changing the identity of a list, and fold as turning a list into a value
<guanohhh>
so I guess a fold, since I want to extract item by item?
mxns has joined #ocaml
amiloradovsky has joined #ocaml
Anarchos has joined #ocaml
<octachron>
Yes, a fold is the right option(note that this means reimplementing a basic solution from the List module)
<octachron>
Another way to see that is that map does not change the number of elements of the list. Your function (you should really use a better name that flatten) certainly does.
mxns has quit [Ping timeout: 258 seconds]
narimiran has quit [Ping timeout: 256 seconds]
Jesin has quit [Quit: Leaving]
Jesin has joined #ocaml
ggole has quit [Quit: Leaving]
<guanohhh>
@octachron I am really stumped, I cannot figure out how to do it.
mxns has joined #ocaml
<octachron>
First, what is the result of `flatten [1;2;3] [4;5;6]`?
hnOsmium0001 has joined #ocaml
<guanohhh>
[1;2;3;4;5;6]
<guanohhh>
wait sorry
<guanohhh>
wait I was right, [1;2;3;4;5;6]
<octachron>
So flatten is better named append. Then the question is how to implement append with a fold (if `@` is not allowed).
amiloradovsky has quit [Remote host closed the connection]
amiloradovsky has joined #ocaml
Yagotzirck has joined #ocaml
<guanohhh>
right, which I am stuck on.
<guanohhh>
thank you so much for helping me by the way.
<guanohhh>
okay I got it
<guanohhh>
List.fold_right (fun x acc -> x::acc) [1;2;3] [4;5;6];;
<octachron>
Indeed.
amiloradovsky has quit [Remote host closed the connection]
<guanohhh>
okay I have another problem. I am supposed to basically assign [false;true;false;true...] to a list, and then get the last item
<guanohhh>
basically there is a 'parity' value that changes the true/false value of the next assignment, and the only one that matters is the last one
amiloradovsky has joined #ocaml
<guanohhh>
I cannot use length to help
<Anarchos>
guanohhh use List.rev and the only that matters is the first one...
<mrvn>
List.fold_right (fun x acc -> x)
<mrvn>
or fold_left for the other end
<guanohhh>
mrvn: thanks!
<guanohhh>
I just got: `List.fold_left (fun a b -> not a) true [1;2];;`
<mrvn>
or just write out the recursion
<guanohhh>
cannot use recursion or if
<mrvn>
then just fold and ignore the acc. That gives you the last x
<guanohhh>
mvrn: I have no idea how I could map your code to this problem, I am not that good with ocaml
<mrvn>
let return_last default list = List.fold_left (fun acc x -> x) default list
wreed has joined #ocaml
DanC has quit [Ping timeout: 260 seconds]
objmagic_ has joined #ocaml
objmagic has quit [Ping timeout: 264 seconds]
GuerrillaMonkey has joined #ocaml
<guanohhh>
how do you get the index of the first variable that make a function return true, when applied to a list?
Jeanne-Kamikaze has quit [Ping timeout: 265 seconds]
<guanohhh>
okay I figured it out, used some old code
<guanohhh>
what is the best way to write code so it isn't a bunch of `let f x y = ... in let z x y = ... in let a x y = ... in etc.`. I like the reasonml brackets, I am not used to ocaml's version.
GuerrillaMonkey has quit [Ping timeout: 256 seconds]
DanC has joined #ocaml
jnavila has quit [Ping timeout: 272 seconds]
nullcone has joined #ocaml
DanC has quit [Quit: ZNC 1.6.6+deb1ubuntu0.2 - http://znc.in]
TheLemonMan has quit [Quit: "It's now safe to turn off your computer."]
mxns has quit [Ping timeout: 260 seconds]
DanC has joined #ocaml
<Anarchos>
guanohhh let f,g,h = fun1, fun2,fun3 ?
<guanohhh>
Anarchos: thanks!
tane has quit [Quit: Leaving]
<guanohhh>
why does `index != -1` throw an error for me? it's not a type error, it's a 'integer subtraction. left associative ...` error
<Anarchos>
guanohhh let f x = x != -1;; val f : int -> bool = <fun>
<Anarchos>
guanohhh it works here
<guanohhh>
huh vscode with the ocaml-lsp plugin is throwing an error
<Anarchos>
(-1) should do the trick
<Anarchos>
the vscode parser is not as efficient as the ocaml one maybe
<guanohhh>
yeah that doesn't work either
wonko7 has joined #ocaml
<guanohhh>
also what does `let () = ht.arr.(i) <- x in ...` mean? I don't understand why you have to do `let ()` or the `in` part. It is to run a side effect, right?
DanC has quit [Quit: ZNC 1.6.6+deb1ubuntu0.2 - http://znc.in]
<Anarchos>
right
<Anarchos>
the side effect being an assignment in your case
DanC has joined #ocaml
mxns has joined #ocaml
<mrvn>
guanohhh: it's equivalent to "ht.arr.(i) <- x;"
Tuplanolla has quit [Quit: Leaving.]
<Anarchos>
what is the best way to find the index in a list of the first item verifying a given predicate ?
<mrvn>
none. fidning in a list is never the best.
<Anarchos>
mrvn i know, but for now i am only prototyping
<mrvn>
sounds more like homework.
<Anarchos>
no it isnet
<Anarchos>
mrvn you can look at my repo at github.com/Sylvain78/Preuves
<mrvn>
then just recurse
<Johann>
Anarchos: containers has a CCList.find_idx which does just that
<Anarchos>
i use find_map with a ref on the index that i increment in the predicate. not clean but should work
<Anarchos>
Johann anyway i would prefer a clean way
<mrvn>
let rec find_idx ~i=0 pred = function [] -> raise Not_found | x::_ when pred x -> i | _::xs -> find_idx ~i:(i+1) pred xs
<mrvn>
Anarchos: find_map? that's the worst.
<Anarchos>
mrvn why ?
<mrvn>
why do you build a new list? You never use it.
<mrvn>
you also don't terminate when you find the index. So you call the predicate a lot more than needed
<Anarchos>
mrvn List.find_map doesn't build a new list, it returns an option
<mrvn>
Oh? got confused by the "map" part. That usualy when you change one list into another.
<Anarchos>
mrvn not the find_map :)
<mrvn>
Ah, it maps just the found element.
<Anarchos>
yes
<mrvn>
ok, I take it all back then. That's just bad because of the reference.
<mrvn>
If ocaml where constistent in its standard library there should be a List.find_map_i