Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type declaration [[Integer] -> Integer]

Tags:

types

haskell

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?

like image 275
nazarevic Avatar asked Dec 03 '22 19:12

nazarevic


2 Answers

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.

like image 54
fgv Avatar answered Dec 18 '22 15:12

fgv


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.

like image 20
chi Avatar answered Dec 18 '22 16:12

chi