[Haskell-cafe] Re: General function to count list elements?
The problem is that you must note in the type signature the fact that 'a' must be a member of typeclass Eq in order to be able to use == count :: (Eq a) = a - [a] - Int count x ys = length (filter (== x) ys) JP michael rice wrote: Is there a general function to count list elements. I'm trying this count :: a - [a] - Int count x ys = length (filter (== x) ys) with this error upon loading = [mich...@localhost ~]$ ghci count GHCi, version 6.10.1: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer ... linking ... done. Loading package base ... linking ... done. [1 of 1] Compiling Main ( count.hs, interpreted ) count.hs:2:29: Could not deduce (Eq a) from the context () arising from a use of `==' at count.hs:2:29-32 Possible fix: add (Eq a) to the context of the type signature for `count' In the first argument of `filter', namely `(== x)' In the first argument of `length', namely `(filter (== x) ys)' In the expression: length (filter (== x) ys) Failed, modules loaded: none. Prelude = Not sure what it's trying to tell me other than I need an (Eq a) somewhere. Michael ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: How the following works
I find it easier to understand when looking at the declarative meaning of the definition. There is the trivial case of the empty list, but for the non-empty list: The possible inits of the list is the empty list and the inits of the rest of the list with the head element it their front. The execution would look like this: 1. The recurrsion would get to the tail of the list and calculate its inits, [[]]. 2. Then the recursion would go back, add the (n-th) element at the front of all the previously calculated inits [[x_n]], and add the empty list, we get [[],[x_n]]. 3. The same applies for the element n-1 and we get, [x, [x_(n-1)], [x_(n-1), n]] ... I hope this helped :p JP Tsunkiet Man wrote: Hello, I can hardly imagine how the following code works: cinits :: [a] - [[a]] cinits [] = [[]] cinits (x:xs) = [] : map (x:) (cinits xs) can someone give me a good explaination? (I understand it a bit, but it's really hard for me to figure out how a map in a map function works.) Thank you for your time, Tsunkiet ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: How the following works
I don't know if the following explanation seems too basic or redundant to you, I'm sorry if that is the case, but it might help if you don't understand high order functions very well: In my description of the declarative meaning of the definition I understood this: map (x:) (cinits xs) as add the element 'x' in the front of every element (int this case, the elements are lists) of the list returned by (cinit xs) Thing of (:) as a function: (:) :: a - [a] - [a] That appends its first argument in the front of the list given as a second argument. Then (x:) is a function with type: (x:) :: [a] - [a] It is a function that appends to a fixed element x to a given list. As map f list returns a new list that is the result of applying f to every element of list, assuming list is a list of lists, map (x:) list returns a new list that has 'x' in the front of every element. An example: map (1:) [[2], [3], [4]] Prelude map (1:) [[2], [3], [4]] [[1,2],[1,3],[1,4]] Because: map f [[2], [3], [4]] = [f [2], f [3], f [4]] So let f = (x:) then: map f [[2], [3], [4]] = [(1:) [2], (1:) [3], (1:) [4]] = [[1, 2], [1, 3], [1, 4]] JP Juan Pedro Bolivar Puente wrote: I find it easier to understand when looking at the declarative meaning of the definition. There is the trivial case of the empty list, but for the non-empty list: The possible inits of the list is the empty list and the inits of the rest of the list with the head element it their front. The execution would look like this: 1. The recurrsion would get to the tail of the list and calculate its inits, [[]]. 2. Then the recursion would go back, add the (n-th) element at the front of all the previously calculated inits [[x_n]], and add the empty list, we get [[],[x_n]]. 3. The same applies for the element n-1 and we get, [x, [x_(n-1)], [x_(n-1), n]] ... I hope this helped :p JP Tsunkiet Man wrote: Hello, I can hardly imagine how the following code works: cinits :: [a] - [[a]] cinits [] = [[]] cinits (x:xs) = [] : map (x:) (cinits xs) can someone give me a good explaination? (I understand it a bit, but it's really hard for me to figure out how a map in a map function works.) Thank you for your time, Tsunkiet ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Best text editor
and Visual Haskell had some unique features (such as hovering tooltips showing types) that are now found in the F# editor, and should now be easier to implement with the recent GHC API (I guess). haskell-mode for Emacs does show the type signature of standard functions in the mini-buffer when the cursor is over them. JP ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe