Cannot create apply function with static language?

前端 未结 12 1019
梦谈多话
梦谈多话 2021-02-04 02:09

I have read that with a statically typed language like Scala or Haskell there is no way to create or provide a Lisp apply function:

(apply #\'+ (lis         


        
12条回答
  •  Happy的楠姐
    2021-02-04 02:54

    The benefit of a static language is that it would prevent you to apply a function to the arguments of incorrect types, so I think it's natural that it would be harder to do.

    Given a list of arguments and a function, in Scala, a tuple would best capture the data since it can store values of different types. With that in mind tupled has some resemblance to apply:

    scala> val args = (1, "a")
    args: (Int, java.lang.String) = (1,a)
    
    scala> val f = (i:Int, s:String) => s + i
    f: (Int, String) => java.lang.String = 
    
    scala> f.tupled(args)
    res0: java.lang.String = a1
    

    For function of one argument, there is actually apply:

    scala> val g = (i:Int) => i + 1
    g: (Int) => Int = 
    
    scala> g.apply(2)
    res11: Int = 3
    

    I think if you think as apply as the mechanism to apply a first class function to its arguments, then the concept is there in Scala. But I suspect that apply in lisp is more powerful.

提交回复
热议问题