首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Agda。字符的模式匹配

Agda。字符的模式匹配
EN

Stack Overflow用户
提问于 2020-12-30 18:10:31
回答 1查看 88关注 0票数 1

我曾尝试实现我的"sprintf“函数(它从一个格式化字符串和许多不同类型的参数构造一个字符串),但是失败了。同样的技术也适用于Idris。

代码语言:javascript
复制
module Printf where
open import Agda.Builtin.List
open import Agda.Builtin.Char
open import Agda.Builtin.String
open import Agda.Builtin.Float
open import Agda.Builtin.Int

data Format : Set where
   TChar : Char → Format → Format
   TString : Format → Format
   TFloat : Format → Format
   TInt : Format → Format
   TEnd : Format

parseFormat : List Char → Format
parseFormat ('%' :: 's' :: rest) = TString (parseFormat rest)
parseFormat ('%' :: 'f' :: rest) = TFloat (parseFormat rest)
parseFormat ('%' :: 'd' :: rest) = TInt    (parseFormat rest)
parseFormat ('%' :: '%' :: rest) = TChar '%' (parseFormat rest)
parseFormat (x :: rest)          = TChar x (parseFormat rest)
parseFormat []                   = TEnd

但是我在编译器中得到了这个错误:

代码语言:javascript
复制
Could not parse the left-hand side parseFormat ('%' :: 's' :: rest)
Operators used in the grammar:
  None
when scope checking the left-hand side
parseFormat ('%' :: 's' :: rest) in the definition of parseFormat

在Agda中是否可以对“1”、“t”、“O”这样的字符进行模式匹配?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-02 20:36:57

没有名为_::_List的构造函数。您想使用的是_∷_。将所有出现的::替换为可解决此问题:

代码语言:javascript
复制
parseFormat : List Char → Format
parseFormat ('%' ∷ 's' ∷ rest) = TString (parseFormat rest)
parseFormat ('%' ∷ 'f' ∷ rest) = TFloat (parseFormat rest)
parseFormat ('%' ∷ 'd' ∷ rest) = TInt    (parseFormat rest)
parseFormat ('%' ∷ '%' ∷ rest) = TChar '%' (parseFormat rest)
parseFormat (x ∷ rest)         = TChar x (parseFormat rest)
parseFormat []                 = TEnd
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65505245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档