[Haskell-cafe] GPT & Haskell

Gregory Guthrie guthrie at miu.edu
Fri Mar 31 21:15:58 UTC 2023


Yep! Programmers still have jobs.

Designers even more.

Dr. Gregory Guthrie

From: Jeffrey Brown <jeffbrown.the at gmail.com>
Sent: Friday, March 31, 2023 3:34 PM
To: Gregory Guthrie <guthrie at miu.edu>
Cc: haskell-cafe at haskell.org; Nenad Končar <nlpresearcher at gmail.com>; Haskell Community <replies+f6607596e3ee63f105e2b0327b86f062 at discourse.haskell.org>
Subject: Re: [Haskell-cafe] GPT & Haskell

For toy problems they can impress, but getting an LLM to understand a realistic codebase well enough to generate useful changes seems impossible.

On Fri, Mar 31, 2023 at 3:04 PM Gregory Guthrie <guthrie at miu.edu<mailto:guthrie at miu.edu>> wrote:
From a colleague:
  GPT Pretty smart(?!).
But this is of course simple, and searchable.

Still…
--------------------------------------------------------------------------------------------------
GPT-4 when prompted:
Write binary search in Haskel. Print results using putStrLn. Ensure bug-free code by specifying preconditions, postconditions, invariants, variants, internal states.

gives the following response:
-- Binary search in Haskell
-- Precondition: the list is sorted in ascending order
-- Postcondition: the function returns the index of the target element if found, or Nothing otherwise
-- Invariant: the search space is always within the bounds of the list
-- Variant: the search space decreases by half at each recursive call
-- Internal state: the lower and upper bounds of the search space

binarySearch :: Ord a => [a] -> a -> Maybe Int
binarySearch xs x = go xs x 0 (length xs - 1)
  where
    go xs x lo hi
      | lo > hi = Nothing -- base case: search space is empty
      | x == midElem = Just mid -- base case: target element is found
      | x < midElem = go xs x lo (mid - 1) -- recursive case: search in the left half
      | otherwise = go xs x (mid + 1) hi -- recursive case: search in the right half
      where
        mid = (lo + hi) `div` 2 -- calculate the middle index
        midElem = xs !! mid -- get the middle element

-- Print results using putStrLn
main :: IO ()
main = do
  let xs = [1, 3, 5, 7, 9] -- a sorted list of integers
  let x = 5 -- a target element to search for
  let result = binarySearch xs x -- call the binary search function
  case result of -- pattern match on the result
    Nothing -> putStrLn "Element not found" -- print a message if Nothing is returned
    Just i -> putStrLn $ "Element found at index " ++ show i -- print a message with the index if Just i is returned

The above code runs:
https://onecompiler.com/cpp/3z48yt2pp


[cid:image001.png at 01D963E7.7BCC1DB0]

_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.


--
Jeff Brown | Jeffrey Benjamin Brown
LinkedIn<https://www.linkedin.com/in/jeffreybenjaminbrown>   |   Github<https://github.com/jeffreybenjaminbrown>   |   Twitter<https://twitter.com/carelogic>  |  Facebook<https://www.facebook.com/mejeff.younotjeff>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20230331/fa515885/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.png
Type: image/png
Size: 39081 bytes
Desc: image001.png
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20230331/fa515885/attachment-0001.png>


More information about the Haskell-Cafe mailing list