[Haskell-beginners] Are these soloutions all valid and a good use of Haskell

Roelof Wobben r.wobben at home.nl
Tue Nov 11 16:35:28 UTC 2014


Oke,

But what If I try it on a left folt.
Then it must be the other way.

let's say we have [1 , 2, 3]

so first it's nothing and 1 is taken.
so y could be one and nothuing must stay nothing because it's not the 
last part,
The same for 2 , When it's three nothing must changed to just 3.

Fold is something I find it very difficult to wrap my head around it.
Maybe haskell or any functional proggramming language is not for me,

Roelof

Kees Bleijenberg schreef op 11-11-2014 15:46:
> The program traverses the list  from right to left. The first element that
> acc sees during traversal is the last element of the list.
> The idea is as follows:
> Parameter y is the partial calcresult.
> If y (in your code) is Nothing then acc returns Just x. So the new partial
> result is the current list element in a Just.
> The next time acc is called, y is not Nothing but a Just. In that case acc
> returns the partial calcresult unchanged.
> So, y is Nothing for the last list element and Just <last element of the
> list> for all other list elements.
>
> last5 :: [a] -> Maybe a
> last5 xs = foldr acc initial xs
>
> acc :: a -> Maybe a -> Maybe a
> acc x Nothing  = Just x
> acc _ aJust    = aJust
>
> initial :: Maybe a
> initial = Nothing
>
> In your code you don’t need the ‘where’ in the acc definition. Define acc
> for Nothing and for Just.
>
> Kees
>
> ---------------
> Im now trying to make a rfold solution to this problem.
>
> Till now I have this :
>
> last5 :: [a] -> Maybe a
> last5 xs = foldr acc initial xs
>
> acc :: a -> Maybe a -> Maybe a
> acc x y = where
>      acc x Nothing  = Just x
>      acc _ j        = j
>
> initial :: Maybe a
> initial = Nothing
>
> So still not working.
>
> Can someone explain to me in small steps how to figure out what the right
> answer is to the acc part.
>
> Roelof
>
>
>
>
>
>
>
>
>
> Roelof Wobben schreef op 11-11-2014 8:20:
> Thanks, this is working well.
>
> But just for understanding
>
> in the part acc x Nothing   x is the input string,
> and in the part _j means the same as (xs:x)
>
> Roelof
>   
>
>
> Alex Hammel schreef op 10-11-2014 22:47:
> It's an indentation issue. Make sure that there are no spaces before the bit
> about 'last5 = foldr acc' etc.
>
> On Mon, Nov 10, 2014 at 12:48 PM, Roelof Wobben <r.wobben at home.nl> wrote:
> Roelof Wobben schreef op 10-11-2014 20:59:
> last5 :: [a] -> Maybe a
>     last5 = foldr acc Nothing where
>         acc x Nothing  = Just x
>         acc _ j        = j
>
> When I enter this in GHCI  I see this error :
>
> Illegal type signature: ‘[a] -> Maybe a last5’
>
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>



More information about the Beginners mailing list