我对替代前奏很感兴趣。我知道有很多选择:
- https://hackage.haskell.org/packages/#cat:Prelude
- https://guide.aelve.com/haskell/alternative-preludes-zr69k1hc
我知道他们中的很多人修复的一件简单的事情是文本,另一件是在类似head
错误的函数中,当你可能更喜欢它们更安全时。
但是,当我尝试使用这些替代方法时head
,hmm 中的行为似乎完全破坏了该功能,并且对我来说看起来不像是一种改进。这里有些例子:
序幕
Prelude> head [1]
1
Prelude> head []
*** Exception: Prelude.head: empty list
基础
Foundation> head [1]
<interactive>:6:6: error:
• Couldn't match expected type ‘NonEmpty c’
with actual type ‘[Integer]’
• In the first argument of ‘head’, namely ‘[1]’
In the expression: head [1]
In an equation for ‘it’: it = head [1]
• Relevant bindings include
it :: foundation-0.0.21:Foundation.Collection.Element.Element c
(bound at <interactive>:6:1)
Foundation> head []
<interactive>:7:6: error:
• Couldn't match expected type ‘NonEmpty c’ with actual type ‘[a0]’
• In the first argument of ‘head’, namely ‘[]’
In the expression: head []
In an equation for ‘it’: it = head []
• Relevant bindings include
it :: foundation-0.0.21:Foundation.Collection.Element.Element c
(bound at <interactive>:7:1)
安全的
Safe> head []
<interactive>:22:1: error: Variable not in scope: head :: [a0] -> t
优雅的前奏曲
ClassyPrelude> head [1]
<interactive>:24:6: error:
• Couldn't match expected type ‘NonNull mono’
with actual type ‘[Integer]’
• In the first argument of ‘head’, namely ‘[1]’
In the expression: head [1]
In an equation for ‘it’: it = head [1]
• Relevant bindings include
it :: Element mono (bound at <interactive>:24:1)
回避
Relude> head [1]
<interactive>:27:6: error:
• Couldn't match expected type ‘NonEmpty a’
with actual type ‘[Integer]’
• In the first argument of ‘head’, namely ‘[1]’
In the expression: head [1]
In an equation for ‘it’: it = head [1]
• Relevant bindings include it :: a (bound at <interactive>:27:1)
里约
RIO> head [1]
<interactive>:7:1: error:
Variable not in scope: head :: [Integer] -> t
序曲
Protolude> head [1]
Just 1
Protolude> head []
Nothing
这看起来不错——它也适用于尾巴,对吧?
Protolude> tail [1]
<interactive>:12:1: error:
• Variable not in scope: tail :: [Integer] -> t
• Perhaps you meant ‘tails’ (imported from Protolude)
Protolude> tails [1]
[[1],[]]
Protolude> tails []
[[]]
好吧,这不完全是一个替代品。
我错过了什么,为什么这会更好,为什么这些函数会在它们失败的情况下被定义?