简体   繁体   中英

How to use user defined data as param in haskell?

I am new to haskell

I have this program:

loop height weight=do
    line<-getLine
    if line=="1" then
        do
            height<-getLine
            loop height weight
    else if line=="2" then
        do 
            weight<-getLine
            loop height weight
        else if line=="3" then
            do
                putStrLn (height)
            else
                do
                    putStrLn "error"

main :: IO ()
main = do
    loop "" ""

It works fine,now I change it as this:

data A=A {height::String,weight::String}

loop::A->IO ()
loop z=do
line<-getLine
if line=="1" then
    do
        height<-getLine
        let z.height=height
        loop z
else if line=="2" then
    do 
        weight<-getLine
        let z.weight=weight
        loop z
    else if line=="3" then
        do
            putStrLn ((height z)++(weight z))
        else
            do
                putStrLn "error"

main :: IO ()
main = do
    let z=A "" ""
    loop z

this program is wrong I know in haskell and other function program there is no assignment,so let z.height=height is wrong,but I don't know how to write it right.I need some help,thanks!

You can use the record update notation:

if line=="1" then
    do
        height'<-getLine
        loop z{height = height'}

Basically, z{height = height'} is a new record value, having the same fields as z except for the updated one. In this case, it is equivalent to A height' (weight z) .

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