<quarters>
I was wondering if someone can help clarify how this expression let double = map (( * ) 2) ;; yields the same as let double ls = (fun x -> 2 * x) ls ;; I understand how the latter works but am struggling with the former
TheRuralJuror has joined #ocaml
Haudegen has quit [Ping timeout: 268 seconds]
TheRuralJuror has quit [Remote host closed the connection]
<tautologico>
is the second one missing a map?
<quarters>
tautologico, oh yeah. thanks
<quarters>
good catch
<tautologico>
ok, so * is the multiplication function, right?
<tautologico>
( * ) turns the infix operator into a prefix function
<tautologico>
int -> int -> int
<tautologico>
it is a curried function with two arguments. ( * ) 2 is a function int -> int that is similar to ( * ) but with the first argument fixed to the value 2
<tautologico>
( * ) 2 is a function that, given an integer, will return the int multiplied by 2
<tautologico>
you can check that in the REPL: (( * ) 2) 5;;
<quarters>
tautologico, I think the prefix function portion is throwing me off due to unfamiliarity with that syntax. I'm used to seeing a variable being worked upon which I don't see here
<tautologico>
so map (( * ) 2) is also a function with the first argument fixed, in this case the function (( * ) 2) which will be mapped in to the list
<quarters>
are all functions capable of being prefix functions?
<tautologico>
map is ('a -> 'b) -> 'a list -> 'b list
<tautologico>
so fixing the first argument means that map (( * ) 2) is int list -> int list, the function that, given a list of integers, will return a list with the values doubled
<tautologico>
if you write let f x = x * 2 you'd call f with f 5, right? so f is in prefix position
<tautologico>
operators are different because they can be infixed
<tautologico>
are you familiar with currying? if you're not, I imagine it will be difficult to understand your first snippet, because it uses the fact that functions are curried twice
<quarters>
tautologico, I'm familiar with currying insofar as it involves a function returning another function and that ocaml's functions' "acceptance" of multiple arguments is syntactical sugar for what is basically currying
<tautologico>
right, that's it
<tautologico>
so * is an (infix) operator, ( * ) is the multiplication function, a 2-argument function.
<quarters>
I'm wondering how I'd be able to write a filter even numbers function using this style. I know how to write it like so: let evens = filter (fun x -> x mod 2 = 0)
<tautologico>
( * ) 2 is a function that accepts only one argument and returns the value of the argument doubled.
<tautologico>
in this case you're fixing the second argument to mod, so currying won't help
<quarters>
tautologico, I think I'll need a bit more time to process what you just told me. ty!
<tautologico>
np
<tautologico>
for simple, pure functions you can think about them using a substitution model
<tautologico>
so if you define let add a b = a + b, it is int -> int -> int, a function of two arguments
<tautologico>
(sum 2), by substitution, would be the same as let sum b = 2 + b, a function with one argument, returning an int (int -> int)
<tautologico>
curried functions are always single-argument, they may return other functions that accept other arguments
mfp has quit [Ping timeout: 250 seconds]
tormen_ has joined #ocaml
tormen has quit [Ping timeout: 250 seconds]
kotrcka has left #ocaml [#ocaml]
nopf has joined #ocaml
FreeBirdLjj has joined #ocaml
mengu has joined #ocaml
mengu has quit [Ping timeout: 246 seconds]
gravicappa has joined #ocaml
notnotdan has quit [Remote host closed the connection]
mengu has joined #ocaml
mengu has quit [Quit: Leaving...]
_whitelogger has joined #ocaml
recj has quit [Ping timeout: 246 seconds]
FreeBirdLjj has quit [Ping timeout: 246 seconds]
jao has joined #ocaml
_whitelogger has joined #ocaml
kakadu has joined #ocaml
jao has quit [Ping timeout: 246 seconds]
haesbaert has joined #ocaml
_whitelogger has joined #ocaml
_whitelogger has joined #ocaml
maattdd has joined #ocaml
emily has joined #ocaml
emily has quit [Client Quit]
maattdd has quit [Quit: WeeChat 2.2]
pierpal has quit [Read error: Connection reset by peer]
pierpal has joined #ocaml
pierpal has quit [Read error: Connection reset by peer]
pierpal has joined #ocaml
pierpal has quit [Read error: Connection reset by peer]
pierpal has joined #ocaml
pierpal has quit [Read error: Connection reset by peer]
pierpal has joined #ocaml
pierpal has quit [Ping timeout: 268 seconds]
initiumdoeslinux has joined #ocaml
pierpal has joined #ocaml
pierpal has quit [Ping timeout: 246 seconds]
initiumdoeslinux has quit [Remote host closed the connection]
pepesza has quit [Ping timeout: 250 seconds]
pierpal has joined #ocaml
pepesza has joined #ocaml
mfp has joined #ocaml
bartholin has joined #ocaml
estrom has joined #ocaml
<estrom>
Hello, I am new to OCaml and was wondering if there is an opinionated way of rewriting lines 11 to 17 in this code: https://pastecode.xyz/view/7b747026
pepesza has quit [Ping timeout: 244 seconds]
pepesza has joined #ocaml
<lyxia>
that looks pretty good to me
jnavila has joined #ocaml
<lyxia>
you could fold this into a single match with: match line |> Option.map float_from_string with
<lyxia>
it makes the code look a bit more regular but it's not really easier to understand IMO.