I was just starting to learn Haskell, and I ran into the following problem. I am trying to iterate over a function \x->[x]
. I expect to get a result [[8]]
on
foldr1 (.) (replicate 2 (\x->[x])) $ (8 :: Int)
This does not work and gives the following error message:
A check occurs: it is impossible to build an infinite type: a ~ [a]
Expected Type: [a -> a]
Actual Type: [a -> [a]]
I understand why this is not working. This is because it foldr1
has a type signature foldr1 :: Foldable t => (a -> a -> a) -> a -> t a -> a
and takes a -> a -> a
as the signature of the type of its first parameter, and nota -> a -> b
This is also for the same reason:
((!! 2) $ iterate (\x->[x]) .) id) (8 :: Int)
However, this works:
(\x->[x]) $ (\x->[x]) $ (8 :: Int)
and I understand that the first (\x->[x])
and second are of different types (ie, [Int]->[[Int]]
and Int->[Int]
), though formally they look the same.
, 2 , 100.
, ? , Template Haskell? , ?
node, read
it. , , read
. ,
read "[[[[[8]]]]]" :: ??
, ??
, . , , - .
, "" . :
natSucc x = [Left x,Right [x]]
succ
, . , foldr1-replicate
!!-iterate
.
. .
Edit:
3 , ( ), , , . (, Just Just Just 8
, ?).