# Can I check the length in a guarded expresion

9 messages
Open this post in threaded view
|

## Can I check the length in a guarded expresion

 Hello, I try to make a guarded solution to find the last item in a list. I thought I could do something like this | length a == 1 = a But then I see a lot of error messages. Is there another way I can check the length of a list with guards ? Roelof _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

## Re: Can I check the length in a guarded expresion

 On Sun, Nov 09, 2014 at 07:33:09PM +0100, Roelof Wobben wrote: > Hello, > > I try to make a guarded solution to find the last item in a list. > > I thought I could do something like this > > | length a == 1 = a ^-- this is valid Haskell. > But then I see a lot of error messages. You should paste a complete example or the error or both, so people can easily see what went wrong with your code -F _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

## Re: Can I check the length in a guarded expresion

 In reply to this post by Roelof Wobben You can do that. If you're getting an error, it's from a mistake on some other line. That said, you shouldn't write it that way. length requires a traversal of the entire list, so if the list is 1000 elements long it will take 1001 steps to compute that result when two steps would do. `length (take 2 a) == 1` would be a constant time way to get the result you want, although likely not the most elegant way to solve this exercise.Perhaps this is the sort of question better suited for the haskell-beginners mailing list: https://www.haskell.org/mailman/listinfo/beginnersOn Sun, Nov 9, 2014 at 10:33 AM, Roelof Wobben wrote:Hello, I try to make a guarded solution to find the last item in a list. I thought I could do something like this | length a == 1 = a But then I see a lot of error messages. Is there another way I can check the length of a list with guards ? Roelof _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

## Re: Can I check the length in a guarded expresion

 Hello, Here is the complete solution : last3::[a]-> Maybe a; last3 a   | null a = Nothing   | length a == 1 = a   otherwise last3 (tail a) And I see these error messages : src/Main.hs@9:21-10:27 Couldn't match expected type ‘Bool -> ([a0] -> Maybe a0) -> [a] -> Maybe a’ with actual type [a] Relevant bindings include a :: [a] (bound at /home/app/isolation-runner-work/projects/75679/session.207/src/src/Main.hs:7:7) last3 :: [a] -> Maybe a (bound at /home/app/isolation-runner-work/projects/75679/session.207/src/src/Main.hs:7:1) The function a is applied to three arguments, but its type [a] has none … Bob Ippolito schreef op 9-11-2014 19:45: You can do that. If you're getting an error, it's from a mistake on some other line. That said, you shouldn't write it that way. length requires a traversal of the entire list, so if the list is 1000 elements long it will take 1001 steps to compute that result when two steps would do. `length (take 2 a) == 1` would be a constant time way to get the result you want, although likely not the most elegant way to solve this exercise. Perhaps this is the sort of question better suited for the haskell-beginners mailing list: https://www.haskell.org/mailman/listinfo/beginners On Sun, Nov 9, 2014 at 10:33 AM, Roelof Wobben wrote: Hello, I try to make a guarded solution to find the last item in a list. I thought I could do something like this | length a == 1 = a But then I see a lot of error messages. Is there another way I can check the length of a list with guards ? Roelof _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe
Open this post in threaded view
|

## Re: Can I check the length in a guarded expresion

Open this post in threaded view
|

## Re: Can I check the length in a guarded expresion

Open this post in threaded view
|

## Re: Can I check the length in a guarded expresion

Open this post in threaded view
|

## Re: Can I check the length in a guarded expresion

Open this post in threaded view
|

## Re: Can I check the length in a guarded expresion

 On 10/11/2014, at 8:07 am, Roelof Wobben <[hidden email]> wrote: > Found it. > > This is working ; > > last3::[a]-> Maybe a; > last3 a >   | null a = Nothing >   | length a == 1 = Just (head a) >   | otherwise = last3 (tail a) I believe it has already been mentioned, but what you have here is an O(|a|**2) solution. The way I’d write it is     last4 :: [a] -> Maybe a     last4 []     = Nothing     last4 (x:xs) = Just \$ loop x xs       where loop x [] = x             loop _ (y:ys) = loop y ys In ghci, *Main> :set +s *Main> last3 [1..10000] Just 10000 (0.14 secs, 13696656 bytes) *Main> last4 [1..10000] Just 10000 (0.00 secs, 3131552 bytes) *Main> last3 [1..100000] Just 100000 (11.92 secs, 33134136 bytes) *Main> last4 [1..100000] Just 100000 (0.02 secs, 15520360 bytes) You really _don’t_ want to be calling length in a loop like that. length a == 1 if and only if a == [_] Using null, head, tail is not really the Haskell Way. last3 []     = Nothing last3 [x]    = Just x last3 (_:xs) = last3 xs _______________________________________________ Haskell-Cafe mailing list [hidden email] http://www.haskell.org/mailman/listinfo/haskell-cafe