是否可以在类型名称本身而不是在类型的构造函数上进行模式匹配?
这是一个人为的例子,代码不能编译,我希望解释我在追求的内容:
what :: a -> String
what (x :: String) = "It's a String!"
what (x :: Int) = "It's an Int!"
what x = "Not sure what it is."
Run Code Online (Sandbox Code Playgroud) 考虑以下.为什么它不起作用.我怎么能让它发挥作用?
type IntOrString
= String
| Int
getInt : IntOrString -> String
getInt intOrString =
case intOrString of
String ->
intOrString
Int ->
toString intOrString
Run Code Online (Sandbox Code Playgroud) 我无法输入域名来观察登录页面:
http://some_url.com
Run Code Online (Sandbox Code Playgroud)
错误:
找不到文件'E:\ index.html'.
但是,以下工作:
仅供参考:本网站由Elm,Giraffe和.Net Core构建.
我正在尝试创建一个将列表中的部分字符串连接到另一个字符串的函数.我已经知道如何选择我想要的字符串,但现在我需要它们的第一部分(并且它并不总是相同的值).
所以我想剪掉一个字符串的第一部分
-- here is a function who should take a string and the numbers
-- of characters we want to take from the start
cutString :: Int -> String -> String
cutString x str = cut x str -- here's a way to take the first x characters
Run Code Online (Sandbox Code Playgroud)
以及使用它的方式:
print (cutString 3 "Hello World")
Output --> "Hel"
Run Code Online (Sandbox Code Playgroud)
有没有简单的方法可以做到这一点?
感谢您提供任何帮助或建议.