简体   繁体   中英

Implementation of Recursive Functions in Haskell

I am learning haskell at the moment and I'm having troubles with my logic, especially how to go about performing certain actions.

What I'm trying to do

  1. The main goal is to have a function that contains multiple sentences (statements) with missing words, but, it only needs to print one sentence at a time.
  2. We also need to have another function that prints out 4 options that will fill out a complete sentence for each statement that the first function contains/prints (These options are then chosen by the user to fill out the sentence).
    Eg. - "This ___ contains ____"
    Options:
    1. String, words
    2. List, options
    3. etc
    4. etc
  3. Now we need another function that will take each sentence from function 1, and the options that the user selects from function 2, and create a complete sentence and returns it to the user.

I'm unsure whether we need a separate function that will take the user input and store the value that they choose from function 2 to complete the sentence from function 1, or if we can just add this in function 2 also.

What I was able to create

I was able to perform the actions stated above, but I was only able to do this for one sentence, and all of the actions were in a single function (not making the code efficient and reusable in my opinion).
I tried again with the structure I have in mind (Shown Above) but I am stuck as I said with the logic and unsure of how to go about it. Below I have the 2 versions of code I did, The first version is showcasing what I am aiming to do, but I was only able to do it with one sentence, and the second version is where I am right now with trying to use multiple functions, but I'm unsure of where to go next

Here's the code for the first version

--First Version
import Data.List
import System.IO

main :: IO()

sentences = do
    putStrLn "The Cat is ______ from ______ the city \n"
    putStrLn "Here are your options:"
    putStrLn "A. big, nearby"
    putStrLn "B. Nearby, in"
    putStrLn "C: You, By"
    putStrLn "D: By, Yourself"
    option <- getChar
    if (option == 'A' || option == 'a')
        then putStrLn "The Cat is big from nearby the city"
    else if (option == 'B' || option == 'b')
        then putStrLn "The Cat is nearby from in the city"
    else putStrLn "Error"

main = sentences    

Here's the code for the second version (Where I am)

import Data.List
import System.IO
main :: IO()

--This function contains all the sentences 
sentences = do
    putStrLn "\nThe Cat is ______ from the ______  \n"
    putStrLn "\nThe Cow belongs to ______ from ______ ______ \n"
    putStrLn "\nThe Man lives in ______ and is neighbours with ______ \n"

-- This function basically prints after each sentence is displayed to signal to the user that they need to select an option
optionsText = do
    putStrLn "Here are your options: \n"

-- These Functions contain the different options for different sentences
options1 = do
    putStrLn "A. Running, dog"
    putStrLn "B. Hiding, Man"
    putStrLn "C. Eating, Trash"
    putStrLn "D. Calling, Roof"

options2 = do
    putStrLn "A. Tom, Next, Door"
    putStrLn "B. Rick, My, Neighbour"
    putStrLn "C. Man, farm, place"
    putStrLn "D. Sheltor, Animal, Factory"

option3 = do
    putStrLn "A. Australia, Me"
    putStrLn "B. UK, Actor"
    putStrLn "C. Florida, Tom"
    putStrLn "D. House, Dog"

This seems to be a question about data modelling. Your solution so far operates on the level of characters: You define strings where a specific character, _ acts as a placeholder, and where you want to insert other characters in this place.

Programming is about abstraction. Thus, take a step back and think of your problem not in terms of individual characters, but in terms of sentence fragments and placeholders . Sentence fragments and placeholders make up a sentence . Define data types for these three. Then, look at the functionality you need. You want functions that render sentence fragments and placeholders for display on screen, and you need a function that can combine sentence fragments and placeholders into sentences. Finally, you need a function to display an entire sentence in terms of the display functions of the fragments and placeholders.

Once you have this abstraction in place, you can replace a placeholder by another sentence fragment and display it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM