Код из лекции
This commit is contained in:
@@ -3,4 +3,4 @@ module Main (main) where
|
||||
import Lib
|
||||
|
||||
main :: IO ()
|
||||
main = someFunc
|
||||
main = putStrLn "temp"
|
||||
|
||||
@@ -1,6 +1,50 @@
|
||||
module Lib
|
||||
( someFunc
|
||||
) where
|
||||
{-# LANGUAGE InstanceSigs #-}
|
||||
|
||||
someFunc :: IO ()
|
||||
someFunc = putStrLn "someFunc"
|
||||
module Lib where
|
||||
|
||||
import Control.Applicative (Alternative(..))
|
||||
import Data.Char (digitToInt, isDigit)
|
||||
|
||||
|
||||
newtype Parser tok a =
|
||||
Parser { runParser :: [tok] -> Maybe ([tok], a)}
|
||||
|
||||
instance Functor (Parser tok) where
|
||||
fmap :: (a -> b) -> Parser tok a -> Parser tok b
|
||||
fmap g (Parser p) = Parser f where
|
||||
f xs = case p xs of
|
||||
Nothing -> Nothing
|
||||
Just (cs, c) -> Just (cs, g c)
|
||||
|
||||
instance Applicative (Parser tok) where
|
||||
pure :: a -> Parser tok a
|
||||
pure x = Parser $ \toks -> Just (toks, x)
|
||||
|
||||
(<*>) :: Parser tok (a -> b) -> Parser tok a -> Parser tok b
|
||||
Parser u <*> Parser v = Parser f where
|
||||
f xs = case u xs of
|
||||
Nothing -> Nothing
|
||||
Just (xs', g) -> case v xs' of
|
||||
Nothing -> Nothing
|
||||
Just (xs'', x) -> Just (xs'', g x)
|
||||
|
||||
instance Alternative (Parser tok) where
|
||||
empty :: Parser tok a
|
||||
empty = Parser $ \_ -> Nothing
|
||||
|
||||
(<|>) :: Parser tok a -> Parser tok a -> Parser tok a
|
||||
Parser u <|> Parser v = Parser f where
|
||||
f xs = case u xs of
|
||||
Nothing -> v xs
|
||||
z -> z
|
||||
|
||||
satisfy :: (tok -> Bool) -> Parser tok tok
|
||||
satisfy pr = Parser f where
|
||||
f (c:cs) | pr c = Just (cs, c)
|
||||
f _ = Nothing
|
||||
|
||||
char :: Char -> Parser Char Char
|
||||
char c = satisfy (== c)
|
||||
|
||||
digit :: Parser Char Char
|
||||
digit = satisfy isDigit
|
||||
|
||||
Reference in New Issue
Block a user