📜 ⬆️ ⬇️

The basics

Today I will try to tell the very basics, such as basic data types, types of functions, FVP, lists (including infinite ones).

Subsequent articles:
Data Types, Pattern Matching, and Functions
Classes of types, monads


Compiler and interpreter


')
First you need to download and install the Glasgow Haskell Compiler (GHC) . At the moment, you can get the latest version, but wxHaskell, unfortunately, provides binaries for each platform for different versions of GHC. In addition, some kind of raw (wxHaskell), so that I personally have GHC 6.10.1 versions and 6.8.3.
GHC has three main programs: ghc is a compiler, ghci is an interpreter, and runghc allows you to run programs as scripts without first compiling.
To edit the source, I use HippoEDIT , although any editor is suitable where there is a highlight and you can customize the call to a third-party program for the file.

By default, the ghci prompt contains a list of loaded modules. In order not to overload, you can change it:
Prelude> :s prompt "ghci> "
ghci>

; , , .. , . , , . , , , .
, - , , - . , .. . .


Char
BoolTrue False
Int — . 32 64 .
Integer — «»
Double — ( )

:t
ghci> :t 'x'
'x' :: Char
gchi> :t True
True :: Bool
ghci> :t 5
5 :: (Num t) => t
, 5 Int. «5 t, Num». , , 5 ( ).
( , ) :
ghci> :t 5::Int
5::Int :: Int
ghci> :t (5 + 6 * 3)::Int
(5 + 6 * 3)::Int :: Int

, . , — , .
ghci> :t (True, 'f')
(True, 'f') :: (Bool, Char)
ghci> :t (False, 5::Int, 'q')
(False, 5, 'q') :: (Bool, Int, Char)
ghci> :t ['h','e','l','l','o']
['h','e','l','l','o'] :: [Char]
ghci> :t "hello"
"hello" :: [Char]
, — , .


, :
ghci> odd 5
True
ghci> max 4 5
5
ghci> lines "first line\nsecond line"
["first line","second line"]
ghci> "left" ++ "right"
"leftright"
++ — , , :
ghci> (++) "left" "right"
"leftright"


gchi> :t lines
lines :: String -> [String]
, lines ,
ghci> :t max
max :: (Ord a) => a -> a -> a
, max a ( Ord), .
:
max :: (Ord a) => a -> (a -> a)
currying. , max (a -> a).
, .
ghci> (max 2) 4
4
:
ghci> ((++) "left") "right"
"leftright"
:
ghci> ("left" ++) "right"
"leftright"
ghci> (++ "right") "left"
"leftright"
.. . , . , , (`):
ghci> 2 `max` 4
4
ghci> (2 `max`) 4
4
:
ghci> let max2 = max 2
, , , .
, max2, :
ghci> :t max2
max2 :: Integer -> Integer
- a . , , , , , (let , ):
ghci> let max2 x = max 2 x
ghci> :t max2
max2 :: (Ord t, Num t) -> t -> t
ghci> max2 3.6
3.6

() . , , :
ghci> let applyFunc f1 f2 x = f1 (f2 x)
ghci> applyFunc (*3) (/2) 5
7.5
(*3), .. , 3, — , . 5 7.5
ghci> :t applyFunc
applyFunc :: (t1 -> t2) -> (t -> t1) -> t -> t2
, . : t1 t2, t t1, t t2. , , ( )
(.)
ghci> (not.odd) 5
False
ghci> (not.odd.(+1)) 5
True
odd, not. . , , , « » , .
flip, :
ghci> (-) 2 3
-1
ghci> (flip (-)) 3 2
-1


:
(:)
ghci> 5:6:[]
[5,6]
head :: [a] -> a
tail :: [a] -> [a]
. , . , .
last :: [a] -> a
init :: [a] -> [a]
.
take :: Int -> [a] -> [a]
n
drop :: Int -> [a] -> [a]
n
null :: [a] -> Bool --
length :: [a] -> Int --
reverse :: [a] -> [a] --
map :: (a -> b) -> [a] -> [b]
map ,
ghci> map (*2) [1, 2, 3]
[2,4,6]
Data.Char ( , , toUpper)
ghci> :m + Data.Char
ghci> map toUpper "hello"
"HELLO"
filter :: (a -> Bool) -> [a] -> [a]
,
foldl :: (a -> b -> a) -> a -> [b] -> a
foldr :: (a -> b -> b) -> b -> [a] -> b

ghci> foldr (+) 0 [1, 2, 3, 4]
10
.. , , . :
f (f (f (f 0 1) 2) 3) 4 -- foldl
f 1 (f 2 (f 3 (f 4 0))) -- foldr

product :: Num a => [a] -> a
product = foldr (*) 1
maximum lst = foldr max (head lst) (tail lst)

, .

«» «» , - , .

:
ghci> [1..10]
[1,2,3,4,5,6,7,8,9,10]
ghci> [1,3..10]
[1,3,5,7,9]
ghci> ['a'..'d']
"abcd"
, , . . , , .


ghci> [1..]
[1,2,3,4Interrupted.
, Ctrl-C. 622, .
. , , .
ghci> let lst = [1..]
ghci> let filteredLst = filter even (map (^2) lst)
ghci> take 10 filteredLst
[4,16,36,64,100,144,196,256,324,400]
, (reverse) , .
ghci> (last.reverse) lst
. - , — , , .
— , , ( ), «» (Total FP) :
ghci> let ones = 1 : ones
ghci> take 5 ones
[1,1,1,1,1]
, ( ones — , 1, — ones, .. ) .
? , , . - , «». , , .

, - . . , - , , .

:
ghci> let substr from len str = take len (drop from str)
ghci> substr 2 3 "01234567"
"234"
:
substr from len str = (take len . drop from) str
-- str
substr from len = take len . drop from
substr from len = (.) (take len) (drop from)
-- (take len) (drop from), len
substr from len = flip(.) (drop from) (take len)
-- len take, (flip(.) (drop from))
-- (.)
substr from len = ((flip(.) (drop from)).take) len
-- len
substr from = (flip(.) (drop from)).take
-- take
substr from = (.) (flip(.) (drop from)) take
-- , ,
-- from
substr from = flip(.) take (flip(.) (drop from))
-- from : drop, flip(.), (flip(.)take),
--
substr from = ((flip(.) take).flip(.).drop) from
-- from
substr = (flip(.)take).flip(.).drop

:
ghci> let substr = (flip(.)take).flip(.).drop
ghci> substr 2 3 "01234567"
"234"

, , , - .

, , .

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


All Articles