简体   繁体   中英

Type declaration [[Integer] -> Integer]

I'm learning haskell and saw function compositions. Tried to composite map and foldl

mapd = (map.foldl)

Than

test = (mapd (\x y -> x + y ) [1,2,3,4])

Type of test is

test :: [[Integer] -> Integer]

So what is this type declaration means?

That means that your function "test" returns a list of functions. And that each of those functions takes a list of integers and returns an integer.

test 
= mapd (\x y -> x + y ) [1,2,3,4]
= mapd (+) [1,2,3,4]
= (map . foldl) (+) [1,2,3,4]
= map (foldl (+)) [1,2,3,4]
= [ foldl (+) 1
  , foldl (+) 2
  , foldl (+) 3
  , foldl (+) 4 ]

The result is a list of functions. The first function takes a list of integers, and sums it up starting from 1 . The second is similar, but starts from 2 . And so on for the remaining functions.

As fgv already stated, this is a list of functions from list of integers to integer, hence the [[Integer] -> Integer] type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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