Learning to read haskell in english
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 t开发者_开发知识库hat)
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 remainderas
ifa
was inas
, ora
followed by the recursive duplicate removal otherwise.
BTW. shouldn't last line be …if a elem as then rd as else
… ?
Let's look at these one-at-a-time.
The pipe syntax indicates a guard, which you can think of as similar to an if-else ladder. I would read it as "If
p
ofx
is true, then returnx
concatenated ontoxs
, otherwise return the empty list".The first line says "
<*>
is an left-associative infix operator with precedence 5" (you'll have to come up with your own pronunciation for<*>
- often in this sort of thing I'd just think of it as "op"). The next line is "xs op ys is the same as xs zipped with ys, using the function application operator$
".This is an example of a function having multiple definitions. The way it works is that if the first "pattern" of arguments matches, that definition is used, and if not, it falls down to any other definitions. In this case, I would read it as "rd of an empty list is an empty list, otherwise (etc.)"
You'd probably benefit from reading through a Haskell tutorial or two, which will help get you familiar with the syntax and way of thinking about Haskell - for example, check out http://learnyouahaskell.com/.
If you have to do more Haskell in the future, the book Programming in Haskell is a pretty concise and nice introduction, that also tells you how to properly read Haskell. Here's an example from the book:
abs n |n >= 0 = n
| otherwise = −n
The symbol | is read as “such that”, and the guard otherwise is defined in the standard prelude simply by otherwise = True.
精彩评论