<div dir="ltr"><div>Dear Gregory,</div><div><br></div><div>Can you please fix your onecompiler vanity link? It points to the C++ runtime implementation.</div><div><br></div><div>Thanks<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, 31 Mar 2023 at 22:04, Gregory Guthrie <<a href="mailto:guthrie@miu.edu">guthrie@miu.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="msg-5676484538880690120">
<div style="overflow-wrap: break-word;" lang="EN-US">
<div class="m_-5676484538880690120WordSection1">
<p class="MsoNormal">From a colleague:<u></u><u></u></p>
<p class="MsoNormal"> GPT Pretty smart(?!).<u></u><u></u></p>
<p class="MsoNormal">But this is of course simple, and searchable.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Still…<u></u><u></u></p>
<p class="MsoNormal">--------------------------------------------------------------------------------------------------<u></u><u></u></p>
<p class="MsoNormal" style="margin-bottom:12pt">GPT-4 when prompted:<u></u><u></u></p>
<p class="MsoNormal"><b>Write binary search in Haskel. Print results using putStrLn. Ensure bug-free code by specifying preconditions, postconditions, invariants, variants, internal states.</b><u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal" style="margin-bottom:12pt">gives the following response:<u></u><u></u></p>
<p class="MsoNormal"><b>-- Binary search in Haskell<br>
-- Precondition: the list is sorted in ascending order<br>
-- Postcondition: the function returns the index of the target element if found, or Nothing otherwise<br>
-- Invariant: the search space is always within the bounds of the list<br>
-- Variant: the search space decreases by half at each recursive call<br>
-- Internal state: the lower and upper bounds of the search space<br>
<br>
binarySearch :: Ord a => [a] -> a -> Maybe Int<br>
binarySearch xs x = go xs x 0 (length xs - 1)<br>
where<br>
go xs x lo hi<br>
| lo > hi = Nothing -- base case: search space is empty<br>
| x == midElem = Just mid -- base case: target element is found<br>
| x < midElem = go xs x lo (mid - 1) -- recursive case: search in the left half<br>
| otherwise = go xs x (mid + 1) hi -- recursive case: search in the right half<br>
where<br>
mid = (lo + hi) `div` 2 -- calculate the middle index<br>
midElem = xs !! mid -- get the middle element<br>
<br>
-- Print results using putStrLn<br>
main :: IO ()<br>
main = do<br>
let xs = [1, 3, 5, 7, 9] -- a sorted list of integers<br>
let x = 5 -- a target element to search for<br>
let result = binarySearch xs x -- call the binary search function<br>
case result of -- pattern match on the result<br>
Nothing -> putStrLn "Element not found" -- print a message if Nothing is returned<br>
Just i -> putStrLn $ "Element found at index " ++ show i -- print a message with the index if Just i is returned</b><u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal" style="margin-bottom:12pt">The above code runs:<u></u><u></u></p>
<p class="MsoNormal"><b><a href="https://onecompiler.com/cpp/3z48yt2pp" target="_blank">https://onecompiler.com/cpp/3z48yt2pp</a></b><u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><img style="width: 6.5in; height: 1.4166in;" id="m_-5676484538880690120Picture_x0020_2" src="cid:1873cfb9c564cff311" width="624" height="136" border="0"><u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
Only members subscribed via the mailman list are allowed to post.</div></blockquote></div><br clear="all"><br><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><b>Λ\oïs</b></div><div><div><a href="http://twitter.com/aloiscochard" target="_blank">http://twitter.com/aloiscochard</a></div><div><a href="http://github.com/aloiscochard" target="_blank">http://github.com/aloiscochard</a></div></div></div></div></div></div>