|
3 | 3 | I am using the library written by Alexander Thiemann at:
|
4 | 4 |
|
5 | 5 | https://github.com/agrafix/openai-hs/tree/main/openai-hs
|
| 6 | + |
| 7 | +If I just print: |
| 8 | + |
| 9 | +``` |
| 10 | + case result of |
| 11 | + Left failure -> print failure |
| 12 | + Right success -> print $ chrChoices success |
| 13 | +``` |
| 14 | + |
| 15 | +then the output looks like this: |
| 16 | + |
| 17 | +``` |
| 18 | +[ChatChoice {chchIndex = 0, chchMessage = ChatMessage {chmContent = Just "Certainly! Here is a simple "Hello, World!" program in Haskell:\n\nhaskell\nmain :: IO ()\nmain = putStrLn \"Hello, World!\"\n\n\nTo run this program, follow these steps:\n\n1. Save the code in a file with a .hs extension, for example, HelloWorld.hs.\n2. Open a terminal and navigate to the directory where you saved the file.\n3. Compile the program using the Glasgow Haskell Compiler (GHC) by running:\n sh\n ghc HelloWorld.hs\n \n4. This will produce an executable file named HelloWorld (or HelloWorld.exe on Windows).\n5. Run the executable by typing:\n sh\n ./HelloWorld\n \n\nYou should see the output:\n\nHello, World!\n", chmRole = "assistant", chmFunctionCall = Nothing, chmName = Nothing}, chchFinishReason = Just "stop"}] |
| 19 | +``` |
| 20 | + |
| 21 | +If I print: |
| 22 | + |
| 23 | +``` |
| 24 | + Right success -> -- print $ chrChoices success |
| 25 | + case chrChoices success of |
| 26 | + (ChatChoice {chchMessage = ChatMessage {chmContent = content}}:_) -> |
| 27 | + putStrLn $ fromMaybe "No content" $ T.unpack <$> content |
| 28 | + _ -> putStrLn "No choices returned" |
| 29 | +``` |
| 30 | + |
| 31 | +then the putput looks like this: |
| 32 | + |
| 33 | +``` |
| 34 | +Certainly! Here's a simple "Hello, World!" program in Haskell: |
| 35 | +
|
| 36 | +```haskell |
| 37 | +main :: IO () |
| 38 | +main = putStrLn "Hello, World!" |
| 39 | +``` |
| 40 | + |
| 41 | +To run this program: |
| 42 | + |
| 43 | +1. Save the code in a file, for example, `HelloWorld.hs`. |
| 44 | +2. Open a terminal (command prompt). |
| 45 | +3. Navigate to the directory where you saved the file. |
| 46 | +4. Compile the program using GHC (Glasgow Haskell Compiler): |
| 47 | + |
| 48 | + ```sh |
| 49 | + ghc --make HelloWorld.hs |
| 50 | + ``` |
| 51 | + |
| 52 | +5. Run the compiled program: |
| 53 | + |
| 54 | + ```sh |
| 55 | + ./HelloWorld |
| 56 | + ``` |
| 57 | + |
| 58 | +You should see the output: |
| 59 | + |
| 60 | +``` |
| 61 | +Hello, World! |
| 62 | +``` |
| 63 | + |
| 64 | +Alternatively, you can run the Haskell code without compiling by using an interpreter like GHCi (the interactive environment for Haskell): |
| 65 | + |
| 66 | +1. Open a terminal. |
| 67 | +2. Start GHCi by typing `ghci`. |
| 68 | +3. Load your file with the following command: |
| 69 | + |
| 70 | + ```sh |
| 71 | + :load HelloWorld.hs |
| 72 | + ``` |
| 73 | + |
| 74 | +4. Run the `main` function: |
| 75 | + |
| 76 | + ```sh |
| 77 | + main |
| 78 | + ``` |
| 79 | + |
| 80 | +This should also produce the output "Hello, World!" in the terminal. |
| 81 | +``` |
0 commit comments