Sei sulla pagina 1di 3

module Excercise2

where
{-Write a function which given a number and a list of numbers returns
those numbers greater than the first number: greaterinlist:: Integer -
> [Integer] -> [Integer].-}
greaterinlist:: Integer -> [Integer] -> [Integer]
greaterinlist x xs = [y|y<-xs,y>x]
{--Write a function to determine whether its first argument, a list of
integers, is lexicographically larger than its second argument: lexInt::
[Int] -> [Int] -> Bool.-}
lexInt::[Int] -> [Int] -> Bool
lexInt [] [] = False
lexInt [] xs = False
lexInt ys [] = True
lexInt (x:xs) (y:ys) =if show x > show y then True
else
if show x < show y then False
else lexInt xs ys
{-Write a function which splits a list into two lists, the first containing
the odd indexed elements and the second containing the even indexed
elements: msplit:: [a] -> ([a],[a]).-}
{-- my solution was to creat a subfuction fs that recursively put elements from
the first list
into two other list, switching the two list at each call.
Then , to obey the specification that odd indexed elements be in the first list
i do a simple
count of the lenght of input list, chech if it is odd or even, then, switch the
result of function fs-}
rev::([a],[a])->([a],[a])
rev (xs,ys)=(ys,xs)
fs::([a],[a],[a])->([a],[a])
fs ([],xs,ys) = (xs,ys)
fs (z:zs,xs,ys) = fs(zs,ys,z:xs)
msplit:: [a] -> ([a],[a])
--The first containing the odd indexed elements and the second containing the ev
en indexed
msplit xs = if (odd (length xs )) then
rev (fs(xs,[],[]))
else
fs(xs,[],[])
{--Write a function to merge two lists of integers, taking the least first
element at each step: mergeInt :: ([Integer],[Integer]) -> [Integer] .-}
mergeInt :: ([Integer],[Integer]) -> [Integer]
mergeInt ([],xs) = xs
mergeInt (xs,[]) = xs
mergeInt (x:xs,y:ys)= if x<y then
[x]++mergeInt(xs,y:ys)
else
[y]++mergeInt(x:xs,ys)
{-Given a relation rel:: a -> b -> Bool and a list of a-elements and a list
of b-elements write a function which returns a list of pairs of an aelement
and the list of b-elements from the second list to which it is
related: relgrp:: (a -> b -> Bool) -> [a] -> [b] -> [(a,[b])]. For example
if the relation is the divide relation then relgrp div [2,3] [1,2,3,4,5,6]
= [(2,[2,4,6]),(3,[3,6])]].-}
relgrp::(a->b->Bool)->[a]->[b]->[(a,[b])]
relgrp f xs ys =[(x,[y|y<-ys,f x y ])|x<-xs]
{-Program the "group" function: given a predicate pred:: a -> a -> Bool
and a list the group function breaks the list into a series of (maximal)
sublists such that any two consecutive elements satisfy the predicate
pred. The type of the function group is group:: (a -> a -> Bool) -> [a]
-> [[a]]. An example of its use is as follows: suppose that the
predicate nbr determines whether the absolute difference of two
integers is at most 1 (i.e. they are equal or they differ by one) then
group nbr [2,1,3,4,5,5,4,7,4,3,3] = [[2,1],[3,4,5,5,4],[7],[4,3,3]] :
program up this example to make sure your group works.-}
{--My solution uses a special list of list operator *++ which concatenates
two list of list. The twist is that it concatenates the last inner list of
the first input and the first inner list of the second input -}
group::(a->a->Bool)->[a]->[[a]]
group _ [] =[]
group p (x:[]) =[[x]]
group p (x:xs)= if p x (head xs) then [[x]] *++ (group p xs)
else [[x]]++ group p xs

(*++)::[[a]]->[[a]]->[[a]]
(*++) [] yss = yss
(*++) xss [] = xss
(*++) xss yss =init xss ++ [last xss++head yss]++tail yss
nbr:: Int->Int -> Bool
nbr x y
| x==y = True
|x==(y+1)=True
|x==(y-1)=True
|otherwise=False
{-Write a function which given a list returns a list of all the subsets of
the list: subset:: [a] -> [[a]].-}
{--i think this means the same as the definition of powerset. Both the
input and the output could be infinite, so i propose this algorithm that uses
lazy iteration to get usefull answers. Also it is defined as a mutually recursiv
e fucntion}
This fuction is less usefull in text processing as the list is considered a set.
-}
subset:: [a] -> [[a]]
subset []= [[]]
subset (x:[])= [[x]]
subset (x:xs)= [[x]]++subsetF x xs
subsetF::a->[a]->[[a]]
subsetF x [] = [[x]]
subsetF x xs = subset xs ++ [x:y|y<-(subset xs)]
{-
Write a function to turn decimal numbers into roman numerals and a
function for the reverse translation.
-}
{--We restrict ourselves to the limit of 3,999==MMMCMXCIX which is the largest
number without using bars or ()
fromRome::String->Int
--}
romans= [(1,"I"),(4,"IV"),(5,"V"),(9,"IX"),(10,"X"),(40,"XL"),(50,"L"),(90,"XC")
,(100,"C"),(400,"CD"),(500,"D"),(900,"CM"),(1000,"M")]
toRome :: Integer->String
toRome x
|x== 0 =""
|x > 0 = snd(last [ y |y<-romans,(fst y)<=x]) ++ toRome( x - fst (last [ y |y<
-romans,(fst y)<=x]))
fromRome::String->Integer
fromRome []=0
fromRome (x:[])= fst (head [y|y<-romans,(snd y)==[x]])
fromRome (x:xs)= if fromRome (x:[]) >= fromRome ((head xs):[]) then
fst (head [y|y<-romans,(snd y)==[x]]) + fromRome xs
else
-fst (head [y|y<-romans,(snd y)==[x]])+ fst (head [y|y<-roma
ns,(snd y)==[head xs]]) + fromRome(tail xs)

Potrebbero piacerti anche