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

Pages: 1-

Haskell Question

Name: Anonymous 2007-09-05 21:29 ID:Ci6wlsOk

I'm trying to make a program in Haskell that compares two [Char] and returns a [Integer] of all the positions that the Chars are the same.

commons :: [Char] -> [Char] -> [Integer]
commons a b = case (a, b) of
        ([], _) -> []
        (_, []) -> []
        (_, _) -> fcommons 0 a b []

fcommons n s1 s2 set = case (s1, s2) of
        ([], _) -> set
        (_, []) -> set
        (a:b, c:d) -> if a==c then fcommons n+1 b d (set:n)
        else fcommons n+1 b d set

I end up getting this error:

ERROR "commons.hs":7 - Type error in function binding
*** Term           : fcommons
*** Type           : [a] -> [b] -> [b] -> a -> a
*** Does not match : [a] -> a
*** Because        : unification would give infinite type

I'm new to Haskell and have no idea why this is happening.

Name: Anonymous 2007-09-05 21:30 ID:Ci6wlsOk

Also, sorry if this is a really shitty way to write hscode.

Name: Anonymous 2007-09-05 21:53 ID:7eHhb3ll

yes, it's really shitty, so shitty it looks like a joke.


commons :: [Char] -> [Char] -> [Integer]
commons a b = fcommons 0 a b []

fcommons _ [] _ set = set
fcommons [] _ _ set = set
fcommons n (a:b) (c:d) set | a == c    = fcommons (n+1) b d (set:n)
                           | otherwise = fcommons (n+1) b d set


that is the same thing, rewritten in a good style. anyway, the error is that you're doing set:n, but it's actually n:set. why?
it's head:tail, the head is an element of the same type as the elements of the list, in this case, an integer. and tail must be a list.

still, using an accumulator is not how it's done. the "good" way (in haskell, at least) is:

commons :: [Char] -> [Char] -> [Integer]
commons a b = fcommons 0 a b

fcommons _ [] _ = []
fcommons [] _ _ = []
fcommons n (a:b) (c:d) | a == c    = n : fcommons (n+1) b d
                       | otherwise = fcommons (n+1) b d


this still can be done better, but I won't get into harder ground.

Name: Anonymous 2007-09-05 22:40 ID:Heaven

>>2
>>3
well, haskell itself is shitty, so...

ps: read sicp

Name: Anonymous 2007-09-05 23:48 ID:hpc/nh2V

Just zip it! :)

commons a b = [i|(i,eq) <- zip [0..] $ zipWith (==) a b, eq]

Name: Anonymous 2007-09-06 1:03 ID:IMCJl5DK

>>5
That's clever!  I've spent like 3 minutes or so on it until I got that eq gets either True or False and i|(i,eq) doesn't yield number if eq /= True.  Thanks >>5.  Haskell rules.

Name: Anonymous 2007-09-06 21:52 ID:DwOmTfJ8

So, breaking it down:

Prelude> zipWith (==) "abcd" "xbxde"
[False,True,False,True]

Prelude> zip [0..] $ zipWith (==) "abcd" "xbxde"
[(0,False),(1,True),(2,False),(3,True)]

Prelude> [i|(i,eq) <- zip [0..] $ zipWith (==) "abcd" "xbxde", eq]
[1,3]


So, eq will e.g. be False for the first item (and i 1). And as you say that it will skip that item.

Name: Common Lisp 2007-09-06 22:57 ID:uK1gT4wt

HAY GUISE

(loop for i from 0
      for a across "abcd"
      for b across "xbxde"
      when (eq a b) collect i)



Name: Anonymous 2007-09-06 22:59 ID:DR0KzZL7

>>8
Loop is a much more advanced language than most.

Name: Anonymous 2007-09-06 23:03 ID:uK1gT4wt

iter is more advanced than ruby and python put together

Name: Anonymous 2007-09-06 23:08 ID:uK1gT4wt

actually I think the loop example is

[i | i <- [0..] | a <- "abcd" |  b <- "xbxde", a == b]

in GHC, let the haskell masters tell me if this is true.

Name: Anonymous 2007-09-08 2:00 ID:oZdMHlDv

OP here. 

The REAL problem was that I forgot pars around the n+1s.  Well, I guess the real problem is that I'm a fucking idiot.  You've all been there once, no?

Name: Anonymous 2007-09-08 2:08 ID:VmchpAU0

I guess the real problem is that I'm a fucking idiot
that was obvious from
Haskell Question
Haskell

Name: Anonymous 2011-03-22 9:44


commons xs ys = [c | (a,b,c) <- zip3 xs ys [1..], a == b]

maximum pretty

Name: Anonymous 2011-03-22 10:32

>>13
you are just upset because you don't understand nomads

Name: Anonymous 2011-03-22 10:56

commons = (.) (length . filter (uncurry (==))) . zip

Name: Anonymous 2011-03-22 11:08

not dubs

Name: Anonymous 2011-03-22 11:09

HAX MY ANUS

Name: Anonymous 2011-03-22 18:26

>>18
you win hard
/thread

Name: Anonymous 2011-03-22 20:32

>>23
Actually, >>18 doesn't work, here's an improved version:
commons xs ys = sum [1 | (a,b) <- zip xs ys, a == b]

Name: >>24 2011-03-22 20:33

Uh, make that
commons xs ys = sum [1 | (x, y) <- zip xs ys, x == y]

Name: Anonymous 2011-03-23 17:18

>>24
It does work, actually.
quote from op
returns a [Integer] of all the positions that the Chars are the same
Yours on the other hand doesn't.

Name: Anonymous 2013-01-18 23:50

/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