mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-05-05 06:18:32 +00:00
Translate data types
This commit is contained in:
parent
7e3b7a5326
commit
cc0bd98aee
@ -287,29 +287,28 @@ foldr (\x y -> 2*x + y) 4 [1,2,3] -- 16
|
|||||||
(2 * 1 + (2 * 2 + (2 * 3 + 4)))
|
(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
|
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 :: Color -> String
|
||||||
say Red = "You are Red!"
|
say Red = "You are Red!"
|
||||||
say Blue = "You are Blue!"
|
say Blue = "You are Blue!"
|
||||||
say Green = "You are Green!"
|
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
|
data Maybe a = Nothing | Just a
|
||||||
|
|
||||||
-- These are all of type Maybe
|
-- Wszystkie poniższe są typu Maybe
|
||||||
Just "hello" -- of type `Maybe String`
|
Just "hello" -- typu `Maybe String`
|
||||||
Just 1 -- of type `Maybe Int`
|
Just 1 -- typu `Maybe Int`
|
||||||
Nothing -- of type `Maybe a` for any `a`
|
Nothing -- typu `Maybe a` for any `a`
|
||||||
|
|
||||||
----------------------------------------------------
|
----------------------------------------------------
|
||||||
-- 8. Haskell IO
|
-- 8. Haskell IO
|
||||||
|
Loading…
Reference in New Issue
Block a user