Sml understanding of functions

I am trying to understand the difference between the two:

val my_fun = length o List.filter (fn (item) => item = #"a") o String.explode 

This can be called (my_fun "name" will return 1) and will work fine. I am trying to understand why the following does not work.

length o (List.filter (fn (item) => item = #"a" ) (String.explode "name"))

Defining the composition of a function in sml

f o g = f(g(x))

In the second form we do (I think)

length ([#"a"]) 
+4
source share
1 answer

You seem to mix function composition with function application.

, , f g, - , , g , f . o , , -

fun compose (f,g) x = f(g(x))

fn : ('a -> 'b) * ('c -> 'a) -> 'c -> 'b ( , (op o); REPL). , compose 'c -> 'b, .

length o List.filter (fn (item) => item = #"a") o String.explode 

, , -.

, ,

length o (List.filter (fn (item) => item = #"a" ) (String.explode "name"))

length o [#"a"]

. . . length , .

,

length (List.filter(fn (item) = > item = # "a" ) (String.explode "name" ))

length [#"a"] 1.

apply, :

def apply f x = f x

compose, : fn : ('a -> 'b) -> 'a -> 'b. , .

+4

Source: https://habr.com/ru/post/1668335/


All Articles