[Haskell] CodeWars|Who likes it?
2017-10-04 16:44
253 查看
https://www.codewars.com/kata/5266876b8f4bf2da9b000362/haskell
实现函数
对于多于4个人名的情况,只需要改2 others里的数字即可(前面2个人名也要改。。)。
题目
你可能知道Facebook的点赞系统(404网站)和其他页面。人们可以给一个博文、图片或其他内容点赞。我们希望构造一个文本并展示给用户谁给这篇博文点赞。实现函数
likes :: [String] -> String,输入由人名构成的字符串数组。返回值例子如下:
likes [] = "no one likes this" likes ["Peter"] = "Peter likes this" likes ["Jacob", "Alex"] = "Jacob and Alex like this" likes ["Max", "John", "Mark"] = "Max, John and Mark like this" likes ["Alex", "Jacob", "Mark", "Max"] = "Alex, Jacob and 2 others like this"
对于多于4个人名的情况,只需要改2 others里的数字即可(前面2个人名也要改。。)。
题解
没啥好讲的,pattern matching挺好用的。module Likes where likes :: [String] -> String likes [] = "no one likes this" likes [x] = x ++ " likes this" likes [x, y] = x ++ " and " ++ y ++ " like this" likes [x, y, z] = x ++ ", " ++ y ++ " and " ++ z ++ " like this" likes (x:y:xs) = x ++ ", " ++ y ++ " and " ++ show (length xs) ++ " others like this"
相关文章推荐
- [简单题]Who_like_it? Python实现
- A powerful menu, everyone like use it !
- JedisConnectionException: java.net.SocketException: Socket closed;Unknown reply: ; It seems like server has closed the connectio
- 来自于德国的 Digg 网站 iliketotallyloveit 近日获收一笔投资
- I like it.
- centos 6.0 I like it~
- looks like it was orphaned (results never came back)nagios无法取得返回值
- git commit Please tell me who you are it config --global user.email "you@example.com"
- If only it could be all the same like we first me
- centos 7 The path "" is not a valid path to the 3.10.0-229.el7.x86_64 kernel headers. Would you like to change it?
- Storage rage: Like getting a nice steak and being told to only eat 80% of it
- [Haskell] CodeWars|Sum of odd numbers
- Is it possible to implement a Firebug-like “inspect element” DOM element highlighter with client-side JavaScript?
- Get who you are a GHD and like the unbelievable results
- It looks like git-am is in progress. Cannot rebase.
- [Haskell] CodeWars|Build a pile of Cubes
- It looks like git-am is in progress. Cannot rebase.
- It looks like git-am is in progress. Cannot rebase.
- 北京广播音乐电台,I like it!
- A small game to hit it like Zidane!