Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

Haskell point-free

Name: Anonymous 2009-12-25 17:12

I have this:


ops :: [[Char] -> [Char]]
...
fun :: [Char] -> [[Char]]
fun str = zipWith ($) ops $ words str


How do I write fun in a point-free style? Dropping the argument gives me type errors.

Name: Anonymous 2009-12-25 17:14

I mean, I got this;

fun = (zipWith ($) ops).words

But it's ugly with all these parens. Is there a cleaner way?

Name: Anonymous 2009-12-25 17:17

godmaint i hate haskell

Name: sage 2009-12-25 17:18

1000 posts

Name: unburdened by purpose 2009-12-25 17:35

>>2
Remove them, duh.

>>4
That's one post.  Lrn2count.

Name: Anonymous 2009-12-25 17:59

What >>5 said. Also, can't you just write String to make your code clearer?

Name: Anonymous 2009-12-25 18:16

1000 posts

Name: Anonymous 2009-12-25 18:34

>>6
Thats not elitist enough.

Name: Over 8 Thread 2009-12-25 19:57

This thread has over 8 replies.
You can't reply anymore.

Name: Anonymous 2009-12-25 20:02

1000 posts

Name: Anonymous 2009-12-25 20:15

>>3-4,7,9-10
Why so hateful on OP? This thread is surely better than more of the half of the threads on /prog/'s front page. I don't even use Haskell, and I find it baffling that you'd want to prevent actual programming discussion.

Might I suggest you go back to /b/, and take those "sex vs programming" threads there with you too!

Name: Anonymous 2009-12-25 21:06

As long as you dont return a fucking pointer to the stack... its fine.

Have fun but its better to use pointers.

Each time some fucktarded c++ "programmer" goes arround passing huge classes as value a copy constructor dies.

Name: Anonymous 2009-12-25 21:06

As long as you dont return a fucking pointer to the stack... its fine.

Have fun but its better to use pointers.

Each time some fucktarded c++ "programmer" goes arround passing huge classes as value a copy constructor dies.

Name: Anonymous 2009-12-26 2:03

>>2
So fun = zipWith id ops . words

Remember, any time you pass ($) to a function that expects another function, you can substitute id and it will still work

Name: Anonymous 2009-12-26 2:04

>>12-13
Your dumb.

Name: Anonymous 2009-12-26 2:47

>>15
What about it?

Name: Anonymous 2010-01-07 2:51

>>14
don't get it

Name: Anonymous 2010-01-07 3:01

>>17
The fact that id x = x implies that (id x) y = (x) y, which is just a goofier (and syntactically illegal) way of saying id x y = x y.

Name: Anonymous 2010-01-07 3:51

FUCK OFF ALL YOU "HELPFUL" ASSHOLEE

Name: Anonymous 2010-01-07 4:01

>>19
Please. We are civilized here. You clearly meant: "HELPFUL" ANUSE

Name: Anonymous 2010-01-07 16:30

>>13,18
MIND=BLOWN

Name: Anonymous 2010-01-07 22:37

>>14
Why would you? ($) is just one character more and makes your intent clearer.

Name: Anonymous 2010-01-08 0:41

>>2
Function application has a higher precedence than any user-defined operator. Just remove the parens. Write sin x ^ 2, not (sin x) ^ 2. Also, please always use spaces around all binary operators, so that words . lines is consistent with Just . lines (since Just.lines means "the function lines in the module Just")

Name: Anonymous 2010-01-08 9:10

>>11
LOL!!!!!!

Name: Anonymous 2010-01-08 14:23

>>18
if it's syntactically illegal then what does it mean?

Name: Anonymous 2010-01-08 14:26

>>26
I'm no haskeller, but you I don't think you can compose type constructors with functions.

Name: Anonymous 2010-01-08 14:51

>>27
Type constructors are functions.

Name: Anonymous 2010-01-08 14:51

>>27
Wait, I fucked up there. Data constructors are functions. But you're still talking crap.

Name: Anonymous 2010-01-08 14:57

>>29
Yes, you're right. For some reason I thought he was referring to >>24, instead of >>18 and drew my conclusions based on that(with the presumption that it was wrong) Perhaps I should get my eyes tested (and learn Haskell while I'm at it)

Name: Anonymous 2010-01-08 17:05

>>18
GHCi, version 6.8.2: http://www.haskell.org/ghc/  :? for help
Loading package base ... linking ... done.
Prelude> let f = (+ 2)
Prelude> f 1
3
Prelude> (id f) 1
3

What do you mean "syntactically illegal"?

Name: Anonymous 2010-01-08 17:10

>>31
It think it's syntactically illegal to define the function as (f x) y = x y, as f would have to be a data constructor.

Name: Anonymous 2010-01-08 18:30

Prelude> let (f x) y = x y
Prelude> :t f
f :: (t -> t1) -> t -> t1
Prelude> let (f x) y = x y
Prelude> :t f
f :: (t -> t1) -> t -> t1
Prelude> :t let (f *** g) x y = (f x, g y) in (***)
let (f *** g) x y = (f x, g y) in (***)
  :: (t -> t1) -> (t2 -> t3) -> t -> t2 -> (t1, t3)
Prelude>

Name: Anonymous 2010-01-08 18:43

>>30
Learn Haskell? Have fun with that, professor...

Name: Anonymous 2010-01-08 21:23

Did you ever notice that GHCi uses faggot quotes?

Name: Anonymous 2010-01-08 22:17

>>35
I thought that was the original reason that people used them in Prgaue...

Name: Anonymous 2010-01-08 22:26

>>36
http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-5.html#%_chap_Temp_2

Educators, generals, dieticians, psychologists, and parents program. Armies, students, and some societies are programmed. An assault on large problems employs a succession of programs, most of which spring into existence en route. These programs are rife with issues that appear to be particular to the problem at hand. To appreciate programming as an intellectual activity in its own right you must turn to computer programming; you must read and write computer programs -- many of them. It doesn't matter much what the programs are about or what applications they serve. What does matter is how well they perform and how smoothly they fit with other programs in the creation of still greater programs. The programmer must seek both perfection of part and adequacy of collection. In this book the use of ``program'' is focused on the creation, execution, and study of programs written in a dialect of Lisp for execution on a digital computer. Using Lisp we restrict or limit not what we may program, but only the notation for our program descriptions.

Name: Anonymous 2010-01-08 22:32

>>36
IIRC info pages use singular faggot quotes

Name: Anonymous 2010-01-08 22:43

>>38
Who the fuck uses info pages?

Name: Anonymous 2010-01-08 22:56

>>39
Emacs users

Name: Anonymous 2010-01-08 23:34

Help me, LaTeX's also making me use faggot quotes!

Name: Anonymous 2010-01-08 23:48

>>41
That's why I have a strict policy of writing papers only in Microsoft Word.

Name: Anonymous 2010-01-09 4:50

>>41
Those are ``expert quotes''. Stop diluting this board with your newfag antics.

Name: Anonymous 2010-01-09 6:31

>>37
I love how everyone ignores you're post because it clashes with there beliefs.

    +1 Anonymous likes this.

Name: Anonymous 2010-01-09 6:39

>>44
Ihibbit :(

Name: Anonymous 2010-01-09 13:45

>>43
0/10.
Back to /b/, please.

Name: Anonymous 2010-01-09 14:39

OP back here, still learning Haskell, and Im stuck at the technical aspects of monads.

liftM :: (Monad m) => (a1 -> r) -> m a1 -> m r
Right?
So, why doesnt liftM elem (Just 'd') (Just "dongz") work? m is Maybe, a1 is Char and r is [Char]. What's wrong?

Name: Anonymous 2010-01-09 15:01

>>47
elem :: (Eq a) => a -> [a] -> Bool

How is that going to match a1 -> r?

Name: HMA FAN 2010-01-09 15:06

>>47
I chortl'd.
elem :: Eq a => a -> [a] -> Bool
Protip: liftM2

Name: 47 2010-01-09 15:25

>>48,49
FUCK

Name: Anonymous 2010-01-09 15:26

>>47
tl;dr: use liftM2

liftM :: Monad m => (a -> b) -> (m a -> m b)
elem :: (a -> ([a] -> Bool))
liftM elem :: Monad m => m a -> m ([a] -> Bool)
liftM elem (Just 'd') :: Monad m => Maybe ([a] -> Bool)


Can you see why that doesn't work? You can't apply that to Just "dongz", because it's not a function: it is a nomad with the function in his pocket. What you really want is liftM2:

liftM2 :: Monad m => (a -> b -> c) -> (m a -> m b -> m c)
liftM2 elem :: Monad m => m a -> m [a] -> m Bool
liftM2 elem (Just 'd') (Just "dongz") :: Maybe Bool


But I won't stop here, because there's another interesting way, namely the Applicative type class. I recommend the the Typeclassopedia¹ for understanding this and its relationship to the others, but here's an introduction.

class Functor f => Applicative f where
    pure :: a -> f a -- For a monad, pure = return

    -- This is why it is called an applicative functor.
    --
    -- For any monad, you can define <*> as such:
    --   f <*> x = liftM2 ($) f x
    --
    -- There are applicatives which are not monads, such as the ZipList
    -- applicative (as far as I know).
    --
    -- E.g. in the Maybe monad this is
    --   Maybe (a -> b) -> (Maybe a -> Maybe b)
    (<*>) :: f (a -> b) -> (f a -> f b)

-- This is already defined in Control.Applicative.
instance Applicative Maybe where
    pure = Just
    Just f <*> Just x = Just (f x)
    _ <*> _ = Nothing

-- Just succ <*> Just 3 = Just 4
-- [succ, (*2)] <*> [7,8] = [8,9,14,16]
-- (zip <*> tail) [1..10] = [(1,2),(2,3),(3,4),(4,5),(5,6),(6,7),(7,8),(8,9),(9,10)]

liftM elem :: Monad m => m a -> m ([a] -> Bool)
liftM elem (Just 'd') :: Maybe ([a] -> Bool)
(<*>) liftM elem (Just 'd') :: Maybe [a] -> Maybe Bool
liftM elem (Just 'd') <*> Just "dongz" :: Maybe Bool


Now, if you define <$> = liftM², you get a nice result:

elem <$> (Just 'd') <*> Just "dongz" :: Maybe Bool

Alternative, but equivalent answers:

pure elem <*> (Just 'd') <*> Just "dongz" :: Maybe Bool
elem `liftM` (Just 'd') `ap` Just "dongz" :: Maybe Bool


¹ http://haskell.org/sitewiki/images/8/85/TMR-Issue13.pdf
² Or <$> = fmap, it's the same since liftM = fmap.

Name: Anonymous 2010-01-09 15:29

>>47
import Control.Applicative
liftM elem (Just 'd') <*> (Just "dongz")

Name: Anonymous 2010-01-09 15:31

>>51
Enterprise HASKAL Programmer Detected

Now you're stuck with defining a special operator for every flavour of liftM and have several problems.

Name: Anonymous 2010-01-09 15:32

>>53
Wait, I thought you meant liftM2 but it was actually a reference. A++ nice bbcode would read again

Name: Anonymous 2010-01-09 16:13

>>51
it is a nomad with the function in his pocket.
I LOVE YOU! I LOVE YOUR POST! I READ IT FIVE TIMES! KEEP POSTING!

Awarded For: the most intuitive explanation of monads ever
good job!

Name: Anonymous 2010-01-10 21:09

bampu

Name: Anonymous 2010-02-09 21:27

>>>51

Name: Anonymous 2010-02-10 18:35

>>51
This leads me to believe that Haskell is currently solving problems that will affect society in ten years or so. I predict there's a major telecom blackout because Erlang fails to do parallel garbage collection. Maybe major bank systems will crash because of a mistake in taking the wrong locks during transactions. Or perhaps an epic fail of Amazon's databases after an incorrect typecast.
Or perhaps an epic fail
/b/tards program in Haskell. I've been living a lie.

Name: Anonymous 2010-02-10 18:38

>>58
OH MY GOD!

they´re everywhere! what if they´re in my house!

HELP ME /prog/ ! please !

Name: wedding-d 2010-07-06 4:40


I got an <a href="http://www.maternityweddingdressesshop.com" title="wholesale Wedding Dresses">maternity wedding dresses</a> engagement ring for Christmas. My <a href="http://www.maternityweddingdressesshop.com" title="wholesale Wedding Dresses">maternity wedding dresses</a> boyfriend and I had been dating for almost a year and both felt the time was <a href="http://www.maternityweddingdressesshop.com" title="wholesale Wedding Dresses">maternity wedding dresses</a>right to join our lives together in holy matrimony. htmlThe month of January was spent planning our <a href="http://www.wholesale-promgowns.com " title="wholesale Wedding Dresses">wholesale prom gowns</a> perfect Alabama June wedding. My mother, two sisters and I went to Huntsville, the closest <a href="http://www.maternityweddingdressesshop.com" title="wholesale Wedding Dresses">maternity wedding dresses</a> town with a selection of bridal shops, to buy the gown that would play the leading role on my special occasion.We <a href="http://www.wholesale-promgowns.com " title="wholesale Wedding Dresses">wholesale prom gowns</a> had a wonderful time just being together and sharing silly jokes, but the <a href="http://www.wholesale-promgowns.com " title="wholesale Wedding Dresses">wholesale prom gowns</a> day soon turned serious by afternoon: still no sign of the dress of my dreams. Both sisters were ready to give <a href="http://www.wholesale-promgowns.com " title="wholesale Wedding Dresses">wholesale prom gowns</a> up and try another day in another town, but I coerced them into one more boutique.I had a good feeling as we entered the quaint little <a href="http://www.maternityweddingdressesshop.com" title="wholesale Wedding Dresses">maternity wedding dresses</a> shop filled with the scent of fresh flowers. The elderly clerk showed us several <a href="http://www.wholesale-promgowns.com " title="wholesale Wedding Dresses">wholesale prom gowns</a> beautiful gowns in my size <a href="http://www.wholesale-promgowns.com " title="wholesale Wedding Dresses">wholesale prom gowns</a> and price range, but none were right. As I opened the door to leave, the desperate shop owner announced she had one more dress in the back that was expensive and <a href="http://www.maternityweddingdressesshop.com" title="wholesale Wedding Dresses">maternity wedding dresses</a> not even my size, but perhaps I <a href="http://www.wholesale-promgowns.com " title="wholesale Wedding Dresses">wholesale prom gowns</a> might want to look at it anyway. When she brought it out, I squealed in delight.

Name: Anonymous 2012-02-12 12:31

Cartesian product in Haskell:

(<*>) . ((,) <$>)

Point-free + Applicative = awesome.

Name: Anonymous 2012-02-12 14:12

Haskell is constipated shit.

Name: Anonymous 2013-01-18 23:41

/prog/ will be spammed continuously until further notice. we apologize for any inconvenience this may cause.

Don't change these.
Name: Email:
Entire Thread Thread List