From c3fb66cd6dfe79d9c8357e6f65230c750f78b3a7 Mon Sep 17 00:00:00 2001 From: Remigiusz Suwalski Date: Tue, 10 Jan 2017 09:45:11 +0100 Subject: [PATCH 01/11] Initial commit --- pl-pl/haskell-pl.html.markdown | 450 +++++++++++++++++++++++++++++++++ 1 file changed, 450 insertions(+) create mode 100644 pl-pl/haskell-pl.html.markdown diff --git a/pl-pl/haskell-pl.html.markdown b/pl-pl/haskell-pl.html.markdown new file mode 100644 index 00000000..196ad9a6 --- /dev/null +++ b/pl-pl/haskell-pl.html.markdown @@ -0,0 +1,450 @@ +--- +language: Haskell +lang: pl-pl +contributors: + - ["Remigiusz Suwalski", "https://github.com/remigiusz-suwalski"] +--- + +Haskell został zaprojektowany jako praktyczy, czysto funkcyjny język +programowania. Jest znany przede wszystkim ze względu na jego monady oraz system +typów, ale ja lubię do niego wracać przez jego elegancję. Sprawił on, że +programowanie jest prawdziwą przyjemnością. + +```haskell +-- Komentarze jednolinijkowe zaczynają się od dwóch myślników +{- Komentarze wielolinijkowe należy +zamykać w bloki klamrami. +-} + +---------------------------------------------------- +-- 1. Primitive Datatypes and Operators +---------------------------------------------------- + +-- You have numbers +3 -- 3 + +-- Math is what you would expect +1 + 1 -- 2 +8 - 1 -- 7 +10 * 2 -- 20 +35 / 5 -- 7.0 + +-- Division is not integer division by default +35 / 4 -- 8.75 + +-- integer division +35 `div` 4 -- 8 + +-- Boolean values are primitives +True +False + +-- Boolean operations +not True -- False +not False -- True +1 == 1 -- True +1 /= 1 -- False +1 < 10 -- True + +-- In the above examples, `not` is a function that takes one value. +-- Haskell doesn't need parentheses for function calls...all the arguments +-- are just listed after the function. So the general pattern is: +-- func arg1 arg2 arg3... +-- See the section on functions for information on how to write your own. + +-- Strings and characters +"This is a string." +'a' -- character +'You cant use single quotes for strings.' -- error! + +-- Strings can be concatenated +"Hello " ++ "world!" -- "Hello world!" + +-- A string is a list of characters +['H', 'e', 'l', 'l', 'o'] -- "Hello" +"This is a string" !! 0 -- 'T' + + +---------------------------------------------------- +-- Lists and Tuples +---------------------------------------------------- + +-- Every element in a list must have the same type. +-- These two lists are the same: +[1, 2, 3, 4, 5] +[1..5] + +-- Ranges are versatile. +['A'..'F'] -- "ABCDEF" + +-- You can create a step in a range. +[0,2..10] -- [0, 2, 4, 6, 8, 10] +[5..1] -- This doesn't work because Haskell defaults to incrementing. +[5,4..1] -- [5, 4, 3, 2, 1] + +-- indexing into a list +[1..10] !! 3 -- 4 + +-- You can also have infinite lists in Haskell! +[1..] -- a list of all the natural numbers + +-- Infinite lists work because Haskell has "lazy evaluation". This means +-- that Haskell only evaluates things when it needs to. So you can ask for +-- the 1000th element of your list and Haskell will give it to you: + +[1..] !! 999 -- 1000 + +-- And now Haskell has evaluated elements 1 - 1000 of this list...but the +-- rest of the elements of this "infinite" list don't exist yet! Haskell won't +-- actually evaluate them until it needs to. + +-- joining two lists +[1..5] ++ [6..10] + +-- adding to the head of a list +0:[1..5] -- [0, 1, 2, 3, 4, 5] + +-- more list operations +head [1..5] -- 1 +tail [1..5] -- [2, 3, 4, 5] +init [1..5] -- [1, 2, 3, 4] +last [1..5] -- 5 + +-- list comprehensions +[x*2 | x <- [1..5]] -- [2, 4, 6, 8, 10] + +-- with a conditional +[x*2 | x <- [1..5], x*2 > 4] -- [6, 8, 10] + +-- Every element in a tuple can be a different type, but a tuple has a +-- fixed length. +-- A tuple: +("haskell", 1) + +-- accessing elements of a pair (i.e. a tuple of length 2) +fst ("haskell", 1) -- "haskell" +snd ("haskell", 1) -- 1 + +---------------------------------------------------- +-- 3. Functions +---------------------------------------------------- +-- A simple function that takes two variables +add a b = a + b + +-- Note that if you are using ghci (the Haskell interpreter) +-- You'll need to use `let`, i.e. +-- let add a b = a + b + +-- Using the function +add 1 2 -- 3 + +-- You can also put the function name between the two arguments +-- with backticks: +1 `add` 2 -- 3 + +-- You can also define functions that have no letters! This lets +-- you define your own operators! Here's an operator that does +-- integer division +(//) a b = a `div` b +35 // 4 -- 8 + +-- Guards: an easy way to do branching in functions +fib x + | x < 2 = 1 + | otherwise = fib (x - 1) + fib (x - 2) + +-- Pattern matching is similar. Here we have given three different +-- definitions for fib. Haskell will automatically call the first +-- function that matches the pattern of the value. +fib 1 = 1 +fib 2 = 2 +fib x = fib (x - 1) + fib (x - 2) + +-- Pattern matching on tuples: +foo (x, y) = (x + 1, y + 2) + +-- Pattern matching on lists. Here `x` is the first element +-- in the list, and `xs` is the rest of the list. We can write +-- our own map function: +myMap func [] = [] +myMap func (x:xs) = func x:(myMap func xs) + +-- Anonymous functions are created with a backslash followed by +-- all the arguments. +myMap (\x -> x + 2) [1..5] -- [3, 4, 5, 6, 7] + +-- using fold (called `inject` in some languages) with an anonymous +-- function. foldl1 means fold left, and use the first value in the +-- list as the initial value for the accumulator. +foldl1 (\acc x -> acc + x) [1..5] -- 15 + +---------------------------------------------------- +-- 4. More functions +---------------------------------------------------- + +-- partial application: if you don't pass in all the arguments to a function, +-- it gets "partially applied". That means it returns a function that takes the +-- rest of the arguments. + +add a b = a + b +foo = add 10 -- foo is now a function that takes a number and adds 10 to it +foo 5 -- 15 + +-- Another way to write the same thing +foo = (10+) +foo 5 -- 15 + +-- function composition +-- the operator `.` chains functions together. +-- For example, here foo is a function that takes a value. It adds 10 to it, +-- multiplies the result of that by 4, and then returns the final value. +foo = (4*) . (10+) + +-- 4*(10 + 5) = 60 +foo 5 -- 60 + +-- fixing precedence +-- Haskell has another operator called `$`. This operator applies a function +-- to a given parameter. In contrast to standard function application, which +-- has highest possible priority of 10 and is left-associative, the `$` operator +-- has priority of 0 and is right-associative. Such a low priority means that +-- the expression on its right is applied as the parameter to the function on its left. + +-- before +even (fib 7) -- false + +-- equivalently +even $ fib 7 -- false + +-- composing functions +even . fib $ 7 -- false + + +---------------------------------------------------- +-- 5. Type signatures +---------------------------------------------------- + +-- Haskell has a very strong type system, and every valid expression has a type. + +-- Some basic types: +5 :: Integer +"hello" :: String +True :: Bool + +-- Functions have types too. +-- `not` takes a boolean and returns a boolean: +-- not :: Bool -> Bool + +-- Here's a function that takes two arguments: +-- add :: Integer -> Integer -> Integer + +-- When you define a value, it's good practice to write its type above it: +double :: Integer -> Integer +double x = x * 2 + +---------------------------------------------------- +-- 6. Control Flow and If Expressions +---------------------------------------------------- + +-- if expressions +haskell = if 1 == 1 then "awesome" else "awful" -- haskell = "awesome" + +-- if expressions can be on multiple lines too, indentation is important +haskell = if 1 == 1 + then "awesome" + else "awful" + +-- case expressions: Here's how you could parse command line arguments +case args of + "help" -> printHelp + "start" -> startProgram + _ -> putStrLn "bad args" + +-- Haskell doesn't have loops; it uses recursion instead. +-- map applies a function over every element in a list + +map (*2) [1..5] -- [2, 4, 6, 8, 10] + +-- you can make a for function using map +for array func = map func array + +-- and then use it +for [0..5] $ \i -> show i + +-- we could've written that like this too: +for [0..5] show + +-- You can use foldl or foldr to reduce a list +-- foldl +foldl (\x y -> 2*x + y) 4 [1,2,3] -- 43 + +-- This is the same as +(2 * (2 * (2 * 4 + 1) + 2) + 3) + +-- foldl is left-handed, foldr is right-handed +foldr (\x y -> 2*x + y) 4 [1,2,3] -- 16 + +-- This is now the same as +(2 * 1 + (2 * 2 + (2 * 3 + 4))) + +---------------------------------------------------- +-- 7. Data Types +---------------------------------------------------- + +-- Here's how you make your own data type in Haskell + +data Color = Red | Blue | Green + +-- Now you can use it in a function: + + +say :: Color -> String +say Red = "You are Red!" +say Blue = "You are Blue!" +say Green = "You are Green!" + +-- Your data types can have parameters too: + +data Maybe a = Nothing | Just a + +-- These are all of type Maybe +Just "hello" -- of type `Maybe String` +Just 1 -- of type `Maybe Int` +Nothing -- of type `Maybe a` for any `a` + +---------------------------------------------------- +-- 8. Haskell IO +---------------------------------------------------- + +-- While IO can't be explained fully without explaining monads, +-- it is not hard to explain enough to get going. + +-- When a Haskell program is executed, `main` is +-- called. It must return a value of type `IO a` for some type `a`. For example: + +main :: IO () +main = putStrLn $ "Hello, sky! " ++ (say Blue) +-- putStrLn has type String -> IO () + +-- It is easiest to do IO if you can implement your program as +-- a function from String to String. The function +-- interact :: (String -> String) -> IO () +-- inputs some text, runs a function on it, and prints out the +-- output. + +countLines :: String -> String +countLines = show . length . lines + +main' = interact countLines + +-- You can think of a value of type `IO ()` as representing a +-- sequence of actions for the computer to do, much like a +-- computer program written in an imperative language. We can use +-- the `do` notation to chain actions together. For example: + +sayHello :: IO () +sayHello = do + putStrLn "What is your name?" + name <- getLine -- this gets a line and gives it the name "name" + putStrLn $ "Hello, " ++ name + +-- Exercise: write your own version of `interact` that only reads +-- one line of input. + +-- The code in `sayHello` will never be executed, however. The only +-- action that ever gets executed is the value of `main`. +-- To run `sayHello` comment out the above definition of `main` +-- and replace it with: +-- main = sayHello + +-- Let's understand better how the function `getLine` we just +-- used works. Its type is: +-- getLine :: IO String +-- You can think of a value of type `IO a` as representing a +-- computer program that will generate a value of type `a` +-- when executed (in addition to anything else it does). We can +-- name and reuse this value using `<-`. We can also +-- make our own action of type `IO String`: + +action :: IO String +action = do + putStrLn "This is a line. Duh" + input1 <- getLine + input2 <- getLine + -- The type of the `do` statement is that of its last line. + -- `return` is not a keyword, but merely a function + return (input1 ++ "\n" ++ input2) -- return :: String -> IO String + +-- We can use this just like we used `getLine`: + +main'' = do + putStrLn "I will echo two lines!" + result <- action + putStrLn result + putStrLn "This was all, folks!" + +-- The type `IO` is an example of a "monad". The way Haskell uses a monad to +-- do IO allows it to be a purely functional language. Any function that +-- interacts with the outside world (i.e. does IO) gets marked as `IO` in its +-- type signature. This lets us reason about what functions are "pure" (don't +-- interact with the outside world or modify state) and what functions aren't. + +-- This is a powerful feature, because it's easy to run pure functions +-- concurrently; so, concurrency in Haskell is very easy. + + +---------------------------------------------------- +-- 9. The Haskell REPL +---------------------------------------------------- + +-- Start the repl by typing `ghci`. +-- Now you can type in Haskell code. Any new values +-- need to be created with `let`: + +let foo = 5 + +-- You can see the type of any value or expression with `:t`: + +> :t foo +foo :: Integer + +-- Operators, such as `+`, `:` and `$`, are functions. +-- Their type can be inspected by putting the operator in parentheses: + +> :t (:) +(:) :: a -> [a] -> [a] + +-- You can get additional information on any `name` using `:i`: + +> :i (+) +class Num a where + (+) :: a -> a -> a + ... + -- Defined in ‘GHC.Num’ +infixl 6 + + +-- You can also run any action of type `IO ()` + +> sayHello +What is your name? +Friend! +Hello, Friend! + +``` + +There's a lot more to Haskell, including typeclasses and monads. These are the +big ideas that make Haskell such fun to code in. I'll leave you with one final +Haskell example: an implementation of a quicksort variant in Haskell: + +```haskell +qsort [] = [] +qsort (p:xs) = qsort lesser ++ [p] ++ qsort greater + where lesser = filter (< p) xs + greater = filter (>= p) xs +``` + +There are two popular ways to install Haskell: The traditional [Cabal-based installation](http://www.haskell.org/platform/), and the newer [Stack-based process](https://www.stackage.org/install). + +You can find a much gentler introduction from the excellent +[Learn you a Haskell](http://learnyouahaskell.com/) or +[Real World Haskell](http://book.realworldhaskell.org/). From fb5dac4b3cbc92abd16187974eb7bb0608f3e9be Mon Sep 17 00:00:00 2001 From: Remigiusz Suwalski Date: Tue, 10 Jan 2017 09:58:12 +0100 Subject: [PATCH 02/11] Lists and tuples --- pl-pl/haskell-pl.html.markdown | 46 +++++++++++++++++----------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/pl-pl/haskell-pl.html.markdown b/pl-pl/haskell-pl.html.markdown index 196ad9a6..cc65be27 100644 --- a/pl-pl/haskell-pl.html.markdown +++ b/pl-pl/haskell-pl.html.markdown @@ -66,45 +66,46 @@ not False -- True ---------------------------------------------------- --- Lists and Tuples +-- Listy oraz krotki ---------------------------------------------------- --- Every element in a list must have the same type. --- These two lists are the same: +-- Wszystkie elementy listy muszą być tego samego typu. +-- Poniższe dwie listy są identyczne: [1, 2, 3, 4, 5] [1..5] --- Ranges are versatile. +-- Zakresy są uniwersalne. ['A'..'F'] -- "ABCDEF" --- You can create a step in a range. +-- Przy tworzeniu zakresów można określić krok. [0,2..10] -- [0, 2, 4, 6, 8, 10] -[5..1] -- This doesn't work because Haskell defaults to incrementing. +[5..1] -- To nie zadziała, gdyż w Haskellu zakresy tworzone są domyślnie rosnąco [5,4..1] -- [5, 4, 3, 2, 1] --- indexing into a list +-- indeksowanie listy od zera [1..10] !! 3 -- 4 --- You can also have infinite lists in Haskell! -[1..] -- a list of all the natural numbers +-- Można nawet tworzyć listy nieskończone! +[1..] -- lista wszystkich liczb naturalnych --- Infinite lists work because Haskell has "lazy evaluation". This means --- that Haskell only evaluates things when it needs to. So you can ask for --- the 1000th element of your list and Haskell will give it to you: +-- Nieskończone listy mają prawo działać, ponieważ Haskell cechuje się leniwym +-- wartościowaniem. To oznacza, że obliczane są jedynie te elementy listy, +-- których istotnie potrzebujemy. Możemy poprosić o tysiączny element i +-- dostaniemy go: [1..] !! 999 -- 1000 --- And now Haskell has evaluated elements 1 - 1000 of this list...but the --- rest of the elements of this "infinite" list don't exist yet! Haskell won't --- actually evaluate them until it needs to. +-- Haskell wyznaczył pierwsze tysiąc elementów listy, ale cała jej reszta +-- jeszcze nie istnieje! Nie zostanie obliczona ich wartość, póki nie zajdzie +-- taka potrzeba. --- joining two lists +-- łączenie dwóch list [1..5] ++ [6..10] --- adding to the head of a list +-- dodawanie pojedynczego elementu na początek listy 0:[1..5] -- [0, 1, 2, 3, 4, 5] --- more list operations +-- więcej operacji na listach head [1..5] -- 1 tail [1..5] -- [2, 3, 4, 5] init [1..5] -- [1, 2, 3, 4] @@ -113,15 +114,14 @@ last [1..5] -- 5 -- list comprehensions [x*2 | x <- [1..5]] -- [2, 4, 6, 8, 10] --- with a conditional +-- z dodatkowym warunkiem [x*2 | x <- [1..5], x*2 > 4] -- [6, 8, 10] --- Every element in a tuple can be a different type, but a tuple has a --- fixed length. --- A tuple: +-- każdy element krotki może być innego typu, jednak sama krotka musi być stałej +-- długości. Przykładowo: ("haskell", 1) --- accessing elements of a pair (i.e. a tuple of length 2) +-- dostęp do elementów pary (krotki długości 2): fst ("haskell", 1) -- "haskell" snd ("haskell", 1) -- 1 From 7e3b7a5326c948ec80e79c687e462d62538fe319 Mon Sep 17 00:00:00 2001 From: Remigiusz Suwalski Date: Tue, 10 Jan 2017 10:04:14 +0100 Subject: [PATCH 03/11] Translate functions --- pl-pl/haskell-pl.html.markdown | 39 +++++++++++++++++----------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/pl-pl/haskell-pl.html.markdown b/pl-pl/haskell-pl.html.markdown index cc65be27..b747ebb3 100644 --- a/pl-pl/haskell-pl.html.markdown +++ b/pl-pl/haskell-pl.html.markdown @@ -17,53 +17,52 @@ zamykać w bloki klamrami. -} ---------------------------------------------------- --- 1. Primitive Datatypes and Operators +-- 1. Podstawowe typy danych oraz operatory ---------------------------------------------------- --- You have numbers +-- Mamy liczby 3 -- 3 --- Math is what you would expect +-- Podstawowe działania działają tak, jak powinny 1 + 1 -- 2 8 - 1 -- 7 10 * 2 -- 20 35 / 5 -- 7.0 --- Division is not integer division by default +-- dzielenie domyślnie zwraca ,,dokładny'' wynik 35 / 4 -- 8.75 --- integer division +-- dzielenie całkowitoliczbowe 35 `div` 4 -- 8 --- Boolean values are primitives +-- wartości logiczne także są podstawowym typem danych: True False --- Boolean operations +-- operacje logiczne: negacja oraz porównania not True -- False not False -- True 1 == 1 -- True 1 /= 1 -- False 1 < 10 -- True --- In the above examples, `not` is a function that takes one value. --- Haskell doesn't need parentheses for function calls...all the arguments --- are just listed after the function. So the general pattern is: --- func arg1 arg2 arg3... --- See the section on functions for information on how to write your own. +-- W powyższych przykładach, `not` jest funkcją przyjmującą jeden argument. +-- Haskell nie potrzebuje nawiasów, by wywołać funkcję: argumenty są po prostu +-- wypisywane jeden za drugim. Ogólnie wygląda to tak: +-- funkcja arg1 arg2 arg3... +-- Sekcja poświęcona funkcjom zawiera informacje, jak stworzyć własne. --- Strings and characters -"This is a string." -'a' -- character -'You cant use single quotes for strings.' -- error! +-- Łańcuchy znaków (stringi) i pojedyncze znaki: +"To jest lancuch." +'a' -- znak +'Nie mozna laczyc apostrofow z lancuchami.' -- błąd! --- Strings can be concatenated +-- Łańcuchy można sklejać "Hello " ++ "world!" -- "Hello world!" --- A string is a list of characters +-- Łańcuch jest listą własnych znaków ['H', 'e', 'l', 'l', 'o'] -- "Hello" -"This is a string" !! 0 -- 'T' - +"To jest lancuch" !! 0 -- 'T' ---------------------------------------------------- -- Listy oraz krotki From cc0bd98aeed7bb0b03b78422c63c382907eced77 Mon Sep 17 00:00:00 2001 From: Remigiusz Suwalski Date: Tue, 10 Jan 2017 10:06:27 +0100 Subject: [PATCH 04/11] Translate data types --- pl-pl/haskell-pl.html.markdown | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/pl-pl/haskell-pl.html.markdown b/pl-pl/haskell-pl.html.markdown index b747ebb3..ae44cd9a 100644 --- a/pl-pl/haskell-pl.html.markdown +++ b/pl-pl/haskell-pl.html.markdown @@ -287,29 +287,28 @@ foldr (\x y -> 2*x + y) 4 [1,2,3] -- 16 (2 * 1 + (2 * 2 + (2 * 3 + 4))) ---------------------------------------------------- --- 7. Data Types +-- 7. Typy danych ---------------------------------------------------- --- Here's how you make your own data type in Haskell +-- Oto jak tworzy się nowe typy danych w Haskellu: data Color = Red | Blue | Green --- Now you can use it in a function: - +-- Teraz można używać ich we własnych funkcjach: say :: Color -> String say Red = "You are Red!" say Blue = "You are Blue!" say Green = "You are Green!" --- Your data types can have parameters too: +-- Twoje typy danych mogą posiadać nawet parametry: data Maybe a = Nothing | Just a --- These are all of type Maybe -Just "hello" -- of type `Maybe String` -Just 1 -- of type `Maybe Int` -Nothing -- of type `Maybe a` for any `a` +-- Wszystkie poniższe są typu Maybe +Just "hello" -- typu `Maybe String` +Just 1 -- typu `Maybe Int` +Nothing -- typu `Maybe a` for any `a` ---------------------------------------------------- -- 8. Haskell IO From fd89a692838344ab3776334984a7afced5defe93 Mon Sep 17 00:00:00 2001 From: Remigiusz Suwalski Date: Tue, 10 Jan 2017 10:10:04 +0100 Subject: [PATCH 05/11] Translate type signatures --- pl-pl/haskell-pl.html.markdown | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/pl-pl/haskell-pl.html.markdown b/pl-pl/haskell-pl.html.markdown index ae44cd9a..a3c2d187 100644 --- a/pl-pl/haskell-pl.html.markdown +++ b/pl-pl/haskell-pl.html.markdown @@ -220,24 +220,26 @@ even . fib $ 7 -- false ---------------------------------------------------- --- 5. Type signatures +-- 5. Sygnatury typów ---------------------------------------------------- --- Haskell has a very strong type system, and every valid expression has a type. +-- Haskell posiada wyjątkowo silny system typów, w którym każde poprawne +-- wyrażenie ma swój typ. --- Some basic types: +-- Kilka podstawowych typów: 5 :: Integer "hello" :: String True :: Bool --- Functions have types too. --- `not` takes a boolean and returns a boolean: +-- Funkcje też są określonego typu. +-- `not` przyjmuje wartość logiczną i taką też zwraca: -- not :: Bool -> Bool --- Here's a function that takes two arguments: +-- Przykład funkcji przyjmującej dwa argumenty -- add :: Integer -> Integer -> Integer --- When you define a value, it's good practice to write its type above it: +-- Dobrą praktyką podczas definiowania wartości jest napisanie nad nią +-- także jej typu: double :: Integer -> Integer double x = x * 2 From 6fa14954a506717535f29c3ff97d9703a80ddca5 Mon Sep 17 00:00:00 2001 From: Remigiusz Suwalski Date: Tue, 10 Jan 2017 18:29:33 +0100 Subject: [PATCH 06/11] Translate Flow control --- pl-pl/haskell-pl.html.markdown | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/pl-pl/haskell-pl.html.markdown b/pl-pl/haskell-pl.html.markdown index a3c2d187..59fc090e 100644 --- a/pl-pl/haskell-pl.html.markdown +++ b/pl-pl/haskell-pl.html.markdown @@ -244,48 +244,49 @@ double :: Integer -> Integer double x = x * 2 ---------------------------------------------------- --- 6. Control Flow and If Expressions +-- 6. Wyrażenia warunkowe ---------------------------------------------------- --- if expressions -haskell = if 1 == 1 then "awesome" else "awful" -- haskell = "awesome" +-- wyrażenie warunkowe +haskell = if 1 == 1 then "wspaniale" else "paskudnie" -- haskell = "wspaniale" --- if expressions can be on multiple lines too, indentation is important +-- wyrażenie warunkowe można rozbić na wiele linii, +-- ale trzeba uważać na wcięcia w kodzie haskell = if 1 == 1 - then "awesome" - else "awful" + then "wspaniale" + else "paskudnie" --- case expressions: Here's how you could parse command line arguments +-- rozpatrywanie przypadków: oto jak można parsować argumenty z linii poleceń: case args of "help" -> printHelp "start" -> startProgram _ -> putStrLn "bad args" --- Haskell doesn't have loops; it uses recursion instead. --- map applies a function over every element in a list +-- Haskell zastępuje pętle (których nie ma) rekurencyjnymi wywołaniami funkcji. +-- map aplikuje funkcję do każdego elementu listy: map (*2) [1..5] -- [2, 4, 6, 8, 10] --- you can make a for function using map +-- możesz zdefiniować funkcję for przy użyciu map: for array func = map func array --- and then use it +-- a następnie użyć jej: for [0..5] $ \i -> show i --- we could've written that like this too: +-- mogliśmy użyć krótszego zapisu bez zmiany działania funkcji for: for [0..5] show --- You can use foldl or foldr to reduce a list +-- Do redukcji listy służy polecenie foldl (foldr): -- foldl foldl (\x y -> 2*x + y) 4 [1,2,3] -- 43 --- This is the same as +-- Jest to równoważne z: (2 * (2 * (2 * 4 + 1) + 2) + 3) --- foldl is left-handed, foldr is right-handed +-- foldl składa od od lewej strony, foldr od prawej foldr (\x y -> 2*x + y) 4 [1,2,3] -- 16 --- This is now the same as +-- To zaś równoważne jest: (2 * 1 + (2 * 2 + (2 * 3 + 4))) ---------------------------------------------------- From b80c70b4a3e4646c15f21f1c86fed9242ddbd793 Mon Sep 17 00:00:00 2001 From: Remigiusz Suwalski Date: Wed, 11 Jan 2017 10:21:38 +0100 Subject: [PATCH 07/11] Translate REPL --- pl-pl/haskell-pl.html.markdown | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/pl-pl/haskell-pl.html.markdown b/pl-pl/haskell-pl.html.markdown index 59fc090e..f2c31bb4 100644 --- a/pl-pl/haskell-pl.html.markdown +++ b/pl-pl/haskell-pl.html.markdown @@ -395,27 +395,27 @@ main'' = do ---------------------------------------------------- --- 9. The Haskell REPL +-- 9. Interaktywne środowisko programowania ---------------------------------------------------- --- Start the repl by typing `ghci`. --- Now you can type in Haskell code. Any new values --- need to be created with `let`: +-- Aby uruchomić repl (read-eval-print loop, interaktywne środowisko), należy +-- wpisać `ghci`. Można już programować. Do definiowania nowych wartości służy +-- słowo kluczowe `let`: let foo = 5 --- You can see the type of any value or expression with `:t`: +-- Do sprawdzania typów dowolnej wartości (wyrażenia) wykorzystuje się `:t`: > :t foo foo :: Integer --- Operators, such as `+`, `:` and `$`, are functions. --- Their type can be inspected by putting the operator in parentheses: +-- Działania takie jak `+`, `:` czy `$`, są funkcjami. +-- Przed sprawdzeniem ich typu należy otoczyć je nawiasami: > :t (:) (:) :: a -> [a] -> [a] --- You can get additional information on any `name` using `:i`: +-- Dodatkowych informacji dostarcza `:i`: > :i (+) class Num a where @@ -424,7 +424,7 @@ class Num a where -- Defined in ‘GHC.Num’ infixl 6 + --- You can also run any action of type `IO ()` +-- Można nawet wykonywać akcje typu `IO ()`! > sayHello What is your name? @@ -433,9 +433,9 @@ Hello, Friend! ``` -There's a lot more to Haskell, including typeclasses and monads. These are the -big ideas that make Haskell such fun to code in. I'll leave you with one final -Haskell example: an implementation of a quicksort variant in Haskell: +Pominęliśmy wiele aspektów Haskella, wliczając w to monady. To właśnie one +sprawiają, że programowanie w Haskellu sprawia tyle frajdy. Na zakończenie +pokażę Tobie implementację algorytmu quicksort w Haskellu: ```haskell qsort [] = [] @@ -444,8 +444,10 @@ qsort (p:xs) = qsort lesser ++ [p] ++ qsort greater greater = filter (>= p) xs ``` -There are two popular ways to install Haskell: The traditional [Cabal-based installation](http://www.haskell.org/platform/), and the newer [Stack-based process](https://www.stackage.org/install). +Haskell może zostać zainstalowany na co najmniej dwa sposoby: + - tradycyjnie [przy użyciu Cabala](http://www.haskell.org/platform/), + - nowocześnie [z pomocą Stack](https://www.stackage.org/install). -You can find a much gentler introduction from the excellent -[Learn you a Haskell](http://learnyouahaskell.com/) or +Godnymi poleceniami wprowadzeniami są wspaniałe +[Learn you a Haskell](http://learnyouahaskell.com/) albo [Real World Haskell](http://book.realworldhaskell.org/). From ac680b6a7fe84e55c5ee5ae77034150603f4a028 Mon Sep 17 00:00:00 2001 From: Remigiusz Suwalski Date: Fri, 13 Jan 2017 10:10:10 +0100 Subject: [PATCH 08/11] Translate Haskell IO --- pl-pl/haskell-pl.html.markdown | 62 +++++++++++++++------------------- 1 file changed, 28 insertions(+), 34 deletions(-) diff --git a/pl-pl/haskell-pl.html.markdown b/pl-pl/haskell-pl.html.markdown index f2c31bb4..23de1949 100644 --- a/pl-pl/haskell-pl.html.markdown +++ b/pl-pl/haskell-pl.html.markdown @@ -317,31 +317,29 @@ Nothing -- typu `Maybe a` for any `a` -- 8. Haskell IO ---------------------------------------------------- --- While IO can't be explained fully without explaining monads, --- it is not hard to explain enough to get going. +-- Chociaż obsługa wejścia i wyjścia nie może zostać wyjaśniona przez poznaniem +-- monad, spróbujemy zrobić to częściowo --- When a Haskell program is executed, `main` is --- called. It must return a value of type `IO a` for some type `a`. For example: +-- Wykonanie programu napisanego w Haskellu wywołuje funkcję `main` +-- Musi zwrócić wartość typu `IO a` dla pewnego `a`. Przykład: main :: IO () main = putStrLn $ "Hello, sky! " ++ (say Blue) -- putStrLn has type String -> IO () --- It is easiest to do IO if you can implement your program as --- a function from String to String. The function +-- Najłatwiej obsłużyć wejście i wyjście, kiedy program zostanie +-- zaimplementowany jako funkcja String -> String. Funkcja -- interact :: (String -> String) -> IO () --- inputs some text, runs a function on it, and prints out the --- output. +-- pobiera pewien tekst, wykonuje na nim operacje, po czym wypisuje wynik. countLines :: String -> String countLines = show . length . lines main' = interact countLines --- You can think of a value of type `IO ()` as representing a --- sequence of actions for the computer to do, much like a --- computer program written in an imperative language. We can use --- the `do` notation to chain actions together. For example: +-- Możesz myśleć o wartości typu `IO ()` jako reprezentującej ciąg czynności, +-- które komputer ma wykonać, zupełnie niczym program komputerowy w imperatywnym +-- języku programowania. Akcje można łączyć przy użyciu notacji `do`: sayHello :: IO () sayHello = do @@ -349,23 +347,20 @@ sayHello = do name <- getLine -- this gets a line and gives it the name "name" putStrLn $ "Hello, " ++ name --- Exercise: write your own version of `interact` that only reads --- one line of input. +-- Ćwiczenie: napisz własną wersję `interact`, +-- która czyta tylko jedną linię wejścia. --- The code in `sayHello` will never be executed, however. The only --- action that ever gets executed is the value of `main`. --- To run `sayHello` comment out the above definition of `main` --- and replace it with: +-- Kod w `sayHello` nigdy się nie wykona. Jedyną akcją, która zostanie +-- uruchomiona, jest wartość `main`. +-- Aby uruchomić `sayHello`, należy zastąpić poprzednią definicję `main` przez -- main = sayHello --- Let's understand better how the function `getLine` we just --- used works. Its type is: +-- Spróbujmy lepiej zrozumieć, jak działa funkcja `getLine`, której właśnie +-- użyliśmy. Jej typem jest -- getLine :: IO String --- You can think of a value of type `IO a` as representing a --- computer program that will generate a value of type `a` --- when executed (in addition to anything else it does). We can --- name and reuse this value using `<-`. We can also --- make our own action of type `IO String`: +-- Możesz myśleć o wartości typu `IO a` jako reprezentującej program, który +-- wygeneruje wartość typu `a`, poza wszystkim innym, co jeszcze zrobi. +-- Możemy także tworzyć własne akcje typu `IO String`: action :: IO String action = do @@ -376,7 +371,7 @@ action = do -- `return` is not a keyword, but merely a function 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 putStrLn "I will echo two lines!" @@ -384,15 +379,14 @@ main'' = do putStrLn result putStrLn "This was all, folks!" --- The type `IO` is an example of a "monad". The way Haskell uses a monad to --- do IO allows it to be a purely functional language. Any function that --- interacts with the outside world (i.e. does IO) gets marked as `IO` in its --- type signature. This lets us reason about what functions are "pure" (don't --- interact with the outside world or modify state) and what functions aren't. - --- This is a powerful feature, because it's easy to run pure functions --- concurrently; so, concurrency in Haskell is very easy. +-- Typ `IO` jest przykładem monady. Sposób w jakim Haskell używa monad do +-- obsługi wejścia i wyjścia pozwala mu być czysto funkcyjnym językiem. +-- Każda funkcja, która wchodzi w interakcje ze światem zewnętrznym, oznaczana +-- jest jako `IO` w jej sygnaturze typu, co umożliwia odróżnianie funkcji +-- czystych od zależnych od świata lub modyfikujących stan. +-- To naprawdę użyteczna własność, dzięki której jesteśmy w stanie uruchamiać +-- czyste funkcje jednocześnie. ---------------------------------------------------- -- 9. Interaktywne środowisko programowania From ca0d124a8e26dc0e9de2534395708ccd6bead7f2 Mon Sep 17 00:00:00 2001 From: Remigiusz Suwalski Date: Fri, 13 Jan 2017 10:31:24 +0100 Subject: [PATCH 09/11] Translate more functions --- pl-pl/haskell-pl.html.markdown | 44 +++++++++++++++++----------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/pl-pl/haskell-pl.html.markdown b/pl-pl/haskell-pl.html.markdown index 23de1949..4de76281 100644 --- a/pl-pl/haskell-pl.html.markdown +++ b/pl-pl/haskell-pl.html.markdown @@ -178,45 +178,45 @@ myMap (\x -> x + 2) [1..5] -- [3, 4, 5, 6, 7] foldl1 (\acc x -> acc + x) [1..5] -- 15 ---------------------------------------------------- --- 4. More functions +-- 4. Więcej funkcji ---------------------------------------------------- --- partial application: if you don't pass in all the arguments to a function, --- it gets "partially applied". That means it returns a function that takes the --- rest of the arguments. +-- częściowe nakładanie: jeśli funkcja nie otrzyma wszystkich swoich argumentów, +-- zostaje cześciowo nałożona - zwraca funkcję, która przyjmuje pozostałe, +-- brakujące argumenty. add a b = a + b -foo = add 10 -- foo is now a function that takes a number and adds 10 to it +foo = add 10 -- foo jest teraz funkcją, która przyjmuje liczbę, zwiększa ją o 10 foo 5 -- 15 --- Another way to write the same thing +-- Inny sposób na zapisanie tego samego: foo = (10+) foo 5 -- 15 --- function composition --- the operator `.` chains functions together. --- For example, here foo is a function that takes a value. It adds 10 to it, --- multiplies the result of that by 4, and then returns the final value. +-- składanie funkcji: +-- operator `.` składa wiele funkcji w jedną. +-- Dla przykładu, foo jest funkcją, która powiększa swój argument o 10, mnoży +-- tak uzyskaną liczbę przez 4 i zwraca wynik: foo = (4*) . (10+) -- 4*(10 + 5) = 60 foo 5 -- 60 --- fixing precedence --- Haskell has another operator called `$`. This operator applies a function --- to a given parameter. In contrast to standard function application, which --- has highest possible priority of 10 and is left-associative, the `$` operator --- has priority of 0 and is right-associative. Such a low priority means that --- the expression on its right is applied as the parameter to the function on its left. +-- ustalanie kolejności +-- Haskell posiada inny operator, `$`, który nakłada funkcję do podanego +-- parametru. W przeciwieństwie do zwykłego lewostronnie łącznego nakładania +-- funkcji, którego priorytet jest najwyższy (10), operator `$` posiada +-- priorytet 0 i jest prawostronnie łączny. Tak niski priorytet oznacza, że +-- wyrażenie po prawej traktowane jest jako parametr funkcji po lewej --- before -even (fib 7) -- false +-- wcześniej +even (fib 7) -- fałsz --- equivalently -even $ fib 7 -- false +-- równoważnie +even $ fib 7 -- fałsz --- composing functions -even . fib $ 7 -- false +-- składanie funkcji +even . fib $ 7 -- fałsz ---------------------------------------------------- From 5a1df9e1dabda88d1fd83aaa2fc8e6209dd275e0 Mon Sep 17 00:00:00 2001 From: Remigiusz Suwalski Date: Fri, 13 Jan 2017 10:47:08 +0100 Subject: [PATCH 10/11] Translate functions --- pl-pl/haskell-pl.html.markdown | 43 ++++++++++++++++------------------ 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/pl-pl/haskell-pl.html.markdown b/pl-pl/haskell-pl.html.markdown index 4de76281..4c6b0243 100644 --- a/pl-pl/haskell-pl.html.markdown +++ b/pl-pl/haskell-pl.html.markdown @@ -125,56 +125,53 @@ fst ("haskell", 1) -- "haskell" snd ("haskell", 1) -- 1 ---------------------------------------------------- --- 3. Functions +-- 3. Funkcje ---------------------------------------------------- --- A simple function that takes two variables +-- Prosta funkcja przyjmująca dwa argumenty add a b = a + b --- Note that if you are using ghci (the Haskell interpreter) --- You'll need to use `let`, i.e. +-- Pamiętaj, że podczas stosowania ghci, interpretera Haskella, wszelkie +-- definicje muszą zostać poprzedzone słowem `let`, na przykład: -- let add a b = a + b --- Using the function +-- Używanie funkcji: add 1 2 -- 3 --- You can also put the function name between the two arguments --- with backticks: +-- Nazwę funkcji można podać między dwoma argumentami, ale wtedy musi zostać +-- otoczona grawisami: 1 `add` 2 -- 3 --- You can also define functions that have no letters! This lets --- you define your own operators! Here's an operator that does --- integer division +-- Nazwa funkcji nie musi zawierać żadnych liter, przykładem czego jest +-- operator dzielenia: (//) a b = a `div` b 35 // 4 -- 8 --- Guards: an easy way to do branching in functions +-- Strażnicy: prosty sposób na rozbijanie funkcji na przypadki fib x | x < 2 = 1 | otherwise = fib (x - 1) + fib (x - 2) --- Pattern matching is similar. Here we have given three different --- definitions for fib. Haskell will automatically call the first --- function that matches the pattern of the value. +-- Dopasowanie wzorca jest podobne. Haskell sam automatycznie wybierze, która +-- z poniższych definicji fib powinna zostać użyta: fib 1 = 1 fib 2 = 2 fib x = fib (x - 1) + fib (x - 2) --- Pattern matching on tuples: +-- Dopasowanie z krotkami: foo (x, y) = (x + 1, y + 2) --- Pattern matching on lists. Here `x` is the first element --- in the list, and `xs` is the rest of the list. We can write --- our own map function: +-- Dopasowanie z listami. Tutaj `x` jest pierwszym elementem listy, +-- natomiast `xs` to jej reszta (ogon). Poniższa funkcja nakłada funkcję +-- na każdy z elementów listy: myMap func [] = [] myMap func (x:xs) = func x:(myMap func xs) --- Anonymous functions are created with a backslash followed by --- all the arguments. +-- Funkcje anonimowe tworzone są przy użyciu w-tył-ciachu, po którym następują +-- wszystkie argumenty: myMap (\x -> x + 2) [1..5] -- [3, 4, 5, 6, 7] --- using fold (called `inject` in some languages) with an anonymous --- function. foldl1 means fold left, and use the first value in the --- list as the initial value for the accumulator. +-- używanie zwijania z anonimowymi funkcjami: foldl1 zwija z lewej strony, +-- przyjmując jako wartość początkową zbieracza pierwszy element listy. foldl1 (\acc x -> acc + x) [1..5] -- 15 ---------------------------------------------------- From 5ef6b1074930c552b1dd4a30e556f4e3ffd0e396 Mon Sep 17 00:00:00 2001 From: Remigiusz Suwalski Date: Fri, 13 Jan 2017 18:05:03 +0100 Subject: [PATCH 11/11] Add a missing letter --- pl-pl/haskell-pl.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pl-pl/haskell-pl.html.markdown b/pl-pl/haskell-pl.html.markdown index 4c6b0243..5eb68e2e 100644 --- a/pl-pl/haskell-pl.html.markdown +++ b/pl-pl/haskell-pl.html.markdown @@ -5,7 +5,7 @@ contributors: - ["Remigiusz Suwalski", "https://github.com/remigiusz-suwalski"] --- -Haskell został zaprojektowany jako praktyczy, czysto funkcyjny język +Haskell został zaprojektowany jako praktyczny, czysto funkcyjny język programowania. Jest znany przede wszystkim ze względu na jego monady oraz system typów, ale ja lubię do niego wracać przez jego elegancję. Sprawił on, że programowanie jest prawdziwą przyjemnością.