是否有一个像chr一样的函数但是如果它的参数不是Unicode标量值则会失败?

Gai*_*ith 5 unicode haskell

我认为chr在其参数不是Unicode标量值的情况下,应该有一个函数可以工作但失败.

我通常用这种方式编写自己的函数:

toUnicode :: Int -> Maybe Char
toUnicode x
  -- Ranges from "The Unicode Standard".
  -- See definition D76 in Section 3.9, Unicode Encoding Forms.
  | x >= 0      && x <= 0xD7FF   = Just (chr x)
  | x >= 0xE000 && x <= 0x10FFFF = Just (chr x)
  | otherwise                    = Nothing
Run Code Online (Sandbox Code Playgroud)

但如果有更好的方法可以做到这一点会很好.

Dam*_*ero 1

这是您用来做的事情,您可以创建一个添加模块,例如Data.Char.Adds并在需要时导入。好像现在不存在。我看不到其他解决方法。