【问题标题】:Can anybody help to fix these haskell exercises?任何人都可以帮助解决这些haskell练习吗?
【发布时间】:2022-06-13 21:22:47
【问题描述】:

在这段代码中 sumDaytime 不起作用,我不知道问题所在。

data Temperature = Night Double | Daytime Double deriving (Eq,Show)

isDaytime :: Temperature -> Bool
isDaytime (Daytime y) = True
isDaytime (Night y) = False

countDaytime :: [Temperature] -> Double
countDaytime [] = 0.0
countDaytime (x:xs)
 | (isDaytime x) = 1.0 + (countDaytime xs)
 | otherwise = countDaytime xs


sumDaytime :: [Temperature] -> Double
sumDaytime [] = 0.0
sumDaytime (x:xs) 
 | (isDaytime x) =  x + (sumDaytime xs)  
 | otherwise = sumDaytime xs

daytimeAvg :: [Temperature] -> Double
daytimeAvg [] = 0.0
daytimeAvg x = sumDaytime x `div` countDaytime x

Example:
daytimeAvg [Night (-5), Night (-6), Daytime 0, Daytime 3, Daytime 5, Daytime 1, Night (-7)] == 2.25
daytimeAvg [Night 5, Night 0, Daytime 1, Daytime 10, Daytime 8, Daytime 5, Night 2] == 6.0

此代码有效,但它仅适用于一个字符,而不适用于字符串中的所有字符。

snakeToCamel :: String -> String
snakeToCamel "" = ""
snakeToCamel (x:x1:xs) 
 | x == '_' = toUpper x1 : snakeToCamel xs
 | otherwise = x:x1: snakeToCamel xs 

Example: 
snakeToCamel "apple" == "apple"
snakeToCamel "snake_to_camel" == "snakeToCamel"
snakeToCamel "snake_2_camel" == "snake2Camel"
snakeToCamel "i_am_in_snake_case" == "iAmInSnakeCase"

我不知道我的代码有什么问题。

match :: String -> String -> Maybe Char
match (x:xs) (y:ys) 
 | ' ' == y = Nothing
 | x /= y = Just x
 | x == y = match xs ys 
 | otherwise = Nothing

Example:
match "alma" "alma" == Nothing
match "alma" "asztal" == Just 'l'
match "asztal" "alma" == Just 's'
match "" "" == Nothing

【问题讨论】:

  • 请比“不工作,我不知道问题”更具体。它以什么方式不起作用?它不编译吗?如果是这样,您是否收到可以与我们分享的错误?您是否得到了意想不到的结果,如果是,您期望得到什么结果?

标签: haskell


猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-03
  • 2012-10-28
  • 1970-01-01
  • 2023-04-01
相关资源
最近更新 更多