Iterate abstract --Basic

本文介绍了Haskell编程语言中的基本数据类型,包括布尔型、字符型、整数型及浮点型等,并简要说明了Maybe与Either这两种常用的数据结构。

不存在“无中生有”,物理学家寻找基本粒子,我们即是上帝,数学家用定义和公理确定这些“基本例子”。下面我来总结一下Haskell里面最Basic的基本粒子。

 

Bool
-- |The 'Bool' type is an enumeration.  It is defined with 'False'
-- first so that the corresponding 'Prelude.Enum' instance will give
-- 'Prelude.fromEnum' 'False' the value zero, and
-- 'Prelude.fromEnum' 'True' the value 1.
data  Bool  =  False | True  deriving (Eq, Ord)

        -- Read in GHC.Read, Show in GHC.Show
Ordering
-- | Represents an ordering relationship between two values: less
-- than, equal to, or greater than.  An 'Ordering' is returned by
-- 'compare'.
data Ordering = LT | EQ | GT deriving (Eq, Ord)

        -- Read in GHC.Read, Show in GHC.Show
 Char
{-| The character type 'Char' is an enumeration whose values represent
Unicode (or equivalently ISO\/IEC 10646) characters
(see <http://www.unicode.org/> for details).
This set extends the ISO 8859-1 (Latin-1) character set
(the first 256 charachers), which is itself an extension of the ASCII
character set (the first 128 characters).
A character literal in Haskell has type 'Char'.

To convert a 'Char' to or from the corresponding 'Int' value defined
by Unicode, use 'Prelude.toEnum' and 'Prelude.fromEnum' from the
'Prelude.Enum' class respectively (or equivalently 'ord' and 'chr').
-}
data Char = C# Char#


-- We don't use deriving for Eq and Ord, because for Ord the derived
-- instance defines only compare, which takes two primops.  Then
-- '>' uses compare, and therefore takes two primops instead of one.

 Int
data Int = I# Int#

-- ^A fixed-precision integer type with at least the range @[-2^29 .. 2^29-1]@.
-- The exact range for a given implementation can be determined by using
-- 'Prelude.minBound' and 'Prelude.maxBound' from the 'Prelude.Bounded' class.
 Integer
-- | Arbitrary-precision integers.
data Integer    

= S# Int# -- small integers #ifndef ILX
| J# Int# ByteArray# -- large integers #else
| J# Void BigInteger -- .NET big ints foreign type dotnet "BigInteger" BigInteger #endif
 Float
-- | Single-precision floating point numbers.
-- It is desirable that this type be at least equal in range and precision
-- to the IEEE single-precision type.
data Float      = F# Float#

 Double
-- | Double-precision floating point numbers.
-- It is desirable that this type be at least equal in range and precision
-- to the IEEE double-precision type.
data Double     = D# Double#

 Basic Monads(有些不合时宜,先放在这里)

IO Monad
{-|
A value of type @'IO' a@ is a computation which, when performed,
does some I\/O before returning a value of type @a@.  

There is really only one way to \"perform\" an I\/O action: bind it to
@Main.main@ in your program.  When your program is run, the I\/O will
be performed.  It isn't possible to perform I\/O from an arbitrary
function, unless that function is itself in the 'IO' monad and called
at some point, directly or indirectly, from @Main.main@.

'IO' is a monad, so 'IO' actions can be combined using either the do-notation
or the '>>' and '>>=' operations from the 'Monad' class.
-}
newtype IO a = IO (State# RealWorld -> (# State# RealWorld, a #))

 Maybe Monad
-- | The 'Maybe' type encapsulates an optional value.  A value of type
-- @'Maybe' a@ either contains a value of type @a@ (represented as @'Just' a@), 
-- or it is empty (represented as 'Nothing').  Using 'Maybe' is a good way to 
-- deal with errors or exceptional cases without resorting to drastic
-- measures such as 'error'.
--
-- The 'Maybe' type is also a monad.  It is a simple kind of error
-- monad, where all errors are represented by 'Nothing'.  A richer
-- error monad can be built using the 'Data.Either.Either' type.

data  Maybe a  =  Nothing | Just a

deriving (Eq, Ord)
 Either Monad
{-|

The 'Either' type represents values with two possibilities: a value of
type @'Either' a b@ is either @'Left' a@ or @'Right' b@.

The 'Either' type is sometimes used to represent a value which is
either correct or an error; by convention, the 'Left' constructor is
used to hold an error value and the 'Right' constructor is used to
hold a correct value (mnemonic: \"right\" also means \"correct\").
-}
data  Either a b  =  Left a | Right b   deriving (Eq, Ord ) 
### 错误解析与解决方法 `Invalid attempt to destructure non-iterable instance` 或 `Invalid attempt to spread non-iterable instance` 是 JavaScript 中常见的运行时错误,通常发生在尝试对一个非可迭代对象进行解构或展开操作时。这种错误的核心原因是:JavaScript 要求被解构或展开的对象必须是**可迭代的(Iterable)**,例如数组、字符串、Map、Set 等内置数据结构。 #### 常见报错场景 1. **函数参数使用了解构语法,但传入的参数不是数组或可迭代对象** ```javascript function example([a, b]) { console.log(a, b); } example(); // 报错:Invalid attempt to destructure non-iterable instance ``` 该情况中,函数期望接收一个数组作为参数并从中提取元素,但调用时未传入任何参数,导致解构失败[^1]。 2. **使用扩展运算符(Spread Operator)对非可迭代对象进行展开** ```javascript const obj = { key: 'value' }; const arr = [...obj]; // 报错:Invalid attempt to spread non-iterable instance ``` 扩展运算符要求其作用的对象具有 `[Symbol.iterator]` 方法,而普通对象不具备此特性[^3]。 3. **在异步代码中处理 Promise 链时,某个环节返回了非可迭代值** ```javascript const data = await fetchSomeData(); // 假设 data 不是可迭代对象 const [firstItem] = data; // 如果 data 是 undefined 或 null,会触发错误 ``` #### 解决方案 ##### 检查函数参数是否为可迭代对象 确保传递给函数的参数是一个数组或其他可迭代对象。如果参数可能为空或不可预测,可以在函数内部添加默认值或类型检查: ```javascript function example([a, b] = []) { console.log(a, b); // 如果未传参,则 a 和 b 为 undefined } example(); // 不再报错 ``` ##### 对非可迭代对象进行转换 如果需要对普通对象进行类似数组的操作,可以先将其键值提取为数组: ```javascript const obj = { a: 1, b: 2 }; const values = Object.values(obj); // 转换为数组 [1, 2] const arr = [...values]; // 正确展开 ``` ##### 在异步代码中添加防御性判断 当从 API 获取数据时,确保数据格式符合预期,避免直接对未定义或空值进行解构或展开: ```javascript const data = await fetchSomeData(); if (Array.isArray(data)) { const [firstItem] = data; console.log(firstItem); } else { console.warn('数据格式不正确,无法解构'); } ``` ##### 使用默认值防止未定义错误 为变量赋予默认值可以有效避免因未传参或数据缺失而导致的错误: ```javascript function method([el] = []) { console.log(el); } method(); // 不再报错 ``` ##### React 应用中的常见问题 在 React 开发中,尤其是在使用状态管理库(如 Redux、MobX)或异步请求库(如 Axios、Fetch)时,此类错误也可能出现。常见原因包括组件 props 未正确传递、API 返回格式不符合预期等。解决方案包括: - **验证 props 类型和默认值**:使用 `PropTypes` 或 TypeScript 定义组件输入规范。 - **对异步响应进行规范化处理**:确保返回的数据结构一致,避免直接操作未经校验的结果。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值