# 99 questions/Solutions/4

### From HaskellWiki

< 99 questions | Solutions(Difference between revisions)

(Add description for each type of solution. Change formatting, ensure that all code is enclosed in "haskell" braces.) |
|||

Line 1: | Line 1: | ||

(*) Find the number of elements of a list. |
(*) Find the number of elements of a list. |
||

+ | The simple, recursive solution. |
||

+ | This is similar to the <hask>length</hask> from <hask>Prelude</hask>: |
||

<haskell> |
<haskell> |
||

myLength :: [a] -> Int |
myLength :: [a] -> Int |
||

myLength [] = 0 |
myLength [] = 0 |
||

myLength (_:xs) = 1 + myLength xs |
myLength (_:xs) = 1 + myLength xs |
||

+ | </haskell> |
||

+ | The prelude for haskell 2010 can be found [http://www.haskell.org/onlinereport/haskell2010/haskellch9.html#x16-1710009 here.] |
||

− | myLength' :: [a] -> Int |
+ | Same, but now we use an "accumulator" argument. |

− | myLength' list = myLength_acc list 0 -- same, with accumulator |
+ | <haskell> |

+ | myLength :: [a] -> Int |
||

+ | myLength list = myLength_acc list 0 |
||

where |
where |
||

myLength_acc [] n = n |
myLength_acc [] n = n |
||

Line 13: | Line 17: | ||

</haskell> |
</haskell> |
||

+ | Using foldl/foldr: |
||

<haskell> |
<haskell> |
||

− | myLength' = foldl (\n _ -> n + 1) 0 |
+ | myLength :: [a] -> Int |

− | myLength'' = foldr (\_ n -> n + 1) 0 |
+ | myLength1 = foldl (\n _ -> n + 1) 0 |

− | myLength''' = foldr (\_ -> (+1)) 0 |
+ | myLength2 = foldr (\_ n -> n + 1) 0 |

− | myLength'''' = foldr ((+) . (const 1)) 0 |
+ | myLength3 = foldr (\_ -> (+1)) 0 |

− | myLength''''' = foldr (const (+1)) 0 |
+ | myLength4 = foldr ((+) . (const 1)) 0 |

− | myLength'''''' = foldl (const . (+1)) 0 |
+ | myLength5 = foldr (const (+1)) 0 |

+ | myLength6 = foldl (const . (+1)) 0 |
||

</haskell> |
</haskell> |
||

+ | We can also create an infinite list starting from 1. |
||

+ | Then we "zip" the two lists together and take the last element (which is a pair) from the result: |
||

<haskell> |
<haskell> |
||

− | myLength' xs = snd $ last $ zip xs [1..] -- Just for fun |
+ | myLength :: [a] -> Int |

− | myLength'' = snd . last . (flip zip [1..]) -- Because point-free is also fun |
+ | myLength1 xs = snd $ last $ zip xs [1..] -- Just for fun |

− | myLength''' = fst . last . zip [1..] -- same, but easier |
+ | myLength2 = snd . last . (flip zip [1..]) -- Because point-free is also fun |

+ | myLength3 = fst . last . zip [1..] -- same, but easier |
||

</haskell> |
</haskell> |
||

+ | We can also change each element into our list into a '1' and then add them all together. |
||

<haskell> |
<haskell> |
||

+ | myLength :: [a] -> Int |
||

myLength = sum . map (\_->1) |
myLength = sum . map (\_->1) |
||

</haskell> |
</haskell> |
||

− | |||

− | This is <hask>length</hask> in <hask>Prelude</hask>. |
||

− | |||

− | -- length returns the length of a finite list as an Int. |
||

− | length :: [a] -> Int |
||

− | length [] = 0 |
||

− | length (_:l) = 1 + length l |
||

− | |||

− | The prelude for haskell 2010 can be found [http://www.haskell.org/onlinereport/haskell2010/haskellch9.html#x16-1710009 here.] |
||

## Revision as of 13:04, 15 May 2014

(*) Find the number of elements of a list.

The simple, recursive solution.

This is similar to thelength

Prelude

myLength :: [a] -> Int myLength [] = 0 myLength (_:xs) = 1 + myLength xs

The prelude for haskell 2010 can be found here.

Same, but now we use an "accumulator" argument.

myLength :: [a] -> Int myLength list = myLength_acc list 0 where myLength_acc [] n = n myLength_acc (_:xs) n = myLength_acc xs (n + 1)

Using foldl/foldr:

myLength :: [a] -> Int myLength1 = foldl (\n _ -> n + 1) 0 myLength2 = foldr (\_ n -> n + 1) 0 myLength3 = foldr (\_ -> (+1)) 0 myLength4 = foldr ((+) . (const 1)) 0 myLength5 = foldr (const (+1)) 0 myLength6 = foldl (const . (+1)) 0

We can also create an infinite list starting from 1. Then we "zip" the two lists together and take the last element (which is a pair) from the result:

myLength :: [a] -> Int myLength1 xs = snd $ last $ zip xs [1..] -- Just for fun myLength2 = snd . last . (flip zip [1..]) -- Because point-free is also fun myLength3 = fst . last . zip [1..] -- same, but easier

We can also change each element into our list into a '1' and then add them all together.

myLength :: [a] -> Int myLength = sum . map (\_->1)