haskell - 字符串加倍?
问题描述
所以对于我的任务,我必须创建几个函数(Haskell 的新函数)。我可以很容易地做到以下几点
data Shape
= Circle Double -- radius
| Square Double -- length
| Rectangle Double Double --length and width
deriving (Show)
showShape :: Shape -> String
showShape (Circle r) = "circle of radius " ++ show r
showShape (Square l) = "square of length " ++ show l
showShape (Rectangle l w) = "rectangle of length " ++ show l ++ " and width " ++ show w
area :: Shape -> String
area (Circle r) =
"Circle of radius " ++ show r ++ " has an area of " ++ show (pi * r * r)
除了我的任务特别希望我使用
area :: Shape -> Double
我该怎么做?
解决方案
如果你这样做:
data Shape
= Circle Double -- radius
| Square Double -- length
| Rectangle Double Double --length and width
deriving (Show)
showShape :: Shape -> String
showShape (Circle r) = "circle of radius " ++ show r
showShape (Square l) = "square of length " ++ show l
showShape (Rectangle l w) = "rectangle of length " ++ show l ++ " and width " ++ show w
area :: Shape -> Double
area (Circle r) = (pi * r * r)
这个对我有用。
我认为你的问题是你试图在同一个函数中做两件事:
- 计算面积
- 返回该区域的漂亮字符串信息
你的函数,area :: Shape -> Double
应该只计算面积。
推荐阅读
- java - 如何在我的 json 数据中找到最低记录以及如何在我的 json 数据中找到总页记录
- python - 是否有任何功能可以在 opencv 或其他包中裁剪二进制图像?
- python - 将数据从 c++ 实时传输到 python
- mongodb - 通过mongodb中的子文档_id数组更新多个子文档
- aem - 不是有效的标识符'index,index.html' AEM 6.2
- html - 在最小化浏览器期间,网格标题将隐藏在 p-dialog 中
- python - python xml文件到熊猫数据框
- java - 如何在模板中解析/渲染 Thymeleaf 模板两次
- mongodb - Mongodb聚合对查找结果应用排序,并添加字段索引号
- javascript - 如何为在 java 脚本节点/html 中单击的按钮添加指定的图像?(我是新手)