Why is `pure` only required for Applicative and not already for Functor?

Reading this Wikibook about Haskell and Category Theory basics, I learn about Functors: A functor is essentially a transformation between categories, so given categories C and D, a functor F : C -> D maps any object A in C to F(A), in D. maps morphisms f : A -> B in C to F(f) : F(A) -> F(B) in D. ... which sounds all nice. Later an example is provided: Let's have a sample

为什么`pure`只适用于Applicative而不是Functor?

阅读这本关于Haskell和Category Theory基础知识的Wikibook,我了解了Functors: 函子本质上是类别之间的转换,因此给定类别C和D,函子F:C - > D 将C中的任何对象A映射到F(A),D中。 映射态射f:在C中的A→B到F(f):在D中的F(A)→F(B) ......听起来很好。 稍后提供一个例子: 我们也有一个示例实例: instance Functor Maybe where fmap f (Just x) = Just (f x) fmap _ Nothing = Nothing 以下是关

Where do values fit in Category of Hask?

So we have Category of Hask, where: Types are the objects of the category Functions are the morphisms from object to object in the category. Similarly for Functor we have: a Type constructor as the mapping of objects from one category to another fmap for the mapping of morphisms from one category to another. Now, when we write program we basically transform values (not types) and it s

Hask的类别适合哪些值?

所以我们有Hask Category,其中: 类型是类别的对象 函数是类中从对象到对象的态射。 与Functor类似,我们有: 一个类型构造器,用于将对象从一个类别映射到另一个类别 用于将态射从一个类别映射到另一个类别的fmap 。 现在,当我们编写程序时,我们基本上转换了值(而不是类型),看起来Hask的类别根本不谈论值。 我试图在整个方程中拟合数值,并提出以下观察结果: 每个类型本身就是一个类别。 例如:Int是所

How are functors in Haskell related to functors in category theory?

For as far as I understand, a functor is a mapping between two categories, for example from objects in C http://mathurl.com/32qch9w.png to objects in D http://mathurl.com/36b8r37.png where C http://mathurl.com/32qch9w.png and D http://mathurl.com/36b8r37.png are categories. In Haskell there is Hask in which the objects are Haskell types and the morphisms are Haskell functions. However, the Fun

Haskell中的函子如何与类别理论中的函子相关?

就我所知,函子是两类之间的映射,例如从C http://mathurl.com/32qch9w.png中的对象到D http://mathurl.com/36b8r37.png中的对象,其中C http://mathurl.com/32qch9w.png和D http://mathurl.com/36b8r37.png是类别。 在Haskell中有Hask ,其中对象是Haskell类型,态射是Haskell函数。 然而, Functor类型类有一个函数fmap ,它映射这些类型(因此它们是对象而不是类别本身): fmap :: (a -> b) -> f a -> f b fa

Haskell error: non

I'm trying to write a function that will count the words in a sentence. cwords :: String -> Int cwords "" = 0 cwords (a:b:c) |isAlpha a && (isAlpha b == False) = 1 + cwords (b:c) |otherwise = cwords (b:c) I keep getting an error saying "non-exhaustive patterns in function cwords" whenever I enter a sentence. An empty string works fine. (I'm very new to Ha

哈斯克尔错误:非

我试图写一个函数来计算句子中的单词。 cwords :: String -> Int cwords "" = 0 cwords (a:b:c) |isAlpha a && (isAlpha b == False) = 1 + cwords (b:c) |otherwise = cwords (b:c) 每当我输入一个句子时,我都会收到一个错误,说“功能词汇中的非穷举模式”。 一个空字符串正常工作。 (我对Haskell很新颖) 问题是你定义了两个子句: 一个空列表 cwords "" = 0 还有一个列表至少包含两个元素: c

exhaustive patterns using if

I have the following function: myMaximum [] = error "There is no such thing as 'maximum' in an empty list." myMaximum [x] = x myMaximum (x:xs) = if x >= tailMax then x else tailMax where tailMax = myMaximum xs I works just fine when I run myMaximum [1..5] , but it throws the error defined on the first line when I run myMaximum [5..1] .

使用if的详尽模式

我有以下功能: myMaximum [] = error "There is no such thing as 'maximum' in an empty list." myMaximum [x] = x myMaximum (x:xs) = if x >= tailMax then x else tailMax where tailMax = myMaximum xs 当我运行myMaximum [1..5] ,我工作得很好,但是当我运行myMaximum [5..1]时,它会引发第一行定义的错误。 如果我把第一行写出来,它会抱怨myMaximum上

Calculating the length of an array in haskell

This question already has an answer here: It works when loaded from file, but not when typed into ghci. Why? 2 answers What you have is two declarations, the second of which shadows the first. You need to declare len as one function with two clauses. In GHCi, you can do that like this: :{ let len [] = 0 len (h:t) = 1 + len t :} the :{ ... :} form lets you enter multi-line declarati

计算haskell中数组的长度

这个问题在这里已经有了答案: 它从文件加载时起作用,但在输入ghci时不起作用。 为什么? 2个答案 你有两个声明,其中第二个是第一个声明。 您需要将len声明为具有两个子句的一个函数。 在GHCi中,你可以这样做: :{ let len [] = 0 len (h:t) = 1 + len t :} :{ ... :}表单允许您像在*.hs文件中那样输入多行声明。 GHCi, version 7.6.3: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ..

exhaustive patterns in function

Attempting to create a function that removes duplicates from a list, and replaces them with a single element. Keep getting an error message "Non-exhaustive patterns in function removeduplicate". I assume that means my pattern matching is missing a possible case? I think I've covered all the possibilities though. I'm very new to Haskell, so any help is greatly appreciated. r

功能上的详尽模式

尝试创建一个从列表中删除重复项的函数,并用一个元素替换它们。 继续收到错误消息“函数removeduplicate中的非穷举模式”。 我认为这意味着我的模式匹配缺少可能的情况? 我想我已经涵盖了所有的可能性。 我对Haskell很新,所以任何帮助都非常感谢。 removeduplicate :: (Eq a) => [a] -> [a] removeduplicate [] = [] removeduplicate (x:[]) = [x] removeduplicate (x:z:[]) = if z == x then [x] else (x:z:[]) remo

exhaustive guards cause irrefutable pattern match to fail?

I have this function in Haskell: test :: (Eq a) => a -> a -> Maybe a test a b | a == b = Just a test _ _ = Nothing This is what I got when I tried the function with different inputs: ghci>test 3 4 Nothing ghci>test 3 3 Just 3 According to Real World Haskell, the first pattern is irrefutable. But it seems like test 3 4 doesn't fails the first pattern, and matches the seco

彻底的卫兵造成无可辩驳的模式匹配失败?

我在Haskell中有这个函数: test :: (Eq a) => a -> a -> Maybe a test a b | a == b = Just a test _ _ = Nothing 这是我用不同的输入尝试函数时得到的结果: ghci>test 3 4 Nothing ghci>test 3 3 Just 3 根据真实世界Haskell,第一种模式是无可辩驳的。 但似乎test 3 4不会失败的第一个模式,并匹配第二个。 我预计会出现某种错误 - 也许是“非穷举的守卫”。 那么这里究竟发生了什么,有没有办法在发

Erratic hole type resolution

I recently found out that type holes combined with pattern matching on proofs provides a pretty nice Agda-like experience in Haskell. For example: {-# LANGUAGE DataKinds, PolyKinds, TypeFamilies, UndecidableInstances, GADTs, TypeOperators #-} data (==) :: k -> k -> * where Refl :: x == x sym :: a == b -> b == a sym Refl = Refl data Nat = Zero | Succ Nat data SNat :: N

不规则的孔类型分辨率

我最近发现,类型孔与证明上的模式匹配相结合,在Haskell中提供了非常好的Agda式的体验。 例如: {-# LANGUAGE DataKinds, PolyKinds, TypeFamilies, UndecidableInstances, GADTs, TypeOperators #-} data (==) :: k -> k -> * where Refl :: x == x sym :: a == b -> b == a sym Refl = Refl data Nat = Zero | Succ Nat data SNat :: Nat -> * where SZero :: SNat Zero SSucc :: SNa

Type defaulting with non

The following program compiles with GHC: main :: IO () main = print $ 2^2 despite the signature of (^) :: (Num a, Integral b) => a -> b -> a due to GHC's type defaulting mechanism. I'm using numeric-prelude, which instead exports (^) :: Ring.C a => a -> Integer -> a This is extremely annoying to use with Int exponents, so I prefer Prelude's version with a pol

输入非违约

以下程序使用GHC进行编译: main :: IO () main = print $ 2^2 尽管签署了 (^) :: (Num a, Integral b) => a -> b -> a 由于GHC的类型违约机制。 我使用数字前奏,而不是导出 (^) :: Ring.C a => a -> Integer -> a 这对使用Int指数非常恼人,所以我更喜欢带有多态指数的Prelude版本。 因此,我定义了以下内容: {-# LANGUAGE NoImplicitPrelude, RebindableSyntax, ScopedTypeVariables #-} import