[Haskell-cafe] xs not in scope

Roman Cheplyaka roma at ro-che.info
Sun Nov 9 22:16:01 UTC 2014


You could use pattern guards.

last3 :: [a] -> Maybe a;
last3 a
  | [] <- a = Nothing
  ...

On 09/11/14 12:10, Roelof Wobben wrote:
> Oke,
> 
> So this cannot be done with guards ?
> 
> Roelof
> 
> 
> Max Voit schreef op 9-11-2014 18:04:
>> Hi Roelof,
>>
>> the problem is, you cannot pattern-match within guards. Guards take
>> predicates.
>>
>> On your example: "last3 a" pattern-matches the first argument of last3
>> into a. You may now use a in all further statements. If, however, you
>> want to do pattern matching, it would look like this:
>>
>> last3::[a]-> Maybe a;
>> last3 [] = Nothing
>> last3 ([a]) = Just a
>> last3 (_:xs) = last3 xs
>>
>> Notice that the last case will never be executed, as the matching is
>> complete with the first two case.
>>
>> Max
>>
>> Am Sun, 09 Nov 2014 17:38:26 +0100
>> schrieb Roelof Wobben <r.wobben at home.nl>:
>>
>>> Hello,
>>>
>>> Im trying to find several answers to problem1 of the 99 haskell
>>> problems. Find the last item in a list.
>>>
>>> Now im trying to find a guarded solution.
>>> So far I have this:
>>>
>>> last3::[a]-> Maybe a;
>>> last3 a
>>>     | [] = Nothing
>>>     | ([a]) = Just a
>>>     | (_:xs) = last3 xs
>>>
>>> But I see this error messages :
>>>
>>> src/Main.hs at 10:8-10:10Not in scope: xs
>>>
>>> How to solve this ?
>>>
>>> Roelof
>>>
>>>
>>> _______________________________________________
>>> Haskell-Cafe mailing list
>>> Haskell-Cafe at haskell.org
>>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
> .
> 



More information about the Haskell-Cafe mailing list