Haskell character syntax and semantics

I am studying Haskell.

One of my standard methods for learning a new language is to implement the Hello World genetic algorithm, which tries to generate a string using the methods of the genetic algorithm that corresponds to some input string.

Due to my inexperience with Haskell (the closest I need to compare is Kotlin) I was looking for sample code so that I could match my existing understanding of the basic genetic algorithm with the code and understand some understanding of Haskell based on mine already in the process of reading / language learning.

I came across this tutorial: https://www.arcadianvisions.com/blog/2011/haskell-genetic-algorithm-hello-world.html

I rewrote it in the atom after installing my environment, every part that I did not understand, I quickly made google, if I did not understand the syntax / semantics in 15 minutes, I would continue to decrypt with the intention of catching the later parts of these parts.

So, I understood most of the code, for example, the order in which the function is applied, monads (I think I'm almost there with monads), data types, function types, currying, type substitution, etc. However, there were a few bits of syntax / semantics that I did not see in my readings / studies and I'm not sure what they are doing, but they look very much in the code example that I linked above. I hope someone can explain them to me:

(++)
(:)
<$>
<*>
(,)
(x !!)
p@(info@())

, () <> - , ? ( atom-haskell-ghc), Functor f => Applicative (f :: * -> *) where <*> :: f (a -> b) -> f a -> f b, , / , ( ?).

, :

mate :: RandomGen g => Gene -> Gene -> Rand g Gene
mate gene1 gene2 = (++) <$> flip take gene1 <*> flip drop gene2 <$> pivot
  where pivot = getRandomR (0, length gene1 - 1)
+4
2

.

<> , .

() , , , , (, ++), .

  • ++ - [1,2] ++ [3,4] = [1,2,3,4] (++) [1,2] [3,4] = [1,2,3,4]

  • : - cons, 1 : [2, 3, 4] = [1,2,3,4] (:) 1 [2, 3, 4] = [1,2,3,4] .

  • <$> - infix fmap

  • <*> -

  • , (,) 1 2 = (1, 2)

  • !! - [1,2,3] !! 1 = 2. , , O (n).

  • @ " ". , . , f (xs@[x1, x2]) , x1 x2 xs

+6

Haskell , , , , (++) (:), , , (++) x y, , x ++ y. Haskell , x ++ y (++) x y ( , ).

(++)

: (++) :: [a] -> [a] -> [a] , , . :

(++) [1, 4, 2, 5] [1, 3, 0, 2] == [1, 4, 2, 5, 1, 3, 0, 2]

(:)

[a]. (:) :: a -> [a] -> [a]. ( ), , , . :

(:) 1 [4, 2, 5] = [1, 4, 2, 5]

(<$>)

<$>, , , , , , - (<$>) :: Functor f => (a -> b) -> f a -> f b.

A Functor - . Haskell . - [] a Maybe. (<$>) f :: a -> b , [a]. - [b]. , , Functor ((<$>) Maybe , []).

, Functor (a Maybe Nothing Just x). , , , :

(+1) <$> [1, 4, 2, 5] == [2, 5, 3, 6]
(+1) <$> Nothing == Nothing
(+1) <$> (Just 2) == Just 3

(<*>)

(<*>) :: Applicative f => f (a -> b) -> f a -> f b - . Applicative.

, Applicative, : pure :: Applicative a => a -> f a (<*>) :: Applicative f => f (a -> b) -> f a -> f b ( liftA2, ).

Applicative ( , ) (, [] Maybe). ( a -> b) a s. "" , , , :

   [f1, f2, ..., fm] <*> [x1, x2, ..., xn]
== [f1 x1, f1 x2, ..., f1 xn,
    f2 x1, f2 x2, ..., f2 xn,
    ...,
    fm x1, fm x2, ..., fm xn]

, , , Maybe, Nothing Nothing, Nothing, Nothing, Just (so Just f <*> Just x), a Just (f x):

Just f <*> Just x == Just (f x)
Just f <*> Nothing == Nothing
Nothing <*> Just x == Nothing
Nothing <*> Nothing == Nothing

(,)

2-: (,) :: a -> b -> (a,b), , a a b, 2-, , - . :

(,) 4 'a' == (4, 'a')

(x !!)

. , , . . :

([1, 4, 2, 5] !!) == (!!) [1, 4, 2, 5]
(!! 2) == flip (!!) 2

, , , , . :

(!! 2) [1, 4, 2, 5] == (!!) [1, 4, 2, 5]

(!!) :: [a] -> Int -> a Int, ( ).

p@(info@())

, @ (, ), .

, ( ).

, , , 2-, , :

somefunction total@(left, _) = ...

, somefunction (4, 'a'), , total (4, 'a'), left 4.

+7

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


All Articles