In the next function, I wonder if the compiler is sufficiently equipped to work out what xwill remain constant, or will it calculate the list title for each item in the list? (I use GHC)
x
allSame :: Eq a => [a] -> Bool allSame xs = all (==x) xs where x = head xs
The semantics of “where” in the GHC is that a single closure will be allocated for “x” and is available for all uses. A new closure will be created for the function (== 'x'), and the optimizer will pop up so that it is generated only once per bypass.
, , Core (, ghc-core). GHC :
M.allSame a eq xs = all (let ds = case xs of [] -> error "bad head" x : _-> x in \y -> x == y ) xs
, , , .
, Haskell , : x where -clause. , x all.
where
all
, myall, , all (==x), . , , , , .
myall
all (==x)
: myall .
myall x [] = [x] myall x xs = x:(myall x (tail xs)) test xs = myall (x) xs where x = head xs
test [1,2,3], , [1,1,1,1], .. x 1, myall.
test [1,2,3]
[1,1,1,1]
1
Source: https://habr.com/ru/post/1761861/More articles:Должен ли я освободить объект NSError для NSFileManager copyItemAtPath: toPath: error:? - memory-managementMagento: changing a product name or description that does not appear in the frontend - magentoDatabase design help: whose child is it anyway - sqlArray.count works fine locally, but breaks on the hero - arraysSpecial folder / files for archiving? - version-controlRubyGems с документацией, исходным кодом и документацией API? - githubconstant member of a field in C # - c #persistent and readonly in c #? - c #Problem calling webservice from Silverlight in VisualStudio 2010 - silverlightUnused variable warning for each loop - iosAll Articles