Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

P137 <* vs const的区别 #30

Open
boboyada opened this issue Jan 6, 2017 · 6 comments
Open

P137 <* vs const的区别 #30

boboyada opened this issue Jan 6, 2017 · 6 comments

Comments

@boboyada
Copy link

boboyada commented Jan 6, 2017

P137页第二段:
需要注意的是,很多新手会误认为(< * ) :: f a -> f b -> f a 和const函数一样,就直接忽略第二个参数。。。。。。。。。。。。。
Nothing <* Just 3
--Nothing

Just <* Nothing
--Nothing

[1..2] <* [1..10]
--[1111111111,2222222222]
能推导这个过程吗?为什么?

@winterland1989
Copy link
Owner

可以呀,我们拿Maybe为例:

-- (<*)的默认实现
(<*) :: f a -> f b -> f a
(<*) = liftA2 const   -- \ x y -> const <$> x <*> y 

-- 对于Maybe来说
f <$> (Just x) = Just f x
f <$> Nothing = Nothing
(Just f) <*> (Just x) = Just f x
_ <*> _ = Nothing

Nothing <* Just 3   -- const <$> Nothing <*> Just 3  -- Nothing
Just 3 <* Nothing   -- const <$> Just 3 <*> Nothing  -- Just (const 3) <*> Nothing -- Nothing

[]的类似,都是把定义代入,读者自己拿出纸笔划划就会发现很显而易见😄

@boboyada
Copy link
Author

boboyada commented Jan 6, 2017

很奇妙!清楚了一点。
[1..2] <* [1..10]
--[1111111111,2222222222]
但 []还是请你也一并推导一遍

@boboyada
Copy link
Author

boboyada commented Jan 6, 2017

[],Maybe作为Functor,Applicative,Monad,的实例,应该也有自己的实现。源代码怎么看?

@winterland1989
Copy link
Owner

你先去hackage上查看base的文档(里面有源码的链接),自己推试试,不行我再来给你推个。

@fwt11
Copy link

fwt11 commented Jun 21, 2020

136页的定义
(<*) = flip (*>)
是不是不太对?
按这个定义137页的
[1,2] <* [1..10]
结果是
[1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2]

@winterland1989
Copy link
Owner

是的,确实不是简单的flip,effect的展开是有顺序的,感谢反馈。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants