mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-04-27 15:43:58 +00:00
Translate functions
This commit is contained in:
parent
ca0d124a8e
commit
5a1df9e1da
@ -125,56 +125,53 @@ fst ("haskell", 1) -- "haskell"
|
|||||||
snd ("haskell", 1) -- 1
|
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
|
add a b = a + b
|
||||||
|
|
||||||
-- Note that if you are using ghci (the Haskell interpreter)
|
-- Pamiętaj, że podczas stosowania ghci, interpretera Haskella, wszelkie
|
||||||
-- You'll need to use `let`, i.e.
|
-- definicje muszą zostać poprzedzone słowem `let`, na przykład:
|
||||||
-- let add a b = a + b
|
-- let add a b = a + b
|
||||||
|
|
||||||
-- Using the function
|
-- Używanie funkcji:
|
||||||
add 1 2 -- 3
|
add 1 2 -- 3
|
||||||
|
|
||||||
-- You can also put the function name between the two arguments
|
-- Nazwę funkcji można podać między dwoma argumentami, ale wtedy musi zostać
|
||||||
-- with backticks:
|
-- otoczona grawisami:
|
||||||
1 `add` 2 -- 3
|
1 `add` 2 -- 3
|
||||||
|
|
||||||
-- You can also define functions that have no letters! This lets
|
-- Nazwa funkcji nie musi zawierać żadnych liter, przykładem czego jest
|
||||||
-- you define your own operators! Here's an operator that does
|
-- operator dzielenia:
|
||||||
-- integer division
|
|
||||||
(//) a b = a `div` b
|
(//) a b = a `div` b
|
||||||
35 // 4 -- 8
|
35 // 4 -- 8
|
||||||
|
|
||||||
-- Guards: an easy way to do branching in functions
|
-- Strażnicy: prosty sposób na rozbijanie funkcji na przypadki
|
||||||
fib x
|
fib x
|
||||||
| x < 2 = 1
|
| x < 2 = 1
|
||||||
| otherwise = fib (x - 1) + fib (x - 2)
|
| otherwise = fib (x - 1) + fib (x - 2)
|
||||||
|
|
||||||
-- Pattern matching is similar. Here we have given three different
|
-- Dopasowanie wzorca jest podobne. Haskell sam automatycznie wybierze, która
|
||||||
-- definitions for fib. Haskell will automatically call the first
|
-- z poniższych definicji fib powinna zostać użyta:
|
||||||
-- function that matches the pattern of the value.
|
|
||||||
fib 1 = 1
|
fib 1 = 1
|
||||||
fib 2 = 2
|
fib 2 = 2
|
||||||
fib x = fib (x - 1) + fib (x - 2)
|
fib x = fib (x - 1) + fib (x - 2)
|
||||||
|
|
||||||
-- Pattern matching on tuples:
|
-- Dopasowanie z krotkami:
|
||||||
foo (x, y) = (x + 1, y + 2)
|
foo (x, y) = (x + 1, y + 2)
|
||||||
|
|
||||||
-- Pattern matching on lists. Here `x` is the first element
|
-- Dopasowanie z listami. Tutaj `x` jest pierwszym elementem listy,
|
||||||
-- in the list, and `xs` is the rest of the list. We can write
|
-- natomiast `xs` to jej reszta (ogon). Poniższa funkcja nakłada funkcję
|
||||||
-- our own map function:
|
-- na każdy z elementów listy:
|
||||||
myMap func [] = []
|
myMap func [] = []
|
||||||
myMap func (x:xs) = func x:(myMap func xs)
|
myMap func (x:xs) = func x:(myMap func xs)
|
||||||
|
|
||||||
-- Anonymous functions are created with a backslash followed by
|
-- Funkcje anonimowe tworzone są przy użyciu w-tył-ciachu, po którym następują
|
||||||
-- all the arguments.
|
-- wszystkie argumenty:
|
||||||
myMap (\x -> x + 2) [1..5] -- [3, 4, 5, 6, 7]
|
myMap (\x -> x + 2) [1..5] -- [3, 4, 5, 6, 7]
|
||||||
|
|
||||||
-- using fold (called `inject` in some languages) with an anonymous
|
-- używanie zwijania z anonimowymi funkcjami: foldl1 zwija z lewej strony,
|
||||||
-- function. foldl1 means fold left, and use the first value in the
|
-- przyjmując jako wartość początkową zbieracza pierwszy element listy.
|
||||||
-- list as the initial value for the accumulator.
|
|
||||||
foldl1 (\acc x -> acc + x) [1..5] -- 15
|
foldl1 (\acc x -> acc + x) [1..5] -- 15
|
||||||
|
|
||||||
----------------------------------------------------
|
----------------------------------------------------
|
||||||
|
Loading…
Reference in New Issue
Block a user