简体   繁体   English

如何在Haskell中计算类型

[英]How is the calculation of types in Haskell

Lets say 让我们说

  flip :: (a->b->c) ->b->a->c
  const ::d->e->d

type of (flip const) would be (翻转const)的类型

  a=d,b=e,c=d

in

  b->a->c

so the type would be 所以类型会是

  e->d->d

But for (map take) its 但是(地图拍摄)它

  [Int]->[[a]]->[[a]]

so i didn't understand how the ghci this one calculated. 所以我不明白这个ghci是如何计算的。 i understood the [[a]]->[[a]] but why and how [Int] ? 我理解[[a]] - > [[a]]但是为什么以及如何[Int]?

edit: For example if we'd write in ghci 编辑:例如,如果我们写ghci

  :t flip const 


it would return b->c->c

and ghci would calculate that as i did. 和ghci会像我一样计算出来。

But

 map :: (a->b)->[a]->[b]
 take :: Int->[c]->[c]

so why is map take 那么为什么地图需要

  [Int]->[[a]->[a]]

why [Int] how did the ghci calculate that 为什么[Int] ghci是如何计算出来的

Let's do the same analysis: 我们做同样的分析:

map :: (a -> b) -> [a] -> [b]

And

take :: Int -> [x] -> [x]

But that actually means 但这实际上意味着

take :: Int -> ([x] -> [x])

So with a=Int and b=([x] -> [x]) you get 所以用a=Intb=([x] -> [x])得到

map take :: [Int] -> [ [x] -> [x] ]

A list of list functions! 列表函数列表!

You should copy and paste the types you see, not re-type them into the question. 您应该复制并粘贴您看到的类型,而不是将它们重新输入到问题中。 The reason is you saw wrong. 原因是你看错了。 The type for map take is: map take的类型是:

map take :: [Int] -> [[a] -> [a]]

In other words, the unification works as such: 换句话说,统一就是这样的:

:t map
map :: (a -> b) -> [a] -> [b]
:t take
take :: Int -> [c] -> [c]

so when applying take as the first argument to map you get a ~ Int and b ~ [c] -> [c] (notice that is a function). 所以当应用take作为map的第一个参数时,你得到a ~ Intb ~ [c] -> [c] (注意这是一个函数)。 Performing these replacements in the map type and applying the first argument: map类型中执行这些替换并应用第一个参数:

map take :: [a] -> [b]        (for some specific 'a' and 'b')
-- recall a ~ Int
map take :: [Int] -> [b]      (for some specific 'b')
-- recall b ~ [c] -> [c]
map take :: [Int] -> [[c] -> [c]]

Yay, map take is exactly what you expect. 是的, map take正是你所期望的。 A function that operates over lists of Ints and results in a list of functions that will take some number of elements from the start of a list. 一个在Ints列表上运行的函数,它产生一个函数列表,这些函数将从列表的开头获取一些元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM