Talk:Haskell/More about lists

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

Contents

[edit] List comprehensions

I'm reading the book from the beginning, and the List comprehensions part seems way too complicated: particularly because of "(we'll assume that a test for evenness exists called ifEven):" is a bad assumption ;-). If I can't try out the code, I can't experiment with it, and I can't understand it. --gerymate

That any better now? --Gwern (contribs) 18:40, 27 October 2006 (UTC)
Better for me, but I think, list comprehensions are way too high level for this stage. At this point we haven't learnt control structures, neither
    | this
    | that

statements, nor the term "otherwise". "mod" is also new here, but it's all right... I think, list comprehensions shouldn't be in the 'Haskell Basics' section. --gerymate

Oh. I didn't even realize that guards hadn't been covered yet. As for mod... it might be rewritable in terms of regular old division (since that's what mod really is). As for 'otherwise', isn't that pretty clear? --Gwern (contribs) 02:09, 28 October 2006 (UTC)

[edit] Missing Material

Shouldn't the discussion of lists include a mention of the "head" and "tail" functions? I was completely lost trying to do what I thought was simple list processing (with my only exposure to Haskell so far being this tutorial), until another reference (http://www.cs.utah.edu/~hal/docs/daume02yaht.pdf) mentioned those two functions. I knew there had to be some car/cdr equivalent, and just as car/cdr are among the first Lisp functions you would learn, shouldn't head/tail be mentioned here?

[edit] Solutions

I'm learning Haskell, so I decided to do the exercises on this page.

 -- 1.
 takeInt :: Integer -> [Integer] -> [Integer]
 takeInt 0 _ = []
 takeInt m (n:ns) = n : takeInt (m-1) ns
 main = print (takeInt 4 [11,21,31,41,51,61])

 -- 2.
 dropInt :: Integer -> [Integer] -> [Integer]
 dropInt 0 n = n
 dropInt m (n:ns) = dropInt (m-1) ns
 main = print (dropInt 3 [11,21,31,41,51])

 -- 3.
 sumInt :: [Integer] -> Integer
 sumInt [] = 0
 sumInt (n:ns) = n + (sumInt ns)
 main = print (sumInt [1..5])

 -- 4.
 scanSum :: [Integer] -> [Integer]
 scanSum n = scanSum' n 0
 scanSum' [] _ = []
 scanSum' (n:ns) a = n+a : (scanSum' ns (n+a))
 main = print (scanSum [2,3,4,5])

 -- 5.
 diffs :: [Integer] -> [Integer]
 diffs [] = []
 diffs (m:[]) = []
 diffs (m:n:ns) = n-m : (diffs (n:ns))
 main = print (diffs [3,5,6,8])

--Colonel panic 21:14, 31 January 2006 (UTC)

Maybe something like this is what the author was looking for, based on the hint:

 -- 5.
 diffs' :: [Integer] -> [Integer] -> [Integer]
 diffs' _ [] = []
 diffs' [] _ = []
 diffs' (a:ax) (b:bx) = (b-a) : diffs' ax bx
 diffs :: [Integer] -> [Integer]
 diffs [] = []
 diffs (a:ax) = diffs' (a:ax) ax
 

-- Fshahriar 23:44, 7 May 2006 (UTC)

I think it would be more intuitive to substract the second from first element, and so on. At least when I read "difference between adjacent elements" I immediatly thought that [3,5,6,8] should produce [-2,-1,-2], only the example clarifies that.

-- Moritz, 21 May 2007 (UTC)

Hi folks, would you maybe consider updating the solutions page to this module? Thanks -- Kowey 11:28, 21 May 2007 (UTC)

[edit] Alternate Solution for scanSum

There's a solution for creating scanSum with only one function. The trick is in the pattern matching - "(x:y:zs)" will match the first element in a set, the second element, and the remainder.

[edit] Aborting evaluation

The note about aborting evaluation is useful here, but it may be a good idea to also include it in an earlier chapter, where factorial is introduced. In that chapter, it mentions that you can get into an infinite loop by feeding in a negative number, but it doesn't tell you how to abort. Also, it may be a good idea to spell out the keypress, in the case that the reader doesn't know what "C-c" means. 65.201.194.91 23:13, 22 June 2007 (UTC)