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

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
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Roelof, The acc for the foldr doesn't work in foldl. But foldl is easier then foldr. foldl traverses the list from left to right. For foldl acc returns the current list element (second param) in a Just and ignores the result of the partial calculation (param 1). Walking through the list [1,2,4], acc returns for the first element Just 1, for the second element Just 2 en finally Just 4. The result of the foldl is in this case Just 4. If the foldl is over the empty list, acc is never called and foldl returns initial, so Nothing. In code: module Test() where last5 :: [a] -> Maybe a last5 xs = foldl acc initial xs acc :: Maybe a -> a -> Maybe a -- swapped params! acc _ x = Just x initial :: Maybe a initial = Nothing Kees -----Oorspronkelijk bericht----- 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
--- snip ----

Hi, Is there a way to set a conditional in a .cabal file that applies to all the build targets in the file? I have many build targets and repeating the conditional for each of them doesn't make sense. But I get an error message when doing that. For example, I can do this in my example.cabal file: ---- name: MyExample build-type: Simple -- the conditional flag I want to use Flag production Description: Attempts to get production env Default: True executable client main-is: client.hs if flag(production) CPP-Options: -DPRODUCTION executable server main-is: server.hs if flag(production) CPP-Options: -DPRODUCTION ------ So I have to specify the options once per target. I get an error message if I move the "if flag(production)" outside an "executable" block. And the docs "seem to indicate" that's not possible. Thanks, Dimitri PS. I'm using cabal "1.20.0.3"
participants (3)
-
Dimitri DeFigueiredo
-
Kees Bleijenberg
-
Roelof Wobben