How to make a list of partially used functions using a map?

I am trying to compile a list of functions (for use with zipwithlater), so I have one [take 1,take 2,take 3], so each list item must have a type [Int] -> [Int].

This is what I tried, which seems to be right. I'm not sure why I get an error message?

Prelude> map (\x -> take x) [1..5]

<interactive>:46:1:
    No instance for (Show ([a0] -> [a0]))
      (maybe you haven't applied enough arguments to a function?)
      arising from a use of ‘print’
    In a stmt of an interactive GHCi command: print it
+4
source share
2 answers

First of all, your code works. But functions cannot get shown. Try show idit and you will get a very similar error message.

, , , . , 1 :: Int. , \x -> take x - take, map take [1..5]. :

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

take map. map, a ~ Int - take b ~ [c] -> [c].

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

map take [1..5], :

map take        :: [Int] -> [[c] -> [c]]
[1..5]          :: [Int]
map take [1..5] ::          [[c] -> [c]]

. , . ? . .

, , [[c] -> [c]] . :

Prelude> map (\f -> f [1..10]) (map take [1..5])
[[1],[1,2],[1,2,3],[1,2,3,4],[1,2,3,4,5]]
+3

@kosmikus , , - , :t .

5 , , :

Prelude> :t [(take 1), (take 2), (take 3), (take 4), (take 5)]
[(take 1), (take 2), (take 3), (take 4), (take 5)] :: [[a] -> [a]]

map:

Prelude> :t map (\x -> take x) [1..5]
map (\x -> take x) [1..5] :: [[a] -> [a]]

, :

Prelude> :t map take [1..5]
map take [1..5] :: [[a] -> [a]]

, , - GHCI, . :t @kosmikus . , . :

Prelude> let takers = map take [1..5] in head(tail takers) [8,9,10,11]
[8,9]

take 2 [8 9 10 11].

+1

Source: https://habr.com/ru/post/1676895/


All Articles