You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
--So as you can see, it doesn't match the original "map"'s type and the input argument (second last "a") gets "unfolded" into [a].
86
+
--It doesn't match the original "map"'s type very precisely and the input argument (second last "a") gets "unfolded" into [a].
74
87
--
75
88
-- 2. Another example I found is the one by https://github.com/RoccoMathijn: https://github.com/RoccoMathijn/programming-in-haskell/blob/master/chapter07.hs
76
89
--
77
90
-- map' :: (a -> b) -> [a] -> [b]
78
91
-- map' f = unfold (null) (f.head) (tail)
79
92
--
80
93
-- But this again is not intercangeable with the original "map" as b is simply [a], so the result is really [[a]].
94
+
-- Furthermore, I'm not sure how it even builds as "unfold" operates on (a -> a) and not (a -> b) as the function f. I'm using GHC 8.6.3 and getting the following:
95
+
--
96
+
-- $ ghci
97
+
-- GHCi, version 8.6.3: http://www.haskell.org/ghc/ :? for help
98
+
-- Prelude> :load Main
99
+
-- [1 of 1] Compiling Main ( Main.hs, interpreted )
100
+
101
+
-- Main.hs:95:26: error:
102
+
-- * Couldn't match type `b' with `[a]'
103
+
-- `b' is a rigid type variable bound by
104
+
-- the type signature for:
105
+
-- map'' :: forall a b. (a -> b) -> [a] -> [b]
106
+
-- at Main.hs:94:1-31
107
+
-- Expected type: [a] -> [a]
108
+
-- Actual type: [a] -> b
109
+
-- * In the second argument of `unfold', namely `(f . head)'
110
+
-- In the expression: unfold (null) (f . head) (tail)
111
+
-- In an equation for map'': map'' f = unfold (null) (f . head) (tail)
112
+
-- * Relevant bindings include
113
+
-- f :: a -> b (bound at Main.hs:95:7)
114
+
-- map'' :: (a -> b) -> [a] -> [b] (bound at Main.hs:95:1)
115
+
--|
116
+
-- 95 | map'' f = unfold (null) (f.head) (tail)
117
+
-- | ^^^^^^
118
+
-- Failed, no modules loaded.
81
119
--
82
-
-- After more time than I'd like to admit, I finally figured out the implementation which is interchangeable with the original "map".
120
+
-- After more time than I'd like to admit, I finally figured out the implementation which is almost interchangeable with the original "map".
83
121
-- Terribly inefficient, but I believe it answers the exercise's question precisely.
84
122
85
-
map':: (a->b) -> [a] -> [b]
123
+
map':: (a->a) -> [a] -> [a]
86
124
map' f =concat. unfold null (listify . f .head) (tail)
0 commit comments