Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Re: Returning Maybe Float -> Float -> Float (Theodore Lief Gannon) 2. Understanding wrap function in Connor McBride's Faking It (Ben Rogalski) ---------------------------------------------------------------------- Message: 1 Date: Mon, 8 Feb 2016 15:15:20 -0800 From: Theodore Lief Gannon <tan...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Returning Maybe Float -> Float -> Float Message-ID: <CAJoPsuC9GGGZMrrzbe9opU5KXGroMGpnn=xmd6mubjbwmfy...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" This is a great use case for Monad Maybe: runOperation :: String -> Float -> Float -> Maybe Float runOperation op x y = do op' <- operation op return $ op' x y Or coming at it from the other end, Applicative Maybe: runOperation' :: String -> Float -> Float -> Maybe Float runOperation' op x y = operation op <*> Just x <*> Just y On Mon, Feb 8, 2016 at 6:21 AM, David McBride <toa...@gmail.com> wrote: > You need to test to see whether you have a valid operation before you can > input the 2.0's. Try this: > > main = do > x <- getLine > putStrLn $ case operation x of > Nothing -> "Got nothing." > Just op -> show $ op 2.0 2.0 > > > On Mon, Feb 8, 2016 at 9:14 AM, Olivier Duhart <olivier.duh...@gmail.com> > wrote: > >> thanks Ulrik, >> >> it solves the compilation problem. But now I have an execution error >> (with GHCi) : >> >> *Main> (operation "+") 2.0 2.0 >> >> <interactive>:3:1: >> Couldn't match expected type `Double -> Double -> t' >> with actual type `Maybe (Float -> Float -> Float)' >> Relevant bindings include it :: t (bound at <interactive>:3:1) >> The function `operation' is applied to three arguments, >> but its type `String -> Maybe (Float -> Float -> Float)' >> has only one >> In the expression: (operation "+") 2.0 2.0 >> In an equation for `it': it = (operation "+") 2.0 2.0 >> >> As I understand it ghci guess that the two 2.0 parameters to my >> (operation "+") are Double and the function returned by operation expects >> Float instead, How to solve this ? >> >> I tried to replace every Float with Double in my .hs file but still >> there is a problem >> >> *Main> (operation "+") 2.0 2.0 >> >> <interactive>:3:1: >> Couldn't match expected type `Double -> Double -> t' >> with actual type `Maybe (Double -> Double -> Double)' >> Relevant bindings include it :: t (bound at <interactive>:3:1) >> The function `operation' is applied to three arguments, >> but its type `String -> Maybe (Double -> Double -> Double)' >> has only one >> In the expression: (operation "+") 2.0 2.0 >> In an equation for `it': it = (operation "+") 2.0 2.0 >> >> Could you explain me what is the -> t at the end of the expected type ? >> >> >> >> Le lun. 8 f?vr. 2016 ? 15:07, Ulrik Rasmussen <hask...@utr.dk> a ?crit : >> >>> On 2016-02-08 14:54, Olivier Duhart wrote: >>> > hello, >>> > >>> > I am starting with Haskell and trying some little exercices on my own. >>> I >>> > successfully implemented a reverse polish notation evaluator and I want >>> > to improve it a little using *Maybe* >>> > * >>> > * >>> > All i want is to implement a function that can returned available >>> > functions according to its string name >>> > i.e return (+) when gived "+" >>> > I also want to return Nothing if the operation is not available (not >>> > implemented yet). >>> > So my function should Maybe return a (float -> float -> float) >>> > >>> > My current implementation is >>> > >>> > operation :: String -> Maybe Float -> Float -> Float >>> > operation op >>> > | op == "+" = Just (+) >>> > | op == "-" = Just (-) >>> > | op == "*" = Just (*) >>> > | op == "/" = Just (/) >>> > | otherwise = Nothing >>> > >>> > >>> > but it failed to compile with the following error : >>> > >>> > rpn.hs:64:18: >>> > Couldn't match expected type `Maybe Float -> Float -> Float' >>> > with actual type `Maybe a0' >>> > In the expression: Nothing >>> > In an equation for `operation': >>> > operation op >>> > | op == "+" = Just (+) >>> > | op == "-" = Just (-) >>> > | op == "*" = Just (*) >>> > | op == "/" = Just (/) >>> > | otherwise = Nothing >>> > >>> > >>> > I don't understand the error :( Do I have to explicitly type the >>> Nothing >>> > return ? >>> > >>> > Could you guide me to a solution or explain me what I am doing wrong, >>> > please ? >>> > >>> > Thanks in advance >>> > >>> > Olivier >>> >>> Hi, >>> >>> The type String -> Maybe Float -> Float -> Float parenthesizes as >>> >>> String -> (Maybe Float) -> Float -> Float, >>> >>> but I think you meant >>> >>> String -> Maybe (Float -> Float -> Float) >>> >>> >>> /Ulrik >>> _______________________________________________ >>> Beginners mailing list >>> Beginners@haskell.org >>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >>> >> >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> >> > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160208/49f9270c/attachment-0001.html> ------------------------------ Message: 2 Date: Mon, 8 Feb 2016 23:20:04 -0500 From: Ben Rogalski <bwrogal...@gmail.com> To: beginners@haskell.org Subject: [Haskell-beginners] Understanding wrap function in Connor McBride's Faking It Message-ID: <CAL-j+nLqy2H5NQcHVdDthyg+6=yfhnx6roagpave1uohyqf...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Hello, I am working on a Vector class, heavily based on the paper "Faking It: Simulating Dependent Types in Haskell" by Connor McBride. Here is a stripped down version of my code: --- {-# LANGUAGE RankNTypes #-} import Control.Applicative import Data.Functor infixr 5 :. data Cons u t = (:.) t (u t) deriving (Show) data Nil t = Nil deriving (Show) class (Applicative v, Functor v) => Vector v where wrap :: (forall u. Vector u => s -> f (u t) -> f (Cons u t)) -> f (Nil t) -> v s -> f (v t) instance (Vector u) => Vector (Cons u) where wrap f n (x:.xs) = f x (wrap f n xs) instance Vector Nil where wrap _ n Nil = n instance (Applicative u) => Applicative (Cons u) where pure x = x:.(pure x) (f:.fs) <*> (x:.xs) = (f x):.(fs <*> xs) instance Applicative Nil where pure _ = Nil _ <*> _ = Nil instance (Functor u) => Functor (Cons u) where fmap f (x:.xs) = (f x):.(fmap f xs) instance Functor Nil where fmap _ Nil = Nil transpose :: (Vector w, Vector u) => w (u t) -> u (w t) transpose v = wrap (\x xs -> (:.) <$> x <*> xs) (pure Nil) v --- I am having trouble understanding the type signature of 'wrap'. This is how I interpret it: (function that takes an 's' and container of 'u t' and returns a container of 'Cons u t') -> (container of 'Nil') -> (a vector of 's') -> (a container with the same type as the container of Nil, but containing v t instead) What doesn't make sense to me is how the signature seems to imply that the structure of the container in the return type is the same as the structure of the container holding the Nils, but in 'transpose' the container holding Nil seems to be infinite. Can someone help me to understand what is going on here? Thanks, Ben -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160208/eaca04b2/attachment-0001.html> ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 92, Issue 12 *****************************************