Difference between revisions of "Nhc98"

From HaskellWiki
Jump to navigation Jump to search
(Here is a program to form a new list from two given lists such that a term appears in this new list iff it appeared in exactly one of those two lists. It takes O(nlog n) time.)
 
m
Line 1: Line 1:
  +
<haskell>
 
uncommon :: [Int]->[Int]->[Int]
 
uncommon :: [Int]->[Int]->[Int]
 
uncommon x y=common (finalsort (x++y))
 
uncommon x y=common (finalsort (x++y))
Line 37: Line 38:
 
lastest :: [Int]->[Int]
 
lastest :: [Int]->[Int]
 
lastest x=auxlast x (div (mylength x) 2)
 
lastest x=auxlast x (div (mylength x) 2)
  +
</haskell>

Revision as of 00:12, 18 June 2021

uncommon :: [Int]->[Int]->[Int]
uncommon x y=common (finalsort (x++y))

common :: [Int]->[Int]
common []=[]
common [x]=[x]
common (x:y:zs)
 | (x==y) =common zs
 | otherwise =x:(common (y:zs))

sort :: [Int]->[Int]->[Int]
sort [] x=x
sort x []=x
sort (x:xs) (y:ys)
 | (x>=y) =x:(sort xs (y:ys))
 | otherwise =y:(sort (x:xs) ys)

finalsort :: [Int]->[Int]
finalsort [x]=[x]
finalsort x=sort (finalsort (first x)) (finalsort (lastest x))

mylength :: [Int]->Int
mylength []=0
mylength (x:xs)=1+ mylength xs

auxfirst :: [Int]->Int->[Int]
auxfirst x 0=[]
auxfirst (y:ys) x=y:(auxfirst ys (x-1))

first :: [Int]->[Int]
first x=auxfirst x (div (mylength x) 2)

auxlast :: [Int]->Int->[Int]
auxlast x 0=x
auxlast (x:xs) y=auxlast xs (y-1)

lastest :: [Int]->[Int]
lastest x=auxlast x (div (mylength x) 2)