asdf

asdf 2018-04-27 16:18:35.507381 UTC

1import Data.List
2import System.IO
3
4substitute :: Eq a => a -> [a] -> [a] -> [a]
5substitute _ [] _ = []
6substitute wildcard (x:xs) s =
7 if x == wildcard
8 then s ++ substitute wildcard xs s
9 else x:substitute wildcard xs s
10
11subAB = substitute '*' "* asdf *" "ITS WORKING"
12
13match :: Eq a => a -> [a] -> [a] -> Maybe [a]
14match _ [] [] = Just []
15match _ [] _ = Nothing
16match _ _ [] = Nothing
17match wc (x:xs) (y:ys) =
18 if x == wc
19 then
20 wildcardMatch (x:xs) (y:ys)
21 else
22 if (x==y)
23 then match wc xs ys
24 else Nothing
25
26
27-- Helper function to match
28--singleWildcardMatch,
29wildcardMatch :: Eq a => [a] -> [a] -> Maybe [a]
30
31{-singleWildcardMatch (wc:ps) (x:xs) =
32 if ps == xs
33 then Just [x]
34 else Nothing
35-}
36
37wildcardMatch (t:tx) (s:sx) = wildcardMatchPart tx (s:sx) []
38
39wildcardMatchPart :: Eq a => [a] -> [a] -> [a] -> Maybe[a]
40wildcardMatchPart _ [] _ = Nothing
41wildcardMatchPart t (s:sx) r =
42 if t==sx
43 then Just (r++[s])
44 else wildcardMatchPart t sx (r++[s])
45
46
47testPattern = "a=*;"
48testSubstitutions = "32"
49testString = "a=32;"
50
51substituteTest = substitute '*' testPattern testSubstitutions
52substituteCheck = substituteTest == testString
53
54matchTest = match '*' testPattern testString
55matchCheck = matchTest == Just testSubstitutions
56
57
58orElse :: Maybe a -> Maybe a -> Maybe a
59orElse Nothing x = x
60orElse (Just a) _ = Just a
61
62testa = orElse Nothing (wildcardMatch "*abc" "asdfbc")
63
64-------------------------------------------------------
65-- Applying patterns
66--------------------------------------------------------
67
68-- Applying a single pattern
69transformationApply :: Eq a => a -> ([a] -> [a]) -> [a] -> ([a], [a]) -> Maybe [a]
70transformationApply _ _ _ _ = Nothing
71{- TO BE WRITTEN -}
72
73
74-- Applying a list of patterns until one succeeds
75transformationsApply :: Eq a => a -> ([a] -> [a]) -> [([a], [a])] -> [a] -> Maybe [a]
76transformationsApply _ _ _ _ = Nothing
77{- TO BE WRITTEN -}