I come from an object oriented background(C, c++, java) so I am used to being able to read a line of code in english to understand what it is doing.
I am currently doing an adendum to a test in order to recieve extra credit. I have a couple lines of haskell I can't figure out how to read so I cant figure out what they do. I am hoping some one can make them into english sentences.
an example of what I mean as an english sentence would be:
i = i + 1
i is equal to the contents of i + 1.
or
sul ys = all (`elem` ys)
the function sul checks all elements of ys for a conditon while checking that a specific element is found in ys, then returning true or false(at least I think thats what it does, the teacher said to ignore the fact that all and elem
both require two arguments.
the few pieces of code I can't understand are:
twasf p = foldr clip [] where
clip x xs | p x = x : xs
| otherwise = []
(I dont know how to translate the "|")
infixl 5 <*>
xs <*> ys = zipWith ($) xs ys
(I have NO idea how to say that)
rd [] = []
rd (a:as) = if a `elem` rd as then as else a : rd as
(I think this is "Creates an empty list rd, then checks for a in as. if its found then return as, otherwise push a onto the front and return as)
Any help at all would be appreciated. Very willing to read other websites if someone can point me at a website that helps you translate the language.
Part one:
clip x xs | p x = x : xs
| otherwise = []
clip: If p(x) holds, return x:xs, otherwise return the empty list.
xs <*> ys = zipWith ($) xs ys
<*>
applies a list of functions to a list of values.
rd [] = []
rd (a:as) = if a `elem` rd as then as else a : rd as
Word per word:
If the list is empty, return the empty list. Else, check if the first element of the list is contained more than once, if so remove it and return the rest of the list. Else return the first element and apply the function recursive to the rest.
Haskell is purely functional, so you can't really say "creates, then checks and does such and such". Substantives apply better.
For the last example I guess rd
is for removing duplicates, so I'd say the duplicate removal…
of the empty list: is the empty list;
of an a:as
list: is the remainder as
if a
was in as
, or a
followed by the recursive duplicate removal otherwise.
BTW. shouldn't last line be …if a elem as then rd as else
… ?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With