[Haskell] CodeWars|Who likes it?
来源:互联网 发布:js post 请求 ajax 编辑:程序博客网 时间:2024/06/01 12:39
https://www.codewars.com/kata/5266876b8f4bf2da9b000362/haskell
题目
你可能知道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 wherelikes :: [String] -> Stringlikes [] = "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"
阅读全文
0 0
- [Haskell] CodeWars|Who likes it?
- [Haskell] CodeWars|Printer Errors
- [Haskell] CodeWars|Vowel Count
- [Haskell] CodeWars|Bouncing Balls
- [Haskell] CodeWars|Your order, please
- [Haskell] CodeWars|Sum of Digits
- [Haskell] CodeWars|Integers: Recreation One
- [Haskell] CodeWars|Growth of a Population
- [Haskell] CodeWars|Find the middle element
- [Haskell] CodeWars|Sum of odd numbers
- [Haskell] CodeWars|Build a pile of Cubes
- Who's fault is it?
- Haskell
- Haskell
- It's 2018: Who Owns the Cloud?
- who?
- who
- daily CodeWars
- Mysql初次启动修改root账户密码的方法(解决Access denied for user ''@'localhost' to database 'mysql的方法)
- 正则表达式-学习笔记02
- STL-set容器
- 【Openjudge, NOI, 枚举】1812完美立方
- C++ 接口函数的覆写
- [Haskell] CodeWars|Who likes it?
- 一起学Netty(五)之 初识ByteBuf和ByteBuf的常用API
- 移动架构23_设计模式六大原则一:单一职责原则
- 量子成像与量子雷达技术
- MySql的数据找回
- spring @component的作用
- [最大流] BZOJ 1458: 士兵占领 题解
- 如何杀死一个python的线程
- servlet3.0 part上传文件