<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--><style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
span.EmailStyle18
{mso-style-type:personal-compose;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="#0563C1" vlink="#954F72" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal">From a colleague:<o:p></o:p></p>
<p class="MsoNormal"> GPT Pretty smart(?!).<o:p></o:p></p>
<p class="MsoNormal">But this is of course simple, and searchable.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Still…<o:p></o:p></p>
<p class="MsoNormal">--------------------------------------------------------------------------------------------------<o:p></o:p></p>
<p class="MsoNormal" style="margin-bottom:12.0pt">GPT-4 when prompted:<o:p></o:p></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><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-bottom:12.0pt">gives the following response:<o:p></o:p></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><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-bottom:12.0pt">The above code runs:<o:p></o:p></p>
<p class="MsoNormal"><b><a href="https://onecompiler.com/cpp/3z48yt2pp" target="_blank">https://onecompiler.com/cpp/3z48yt2pp</a></b><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><img border="0" width="624" height="136" style="width:6.5in;height:1.4166in" id="Picture_x0020_2" src="cid:image001.png@01D963E1.B0EB6AB0"><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</body>
</html>