Active1 year, 11 months ago
![Search assistant Search assistant](https://image.shutterstock.com/image-vector/businesswoman-hold-phone-intelligent-voice-260nw-1154855944.jpg)
Public Key Cryptography and the RSA System Suppose your best friend moves to Californiaand you want to communicatevia email. If you want to be sure that no one can intercept and read your messages, you’ll want to encipher them in some way. If you decide to use a. ECS 170 Introduction to Artificial Intelligence Final Examination, Open Text Book and Open Class Notes. Answer All questions on the question paper in the spaces provided Add additional sheets if necessary Time: 2 hours (1) Quickies (12 points) Decide if each of the following is True or False. Please provide a brief justification to your answer.
$begingroup$I currently try to learn some Haskell to improve my overall programming skills. To practice, I implement the C course exercises that I very familiar with. One is this:
Write a function that finds an array (needle) inside of another array (haystack). Return the index where the first match starts. If it cannot be found, return
-1
.So in C, this is implemented using a couple of loops:
The following is my attempt in Haskell. I made comments which probably state the obvious for Haskell experts but it should make my reasoning clear.
Since there are so many lines and I have a bunch of helper functions, I wonder whether there is some better way‽
Martin UedingMartin Ueding
$endgroup$1 Answer
$begingroup$You use explicit recursion, which is fun, but not always necessary.
arraySearchBegin
can be expressed with zipWith
and and
, for example:But that's still to explicit.
Data.List
contains isPrefixOf
, which is exactly what we need:That being said,
arraySearchBegin
's name is misleading. [a]
is a linked list, not an array. We should probably rename it, but we won't use it in the final variant of arraySearch
.Next, we don't need the
Maybe Int
in arraySearchNext
:We can stop as soon as our needle is a prefix of our haystack. However, we're basically checking all tails of
hss
here. And there is again a function in Data.List
that yields those tails
for us:We therefore just check which one is the first list in
tails haystack
that can be prefixed by needle
. If we zip
the indexes with the tails
we're able to write arraySearch
as a list comprehension:But
Data.List
contains findIndex :: (a -> Bool) -> [a] -> Maybe Int
, which brings us to:![Phoneky Phoneky](https://image.shutterstock.com/image-vector/arabic-businessman-hold-phone-intelligent-260nw-1177201480.jpg)
This however needs knowledge of the standard library. So how do you find those functions if you don't know them yet? Welcome to Hoogle. We can find
findIndex
by searching for (a -> Bool) -> [a] -> Maybe Int
, isPrefixOf
by Eq a => [a] -> [a] -> Bool
and tails
by [a] -> [[a]]
.If we didn't have any of those helpers, your code would be fine, except for the strange explicit result in
ZetaZetaarraySearchNext
's first argument. That was too complex, as you will stop as soon as you find a Just
either way.16k22 gold badges4040 silver badges7676 bronze badges