A gentle introduction to Haskell: ".... there is no single type that contains both 2 and b. Can I make such a type?

I am currently studying Haskell, so here are the novice questions:

What does a single type mean in the text below?

Is a single type a special term of Haskell? Does this mean an atomic type here?

Or does this mean that I can never make a list in Haskell, in which I can put both 1, and 'c'?

I thought that type is a set of values.

Therefore, I can not determine the type containing Charand Int?

What about algebraic data types?

Something like data IntOrChar = In Int | Ch Char:? (I think this should work, but I'm confused by what the author meant by this sentence.)

Btw, is this the only way to make a list in Haskell into which I can put both Ints, and Chars? Or is there a more complicated way?

A Scala analogue: in Scala one could write implicit conversions to a type that represents both Int, and Char(for example, IntOrChar), and then it would be possible without visibility Intand Charin List[IntOrChar], is this not possible with Haskell? Do I always have to explicitly wrap each Intor Charin IntOrCharif I want to put them on a list IntOrChar?

From Gentle Introduction to Haskell :

Haskell --- , . . , (forall a) [a] - , a, . (, [1,2,3]), (['a', 'b', 'c']), .. . ( , , [2, 'b'] , , 2, 'B'.)

+4
3

.

Haskell . - ( ). :

someList :: [IntOrChar]
someList = [In 1, Ch 'c']

, , .

. , . , , , API.

, , .

{-# LANGUAGE ExistentialQuantification, RankNTypes #-}
class IntOrChar a where
   intOrChar :: a -> Either Int Char

instance IntOrChar Int where
   intOrChar = Left

instance IntOrChar Char where
   intOrChar = Right 

data List = Nil
          | forall a. (IntOrChar a) => Cons a List

someList :: List
someList = (1 :: Int) `Cons` ('c' `Cons` Nil)

typeclass IntOrChar IntOrChar. forall a. (IntOrChar a) => a Either Int Char.

, . a ( forall) . Cons, - forall a. (IntOrChar a) => a . , ( ) forall a. (IntOrChar a) => a. , : , IntOrChar Either Int Char.

withHead :: (forall a. (IntOrChar a) => a -> b) -> List -> Maybe b
withHead f Nil = Nothing
withHead f (Cons x _) = Just (f x)

intOrCharToString :: (IntOrChar a) => a -> String
intOrCharToString x = 
   case intOrChar of
    Left i -> show i
    Right c -> show c

someListHeadString :: Maybe String
someListHeadString = withHead intOrCharToString someList

,

{- Wont compile
safeHead :: IntOrChar a => List -> Maybe a
safeHead Nil = Nothing
safeHead (Cons x _) = Just x
-}

-- This will
safeHead2 :: List -> Maybe (Either Int Char)
safeHead2 Nil = Nothing
safeHead2 (Cons x _) = Just (intOrChar x)

safeHead , , IntOrChar a => Maybe a a safeHead, Just x IntOrChar a1 => Maybe a1 a1, Cons.

+5

Scala , Int, Char, AnyVal Any, Char Int. Haskell , .

union, " Int, Char ( Either), Haskell Int IntOrChar.

"", :

data AnyBox = forall a. (Show a, Hashable a) => AB a

heteroList :: [AnyBox]
heteroList = [AB (1::Int), AB 'b']

showWithHash :: AnyBox -> String
showWithHash (AB v) = show v ++ " - " ++ (show . hash) v

let strs = map showWithHash heteroList

, .

+3

, , , , IntOrChar " ", IntOrChar, , Int Char.

( C):

typedef union { char c; int i; } intorchar;

IntOrChar, (apriori), . union struct -:

typedef struct {
          int tag;
          union { char c; int i; } intorchar_u
} IntOrChar;

tag , .

union , , . , int ( 8- 32- int):

union { char b[4]; int i }

, " " " ", , .

IntOrChar ( ), , : , Int ( , Int), a Char ( , Char). , , .

F(a,b) a, b, :

F(IntOrChar,b) = F(Int,b) \times F(Char,b)

\times .

IntOrChar, , , , , Int Char, :

F(intorchar,b) = F(Int,b) \cap F(Char,b)

\cap .

Haskell ( ), , , . Haskell , [2, 'b']. , .

, , , , :

[ I 2, C 'b', ... ]

If you don’t mark your values, you create something similar to an anonymous union, but since there are no (useful) functions that can be applied to both integers and characters, you really can’t do anything with such an union.

+2
source

Source: https://habr.com/ru/post/1534009/


All Articles