haskell - All Same Lengths Function -
haskell - All Same Lengths Function -
i'm trying write function [a] -> bool
returns true when elements of list have same lengths.
i tried using list recursion , extracted first 2 elements of list x : y : xs
compared length of x , y length x == length y
case determination. however, ghci
complains types of x , y:
couldn't match expected type `a' actual type `[a0]' `a' stiff type variable bound type signature [a] -> bool in first argument of `length', namely `x' in first argument of `(==)', namely `length x' in expression: length x == length y
why happening , how prepare it? clearly, length :: [a] -> int == length :: [a] -> int
returns bool
correct. in fact, replacing look true
compiles.
we can simply, actually.
samelengths :: [[a]] -> bool samelengths [] = true samelengths (x:xs) = (\y -> length y == length x) xs
no need recursion - of lengths same iff equal first one, extract first , utilize all
check rest.
as why can't define [a]
, remember [string]
[[a]]
[a]
. need [[a]]
because length
must defined whatever a
in [a]
.
haskell
Comments
Post a Comment