diff --git a/1.html b/1.html index f5f79b0..64d490c 100644 --- a/1.html +++ b/1.html @@ -666,6 +666,52 @@

filter тоже есть в стандартной библиотеке.

+
+

Еще немного структур

+
+
+ +

Параметрмческий полиморфизм

+

(Дженерики)

+
data IntList = Cons Int IntList | Nil
+ +
data List a = Cons a (List a) | Nil
+
ints :: List Int
+ints = Cons 1 (Cons 2 (Cons 3 Nil))
+
-- Типы как всегда можно не писать
+strings :: List String
+strings = Cons "one" (Cons "two" (Cons "three" Nil))
+
strings :: List String
+strings = Cons "one" (Cons "two" (Cons "three" Nil))
+
+
+ +
things = Cons ("one" :: String) (Cons (2 :: Int) Nil)
+
    • Couldn't match type ‘Int’ with ‘String’
+      Expected type: List String
+        Actual type: List Int
+   |
+10 | things = Cons ("one" :: String) (Cons (2 :: Int) Nil)
+   |                                  ^^^^^^^^^^^^^^^^^^^
+
data List a = Cons a (List a) | Nil
+

a должен всегда быть a.

+
+
+ +
take :: Int -> IntList -> IntList
+take _ Nil = Nil
+take 0 _ = Nil
+take n (Cons x xs) = Cons x (take (n - 1) xs)
+
take :: Int -> List a -> List a
+take _ Nil = Nil
+take 0 _ = Nil
+take n (Cons x xs) = Cons x (take (n - 1) xs)
+ +

Где и как смотреть “стандартную библиотеку”

diff --git a/1.md b/1.md index 75f27d7..ddaf670 100644 --- a/1.md +++ b/1.md @@ -958,6 +958,85 @@ quicksort [2, 1, 3, 4] --- +# Еще немного структур + +--- + +## Параметрмческий полиморфизм + +### (Дженерики) + +```{ .haskell .fragment } +data IntList = Cons Int IntList | Nil +``` + +:::notes +Мы научиоись делать список интов. А что если мы хотим сделать просто список. Чего-нибудь. +::: + +```{ .haskell .fragment } +data List a = Cons a (List a) | Nil +``` + +```{ .haskell .fragment } +ints :: List Int +ints = Cons 1 (Cons 2 (Cons 3 Nil)) +``` + +```{ .haskell .fragment } +-- Типы как всегда можно не писать +strings :: List String +strings = Cons "one" (Cons "two" (Cons "three" Nil)) +``` + +```{ .haskell .fragment } +strings :: List String +strings = Cons "one" (Cons "two" (Cons "three" Nil)) +``` + +--- + +```{ .haskell } +things = Cons ("one" :: String) (Cons (2 :: Int) Nil) +``` + +```fragment + • Couldn't match type ‘Int’ with ‘String’ + Expected type: List String + Actual type: List Int + | +10 | things = Cons ("one" :: String) (Cons (2 :: Int) Nil) + | ^^^^^^^^^^^^^^^^^^^ +``` + +```{ .haskell .fragment } +data List a = Cons a (List a) | Nil +``` + +[`a` должен всегда быть `a`.]{.fragment} + +--- + +```{ .haskell } +take :: Int -> IntList -> IntList +take _ Nil = Nil +take 0 _ = Nil +take n (Cons x xs) = Cons x (take (n - 1) xs) +``` + +```{ .haskell .fragment } +take :: Int -> List a -> List a +take _ Nil = Nil +take 0 _ = Nil +take n (Cons x xs) = Cons x (take (n - 1) xs) +``` + +:::notes +Поменялся только тип! +::: + +--- + # Где и как смотреть "стандартную библиотеку" --- diff --git a/README.md b/README.md index 7dcb390..71b8389 100644 --- a/README.md +++ b/README.md @@ -11,3 +11,4 @@ - Currying - Lists - Quicksort + - Parametric polymorphism in structures diff --git a/build.sh b/build.sh index 6f18ec8..0f9ac09 100755 --- a/build.sh +++ b/build.sh @@ -3,7 +3,7 @@ for file in *.md do base=$(basename $file .md) - if [base != "README"] + if [ $base != "README" ] then pandoc -i $file -t revealjs -s --highlight-style=breezeDark --include-in-header=style.html >"$base.html" fi