获取&lt; <loop>&gt; Haskell中的异常取决于模式排列

时间:2016-11-13 03:17:31

标签: haskell

有人可以解释为什么我在这段代码中得到一个循环?

module Main where

import Data.List.Split
import Data.Maybe
import Text.Read

main :: IO ()
main = print (snd (toInmetDate "01/02/2012"))

type P a = (Bool, a)

readP :: (Read a) => String -> P a
readP text
  | isJust value    = (True, fromJust value)
  | isNothing value = (False, read "0")
  where value = readMaybe text

data InmetDate = InmetDate {dia :: P Int, mes :: P Int, ano :: P Integer}
  deriving (Show, Read)

toInmetDate :: String -> P InmetDate
toInmetDate x = if length l == 3
  then  (True, InmetDate (readP ds) (readP ms) (readP as))
  else  (False, InmetDate (False, 0) (False, 0) (False, 0))
  where (l,ds:ms:as:_) = (splitOn "/" x, l ++ replicate 20 "NULL")

我会说,当我做:

  where (l,ds:ms:as:_) = (splitOn "/" x, l ++ replicate 20 "NULL")

等于:

  where (ds:ms:as:_) = l ++ replicate 20 "NULL"
        l = splitOn "/" x

代码完美无缺。

1 个答案:

答案 0 :(得分:3)

在原始版本中,len(names) - 2的值取决于以FINAL ALGORITHM START OUTPUT "Enter the number of parcels" INPUT NUMBEROFPARCELS INTEGER PRICE = 0 INTEGER PARCELWEIGHT [1:NUMBEROFPARCELS] INTEGER TOTALPRICE = 0 FOR PARCELLOOP = 1 TO NUMBEROFPARCELS INTEGER REJECT = 0 INTEGER ACCEPT = 0 INTEGER ACCEPTWEIGHT = 0 INTEGER REJECTEDPARCELS = 0 OUTPUT "Enter the weight of the parcel in kg" INPUT WEIGHT IF (WEIGHT < 1) THEN REJECT = REJECT + 1 OUTPUT "The weight of the parcel should be atleast 1kg" ELSE IF (WEIGHT > 10) THEN REJECT = REJECT + 1 OUTPUT "The weight of the parcel should be less than 10kg" ENDIF IF (WEIGHT > 1) THEN IF (WEIGHT < 10) THEN PARCELWEIGHT[PARCELLOOP] = WEIGHT ENDIF ENDIF OUTPUT "Enter the first dimension of the parcel in cm" INPUT DIMENSION1 IF (DIMENSION1 > 80 ) THEN REJECT = REJECT + 1 OUTPUT "Each dimension of the parcel should be less than 80" ENDIF OUTPUT "Enter the second dimension of the parcel in cm" INPUT DIMENSION2 IF (DIMENSION2 > 80 ) THEN REJECT = REJECT + 1 OUTPUT "Each dimension of the parcel should be less than 80" ENDIF OUTPUT "Enter the third dimension of the parcel in cm" INPUT DIMENSION3 IF (DIMENSION3 > 80 ) THEN REJECT = REJECT + 1 OUTPUT "Each dimension of the parcel should be less than 80" ENDIF TOTALDIMENSION = DIMENSION1 + DIMENSION2 + DIMENSION3 IF (TOTALDIMENSION > 200 ) THEN REJECT = REJECT + 1 OUTPUT "The size of the parcel should be less than 200cm" ENDIF IF (REJECT > 0 ) THEN OUTPUT "Your parcel has been rejected for the reasons above" REJECTEDPARCELS = REJECTEDPARCELS + 1 ENDIF IF (REJECT = 0)THEN OUTPUT "Your parcel has been accepted" ACCEPT = ACCEPT + 1 ACCEPTWEIGHT = ACCEPTWEIGHT + WEIGHT END IF INTEGER PARCELSACCEPTED = ACCEPT INTEGER TOTALWEIGHT = ACCEPTWEIGHT INTEGER PARCELSREJECTED = REJECTEDPARCELS OUTPUT "The number of parcels accepted is " PARCELSACCEPTED " and the total weight of the parcels is " TOTALWEIGHT OUTPUT "The number of parcels rejected is " PARCELSREJECTED NEXT PARCELLOOP FOR PRICELOOP = 1 TO NUMBEROFPARCELS IF (PARCELWEIGHT[PARCELLOOP] < 5) THEN PRICE = PRICE + 10 TOTALPRICE = TOTALPRICE +PRICE END IF IF (PARCELWEIGHT[PARCELLOOP] > 5) THEN PRICE = ((PARCELWEIGHT[PARCELLOOP] - 5)*0.10)/100 TOTALPRICE = TOTALPRICE +PRICE END IF OUTPUT "The price of the parcel is " PRICE NEXT PRICELOOP OUTPUT "The total price of all the parcels is " TOTALPRICE STOP 递归定义的元组。在更改中,l仅取决于l的价值。