在此功能的Cardano定义中使用的语法是什么?

发布于 2025-02-12 02:57:11 字数 1487 浏览 0 评论 0 原文

如何解释此功能

friendlyValidityRange
  :: CardanoEra era
  -> (TxValidityLowerBound era, TxValidityUpperBound era)
  -> Aeson.Value
friendlyValidityRange era = \case
  ShelleyTtl ttl -> object ["time to live" .= ttl]
  (lowerBound, upperBound)
    | isLowerBoundSupported || isUpperBoundSupported ->
        object
          [ ...
          ]
    | otherwise -> Null
 where
  isLowerBoundSupported = isJust $ validityLowerBoundSupportedInEra era
  isUpperBoundSupported = isJust $ validityUpperBoundSupportedInEra era

我认为 friendlyvalityrange 函数使用部分函数概念概念,但仍然无法理解它。 friendlyValityRange 's era and (下部,上行)参数以这样的分离方式传递?

我尝试模仿它使用“遵循演示”,但仍无法完成。


module Main where

data Age = Child | Adult

-- Function that accept two agrs: Age, (weightMin, weightMax) , and return health description string
weightAnalyse :: Age -> (Int, Int) -> String
weightAnalyse age = \case
    Child    ->  ?  -- how to comsume the (min, max) tuple
    Adult    ->  ? 
    
main :: IO ()
main = do
  weightAnalyse Child (30, 60)
  weightAnalyse Adult (60, 130)

How to interpret this function:

friendlyValidityRange
  :: CardanoEra era
  -> (TxValidityLowerBound era, TxValidityUpperBound era)
  -> Aeson.Value
friendlyValidityRange era = \case
  ShelleyTtl ttl -> object ["time to live" .= ttl]
  (lowerBound, upperBound)
    | isLowerBoundSupported || isUpperBoundSupported ->
        object
          [ ...
          ]
    | otherwise -> Null
 where
  isLowerBoundSupported = isJust $ validityLowerBoundSupportedInEra era
  isUpperBoundSupported = isJust $ validityUpperBoundSupportedInEra era

I thought the friendlyValidityRange function utilize the partial function concept, but still failed to understand it.
How can friendlyValidityRange's era and (lowerBound, upperBound) parameters be passed in such separated way?

I try to mimic it use follow demo, still unable to finished it.


module Main where

data Age = Child | Adult

-- Function that accept two agrs: Age, (weightMin, weightMax) , and return health description string
weightAnalyse :: Age -> (Int, Int) -> String
weightAnalyse age = \case
    Child    ->  ?  -- how to comsume the (min, max) tuple
    Adult    ->  ? 
    
main :: IO ()
main = do
  weightAnalyse Child (30, 60)
  weightAnalyse Adult (60, 130)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

标点 2025-02-19 02:57:11

您缺少 nofollow noreferrer“>模式同步。 Take a look at 第105行

{-# LANGUAGE PatternSynonyms #-}

pattern ShelleyTtl
  :: SlotNo -> (TxValidityLowerBound era, TxValidityUpperBound era)
pattern ShelleyTtl ttl <-
  ( TxValidityNoLowerBound
  , TxValidityUpperBound ValidityUpperBoundInShelleyEra ttl
  )

这说明了第一个组件为 txvalitynolowerbound code> txvalityuperperbound velityuperityupersupersupertiupipperboundinshelleyera ttlleyera ttl 模式 shelleyttl ttl 带有 ttl 绑定到正确的值。

因此, shelleyttl ttl

friendlyValidityRange era = \case
  ShelleyTtl ttl -> object ["time to live" .= ttl]

匹配特定类型的元组中!

您的 weighanalyse 实际上与您所引用的示例不匹配,但会按照以下方式进行:

{-# LANGUAGE LambdaCase, PatternSynonyms  #-}

data Age = Child String | Adult

pattern ChildPattern x <- (_, Child x)

weightAnalyse :: Int -> (Int, Age) -> String
weightAnalyse id = \case
  ChildPattern x -> x
  (a, age) -> "other"
-- The parameter id is unused and could be removed but is kept
-- to match the original version of the function.

--  *> weightAnalyse 1 (30, Child "foo")
-- "foo"
--  *> weightAnalyse 2 (60, Adult)
-- "other"

You are missing Pattern Synonyms. Take a look at line 105

{-# LANGUAGE PatternSynonyms #-}

pattern ShelleyTtl
  :: SlotNo -> (TxValidityLowerBound era, TxValidityUpperBound era)
pattern ShelleyTtl ttl <-
  ( TxValidityNoLowerBound
  , TxValidityUpperBound ValidityUpperBoundInShelleyEra ttl
  )

This says that a tuple where the first component is TxValidityNoLowerBound and the second component is TxValidityUpperBound ValidityUpperBoundInShelleyEra ttl can be used as the pattern ShelleyTtl ttl with ttl bound to the correct value.

So ShelleyTtl ttl in

friendlyValidityRange era = \case
  ShelleyTtl ttl -> object ["time to live" .= ttl]

matches a particular type of tuple!

Your weightAnalyse does not really match the example you are referencing but would go along the lines of:

{-# LANGUAGE LambdaCase, PatternSynonyms  #-}

data Age = Child String | Adult

pattern ChildPattern x <- (_, Child x)

weightAnalyse :: Int -> (Int, Age) -> String
weightAnalyse id = \case
  ChildPattern x -> x
  (a, age) -> "other"
-- The parameter id is unused and could be removed but is kept
-- to match the original version of the function.

--  *> weightAnalyse 1 (30, Child "foo")
-- "foo"
--  *> weightAnalyse 2 (60, Adult)
-- "other"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文