What does this function signature mean in sml?

后端 未结 2 1691
囚心锁ツ
囚心锁ツ 2021-01-06 23:01

I\'m looking through some notes that my professor gave regarding the language SML and one of the functions looks like this:

fun max gt = 
    let fun lp curr         


        
2条回答
  •  盖世英雄少女心
    2021-01-06 23:49

    Just to clarify a bit on currying, from Faiz's excellent answer.

    As previously stated SML only allows functions to take 1 argument. The reason for this is because a function fun foo x = x is actually a derived form of (syntactic sugar) val rec foo = fn x => x. Well actually this is not entirely true, but lets keep it simple for a second

    Now take for example this power function. Here we declare the function to "take two arguments"

    fun pow n 0 = 1 
      | pow n k = n * pow n (k-1)
    

    As stated above, fun ... was a derived form and thus the equivalent form of the power function is

    val rec pow = fn n => fn k => ...
    

    As you might see here, we have a problem expressing the two different pattern matches of the original function declaration, and thus we can't keep it "simple" anymore and the real equivalent form of a fun declaration is

    val rec pow = fn n => fn k =>
        case (n, k) of 
          (n, 0) => 1
        | (n, k) => n * pow n (k-1)
    

    For the sake of completeness, cases is actually also a derived form, with anonymous functions as the equivalent form

    val rec pow = fn n => fn k => 
        (fn (n,0) => 1
          | (n,k) => n * pow n (k-1)) (n,k)
    

    Note that (n,k) is applied directly to the inner most anonymous function.

提交回复
热议问题