how to read haskell type signature?

故事扮演 提交于 2019-12-25 12:42:18

问题


So I put this statement on my ghci

jkl x f y = f (map (+y) x)

And I got this out put back.

jkl :: Num b => [b] -> ([b] -> t) -> b -> t

But I'm confused when I read it. From my understanding jkl is type num that takes [b], [b] and t, and b. in the end it will output t. is that the right way to read it?


回答1:


jkl :: Num b => [b] -> ([b] -> t) -> b -> t

Whoever calls jkl has to

  • choose types b and t
  • guarantee that b was chosen among numeric types (the Num b constraint)
  • pass a [b] (list of b) as first argument
  • pass a function [b] -> t as second argument (i.e. taking [b] and returning a t)
  • pass a b as third argument
  • receive t back as a final result



回答2:


Whatever is between the double colon :: and the fat arrow => is called constraints. In this case, you have one constraint: Num b. This constraint demands that, whatever type b turns out to be, it must be an instance of type class Num.

After the fat arrow, you get types of function parameters, and at the very end type of its result.

Parameters:

  1. [b] - a list of values of type b, whatever that is.
  2. [b] -> t - a function that takes a list of b and produces a single t.
  3. b - a single value of type b.

And finally, function result is t.



来源:https://stackoverflow.com/questions/47803288/how-to-read-haskell-type-signature

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!