Skip to content

Type does not carry through seq. #8

Description

@indicee-michald

CAL reports a type error in the following, even though the type of r is clear.

test :: [Boolean];
test = readList ["True", "False"];

readList :: Read a => [String] -> [a];
readList xs =
    case xs of
    [] -> [];
    x : rest ->
        let
            r = read x;
        in
            r `seq` (r : readList rest);
    ;

class Read a where
    read :: String -> a;
    ;

instance Read Boolean where
    read = readBoolean;
    ;

readBoolean :: String -> Boolean;
readBoolean !str =
    if str == "True" then
        True
    else if str == "False" then
        False
    else
        error ("readBoolean: can't read '" ++ str ++ "'.");

Equivalent Haskell code:

test :: [Bool]
test = myReadList ["True", "False"]

myReadList :: Read a => [String] -> [a]
myReadList []     = []
myReadList (x:xs) = 
  let
    r = read x
  in
    r `seq` (r : myReadList xs)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions