haskell

Unifying Types in Haskell

你离开我真会死。 提交于 2020-08-24 10:21:50
问题 I'm kind of stuck with an assignement concerning my exams. I want to find out the types of those two functions by applying the unifying algorithm by hand: map map (\x -> x >>= (\y -> y)) Could someone point me to the right direction? The only ressource I could find until now was the wikipedia entry which is not really aiding me because of the high level of abstraction. Greetings and thank you. 回答1: Let's just do the first. map :: (a -> b) -> [a] -> [b] Now we can write it again with two

Haskell underscore vs. explicit variable

有些话、适合烂在心里 提交于 2020-08-24 06:38:09
问题 I've been learning Haskell for a few weeks, and I have a question about the use of the underscore ( _ ) as a function parameter. I think my question will be better asked with a specific example. Let's say I want to define a function that extracts the element of a list based on the provided index—yes, I realize (!!) is already pre-defined. The two ways that I can define the function (I'm sure there are more) are the following: Version 1 indexedElement :: [a] -> Int -> a indexedElement xs n | n

Haskell underscore vs. explicit variable

泪湿孤枕 提交于 2020-08-24 06:36:09
问题 I've been learning Haskell for a few weeks, and I have a question about the use of the underscore ( _ ) as a function parameter. I think my question will be better asked with a specific example. Let's say I want to define a function that extracts the element of a list based on the provided index—yes, I realize (!!) is already pre-defined. The two ways that I can define the function (I'm sure there are more) are the following: Version 1 indexedElement :: [a] -> Int -> a indexedElement xs n | n

Can Nullable be used as a functor in C#?

|▌冷眼眸甩不掉的悲伤 提交于 2020-08-22 08:32:33
问题 Consider the following code in C#. public int Foo(int a) { // ... } // in some other method int? x = 0; x = Foo(x); The last line will return a compilation error cannot convert from 'int?' to 'int' which is fair enough. However, for example in Haskell there is Maybe which is a counterpart to Nullable in C#. Since Maybe is a Functor I would be able to apply Foo to x using fmap . Does C# have a similar mechanism? 回答1: We can implement such functionality ourselves: public static class FuncUtils

Can Nullable be used as a functor in C#?

荒凉一梦 提交于 2020-08-22 08:32:10
问题 Consider the following code in C#. public int Foo(int a) { // ... } // in some other method int? x = 0; x = Foo(x); The last line will return a compilation error cannot convert from 'int?' to 'int' which is fair enough. However, for example in Haskell there is Maybe which is a counterpart to Nullable in C#. Since Maybe is a Functor I would be able to apply Foo to x using fmap . Does C# have a similar mechanism? 回答1: We can implement such functionality ourselves: public static class FuncUtils

Non type-variable argument in the constraint error on Haskell map function

為{幸葍}努か 提交于 2020-08-22 05:11:25
问题 I am writing a function called mapper2 that applies a function to two lists: mapper2 :: (a-> b -> c) -> [a] -> [b] -> [c] mapper2 f (x:xs) (y:ys) = (f x y) : (mapper2 f xs ys) mapper2 _ _ _ = [] I am able to compile the function but get an error when I apply it: *Main> mapper2 (\x -> x*2) [2,4] [4,6] <interactive>:4:1: error: • Non type-variable argument in the constraint: Num (b -> c) (Use FlexibleContexts to permit this) • When checking the inferred type it :: forall b c. (Num (b -> c), Num

Is it possible to reorganize nested tuples?

淺唱寂寞╮ 提交于 2020-08-21 19:43:06
问题 What I want to do is something like this: Take an arbitrary polymorphic tuple: x = (((1, ""), Nothing), ('', 6)) And reorganize with something like this type (not necessarily in the same order but the same structure.: (Int, (Char, (Maybe Int, (String, (Int, ())))) I really don't know the name for this pattern so I am unable to use google to the best of my ability. 回答1: If you only have to deal with this specific case, i.e., converting from (((Int, String), Maybe Int), (Char, Int)) to (Int,

Is it possible to reorganize nested tuples?

六眼飞鱼酱① 提交于 2020-08-21 19:41:25
问题 What I want to do is something like this: Take an arbitrary polymorphic tuple: x = (((1, ""), Nothing), ('', 6)) And reorganize with something like this type (not necessarily in the same order but the same structure.: (Int, (Char, (Maybe Int, (String, (Int, ())))) I really don't know the name for this pattern so I am unable to use google to the best of my ability. 回答1: If you only have to deal with this specific case, i.e., converting from (((Int, String), Maybe Int), (Char, Int)) to (Int,

Is Hask locally small?

拜拜、爱过 提交于 2020-08-21 10:12:31
问题 Is the category Hask of haskell objects an example of a locally small category? http://ncatlab.org/nlab/show/locally+small+category Maybe not.. hask as cpo http://www.cs.gunma-u.ac.jp/~hamana/Papers/cpo.pdf The haskellwiki, http://www.haskell.org/haskellwiki/Hask has very good information, showing that Hask is not Cartesian Closed. 回答1: What is Hask? If it includes all the haskell definable "functions" as morphism then definitly not data Big = Big (Big -> Big) the "hom set" of Big -> Big

Is Hask locally small?

一个人想着一个人 提交于 2020-08-21 10:12:14
问题 Is the category Hask of haskell objects an example of a locally small category? http://ncatlab.org/nlab/show/locally+small+category Maybe not.. hask as cpo http://www.cs.gunma-u.ac.jp/~hamana/Papers/cpo.pdf The haskellwiki, http://www.haskell.org/haskellwiki/Hask has very good information, showing that Hask is not Cartesian Closed. 回答1: What is Hask? If it includes all the haskell definable "functions" as morphism then definitly not data Big = Big (Big -> Big) the "hom set" of Big -> Big