Применить функцию к каждому элементу списка - это просто:
map _ [] = []
map f (x:xs) = f x : map f xs
{-# LANGUAGE LambdaCase, BlockArguments, EmptyCase #-} | |
{-# LANGUAGE DeriveGeneric #-} | |
{-# LANGUAGE TypeSynonymInstances, TypeOperators, FlexibleInstances, FlexibleContexts, UndecidableInstances #-} | |
{-# LANGUAGE TypeFamilies, GADTs, MultiParamTypeClasses, FunctionalDependencies #-} | |
{-# LANGUAGE DataKinds, PolyKinds, AllowAmbiguousTypes, TypeApplications, ScopedTypeVariables #-} | |
import GHC.Generics | |
import Unsafe.Coerce (unsafeCoerce) | |
import Data.Type.Equality |
{-# language RecordWildCards #-} | |
module Main where | |
import Data.List | |
import GHC.HeapView | |
import System.Mem | |
import qualified Data.IntMap as IntMap | |
main = do |
{-# LANGUAGE RankNTypes, TypeInType, TypeApplications, OverloadedLabels, | |
ScopedTypeVariables, TypeOperators, GADTs, FlexibleInstances, FlexibleContexts, | |
TypeFamilies, UndecidableInstances #-} | |
import Data.Kind (Type) | |
import Data.Proxy (Proxy(..)) | |
import GHC.Generics | |
import GHC.TypeLits (Symbol, KnownSymbol, symbolVal) | |
import Control.Lens | |
import Data.Generics.Product.Fields (field') |
Приближается, пожалуй, самое значительное нововведение в хаскеле, со времен FC и превращения хаскеля из ML++ в недоΩmega: модули. Весь этот тектонический сдвиг, правда, остается незамеченным. Даже в Release notes об этом не упомянуто. Есть, только упоминание в руководстве пользователя Также, описания новой системы модулей можно найти на странице Backpack, но установить что из этого уже имплементировано можно только опытным путем.
Представление о ML-модулях можно составить из диссертации Дрейера (pdf)
> import GHC.HeapView | |
> | |
let evLength n l = do | |
ht <- buildHeapTree n (asBox $ l) | |
let cnt (HeapTree _ (ConsClosure {ptrArgs = [hd,tl]})) = 1 + cnt tl | |
cnt _ = 0 | |
return $ cnt ht | |
> let l = [1..] | |
> l !! 10 | |
11 |
module Parsers = | |
let inline konst a b = a //const зарезервировано | |
type Parser<'s, 't> = 's -> ('t * 's) option | |
let inline uncons l = match l with x::xs -> Some(x,xs) | [] -> None | |
let inline unit a xs = Some (a, xs) | |
let inline (<*>) pf px inp = | |
match pf inp with |
{-# LANGUAGE FlexibleInstances #-} | |
import Control.Applicative | |
newtype J a = J { runJ :: a } | |
instance (Applicative f, Num n) => Num (J(f n)) where | |
(+) = (J .) . liftA2 (+) `on` runJ | |
(-) = (J .) . liftA2 (-) `on` runJ | |
(*) = (J .) . liftA2 (*) `on` runJ | |
abs = J . fmap abs . runJ |
Prelude> :set -XDataKinds | |
Prelude> :set -XKindSignatures | |
Prelude> :set -XScopedTypeVariables | |
Prelude> import Data.Fixed | |
Prelude Data.Fixed> import GHC.TypeLits | |
Prelude Data.Fixed GHC.TypeLits> | |
data E (a :: Nat) | |
Prelude Data.Fixed GHC.TypeLits> | |
instance SingI a => HasResolution (E a) where resolution _ = 10 ^ fromSing (sing :: Sing a) |
{-# LANGUAGE TypeFamilies, DataKinds, GADTs, NoMonomorphismRestriction #-} | |
-- http://typesandkinds.wordpress.com/2012/11/26/variable-arity-zipwith/ | |
module Main where | |
import Prelude hiding (map, zipWith, zipWith3) | |
import Control.Applicative hiding (liftA, liftA2, liftA3) | |
import Control.Monad hiding (liftM, liftM2, liftM3) |