mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-05-19 04:40:59 +00:00
Translate Haskell IO
This commit is contained in:
parent
b80c70b4a3
commit
ac680b6a7f
@ -317,31 +317,29 @@ Nothing -- typu `Maybe a` for any `a`
|
|||||||
-- 8. Haskell IO
|
-- 8. Haskell IO
|
||||||
----------------------------------------------------
|
----------------------------------------------------
|
||||||
|
|
||||||
-- While IO can't be explained fully without explaining monads,
|
-- Chociaż obsługa wejścia i wyjścia nie może zostać wyjaśniona przez poznaniem
|
||||||
-- it is not hard to explain enough to get going.
|
-- monad, spróbujemy zrobić to częściowo
|
||||||
|
|
||||||
-- When a Haskell program is executed, `main` is
|
-- Wykonanie programu napisanego w Haskellu wywołuje funkcję `main`
|
||||||
-- called. It must return a value of type `IO a` for some type `a`. For example:
|
-- Musi zwrócić wartość typu `IO a` dla pewnego `a`. Przykład:
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = putStrLn $ "Hello, sky! " ++ (say Blue)
|
main = putStrLn $ "Hello, sky! " ++ (say Blue)
|
||||||
-- putStrLn has type String -> IO ()
|
-- putStrLn has type String -> IO ()
|
||||||
|
|
||||||
-- It is easiest to do IO if you can implement your program as
|
-- Najłatwiej obsłużyć wejście i wyjście, kiedy program zostanie
|
||||||
-- a function from String to String. The function
|
-- zaimplementowany jako funkcja String -> String. Funkcja
|
||||||
-- interact :: (String -> String) -> IO ()
|
-- interact :: (String -> String) -> IO ()
|
||||||
-- inputs some text, runs a function on it, and prints out the
|
-- pobiera pewien tekst, wykonuje na nim operacje, po czym wypisuje wynik.
|
||||||
-- output.
|
|
||||||
|
|
||||||
countLines :: String -> String
|
countLines :: String -> String
|
||||||
countLines = show . length . lines
|
countLines = show . length . lines
|
||||||
|
|
||||||
main' = interact countLines
|
main' = interact countLines
|
||||||
|
|
||||||
-- You can think of a value of type `IO ()` as representing a
|
-- Możesz myśleć o wartości typu `IO ()` jako reprezentującej ciąg czynności,
|
||||||
-- sequence of actions for the computer to do, much like a
|
-- które komputer ma wykonać, zupełnie niczym program komputerowy w imperatywnym
|
||||||
-- computer program written in an imperative language. We can use
|
-- języku programowania. Akcje można łączyć przy użyciu notacji `do`:
|
||||||
-- the `do` notation to chain actions together. For example:
|
|
||||||
|
|
||||||
sayHello :: IO ()
|
sayHello :: IO ()
|
||||||
sayHello = do
|
sayHello = do
|
||||||
@ -349,23 +347,20 @@ sayHello = do
|
|||||||
name <- getLine -- this gets a line and gives it the name "name"
|
name <- getLine -- this gets a line and gives it the name "name"
|
||||||
putStrLn $ "Hello, " ++ name
|
putStrLn $ "Hello, " ++ name
|
||||||
|
|
||||||
-- Exercise: write your own version of `interact` that only reads
|
-- Ćwiczenie: napisz własną wersję `interact`,
|
||||||
-- one line of input.
|
-- która czyta tylko jedną linię wejścia.
|
||||||
|
|
||||||
-- The code in `sayHello` will never be executed, however. The only
|
-- Kod w `sayHello` nigdy się nie wykona. Jedyną akcją, która zostanie
|
||||||
-- action that ever gets executed is the value of `main`.
|
-- uruchomiona, jest wartość `main`.
|
||||||
-- To run `sayHello` comment out the above definition of `main`
|
-- Aby uruchomić `sayHello`, należy zastąpić poprzednią definicję `main` przez
|
||||||
-- and replace it with:
|
|
||||||
-- main = sayHello
|
-- main = sayHello
|
||||||
|
|
||||||
-- Let's understand better how the function `getLine` we just
|
-- Spróbujmy lepiej zrozumieć, jak działa funkcja `getLine`, której właśnie
|
||||||
-- used works. Its type is:
|
-- użyliśmy. Jej typem jest
|
||||||
-- getLine :: IO String
|
-- getLine :: IO String
|
||||||
-- You can think of a value of type `IO a` as representing a
|
-- Możesz myśleć o wartości typu `IO a` jako reprezentującej program, który
|
||||||
-- computer program that will generate a value of type `a`
|
-- wygeneruje wartość typu `a`, poza wszystkim innym, co jeszcze zrobi.
|
||||||
-- when executed (in addition to anything else it does). We can
|
-- Możemy także tworzyć własne akcje typu `IO String`:
|
||||||
-- name and reuse this value using `<-`. We can also
|
|
||||||
-- make our own action of type `IO String`:
|
|
||||||
|
|
||||||
action :: IO String
|
action :: IO String
|
||||||
action = do
|
action = do
|
||||||
@ -376,7 +371,7 @@ action = do
|
|||||||
-- `return` is not a keyword, but merely a function
|
-- `return` is not a keyword, but merely a function
|
||||||
return (input1 ++ "\n" ++ input2) -- return :: String -> IO String
|
return (input1 ++ "\n" ++ input2) -- return :: String -> IO String
|
||||||
|
|
||||||
-- We can use this just like we used `getLine`:
|
-- Możemy użyć tego tak jak używaliśmy `getLine`:
|
||||||
|
|
||||||
main'' = do
|
main'' = do
|
||||||
putStrLn "I will echo two lines!"
|
putStrLn "I will echo two lines!"
|
||||||
@ -384,15 +379,14 @@ main'' = do
|
|||||||
putStrLn result
|
putStrLn result
|
||||||
putStrLn "This was all, folks!"
|
putStrLn "This was all, folks!"
|
||||||
|
|
||||||
-- The type `IO` is an example of a "monad". The way Haskell uses a monad to
|
-- Typ `IO` jest przykładem monady. Sposób w jakim Haskell używa monad do
|
||||||
-- do IO allows it to be a purely functional language. Any function that
|
-- obsługi wejścia i wyjścia pozwala mu być czysto funkcyjnym językiem.
|
||||||
-- interacts with the outside world (i.e. does IO) gets marked as `IO` in its
|
-- Każda funkcja, która wchodzi w interakcje ze światem zewnętrznym, oznaczana
|
||||||
-- type signature. This lets us reason about what functions are "pure" (don't
|
-- jest jako `IO` w jej sygnaturze typu, co umożliwia odróżnianie funkcji
|
||||||
-- interact with the outside world or modify state) and what functions aren't.
|
-- czystych od zależnych od świata lub modyfikujących stan.
|
||||||
|
|
||||||
-- This is a powerful feature, because it's easy to run pure functions
|
|
||||||
-- concurrently; so, concurrency in Haskell is very easy.
|
|
||||||
|
|
||||||
|
-- To naprawdę użyteczna własność, dzięki której jesteśmy w stanie uruchamiać
|
||||||
|
-- czyste funkcje jednocześnie.
|
||||||
|
|
||||||
----------------------------------------------------
|
----------------------------------------------------
|
||||||
-- 9. Interaktywne środowisko programowania
|
-- 9. Interaktywne środowisko programowania
|
||||||
|
Loading…
Reference in New Issue
Block a user