Haskell element of list

elem (1,2,3) [(1,2,3)] -> works (true) elem (1,2,_) [(1,2,3)] -> doesnt work (want it return true as well)

What Im trying to do is if the first two elements of tuple matches one in the list return true.

--------------Solutions-------------

You can use the prelude function any to find out whether at least one element in a list meets a given condition (the condition in this case being "it matches the pattern (1, 2, _)").

An example for this case would be:

any (\x -> case x of (1,2,_) -> True; _ -> False) [(1,2,3),(4,5,6)]

Or a bit more concisely:

or [True | (1,2,x) <- [(1,2,3),(4,5,6)]]

You can use elem if convert the triples to pairs first:

elem (1,2) $ map (\(a,b,_) -> (a,b)) [(1,2,3),(4,5,6)]

Category:list Time:2011-06-03 Views:1
Tags: haskell list

Related post

  • Doing a binary search on some elements in Haskell 2008-11-15

    I'm trying to complete the last part of my Haskell homework and I'm stuck, my code so far: data Entry = Entry (String, String) class Lexico a where (<!), (=!), (>!) :: a -> a -> Bool instance Lexico Entry where Entry (a,_) <! Entry (b,

  • Haskell: Replacing element with a given key in an association list 2009-04-27

    I've got to make a function which is given a key (as a String), a value (as a String) and an association list of keys and values (as [(String, String)]). The function is meant to add the key/value pair on to the end of the list, and, if the key is al

  • How to move an element in a list in Haskell? 2009-06-24

    I'm reading through Learn You a Haskell and reached a spot where I'm trying to move an element in a list to the head. I've come up with what I think is the naive way and I'm curious if someone can show me what the experienced Haskell programmer would

  • Finding index of element in a list in Haskell? 2009-09-30

    I have a function in Haskell which finds the maximum value of an exponentiation from a list: prob99 = maximum $ map (\xs -> (head xs)^(head (tail xs))) numbers What I need to find is the location of this maximum value in the resultant list. How wo

  • Remove elements by index in haskell 2009-11-14

    I'm new in haskell and I'm looking for some standard functions to work with lists by indexes. My exact problem is that i want to remove 3 elements after every 5. If its not clear enough here is illustration: OOOOOXXXOOOOOXXX... I know how to write hu

  • How to get every Nth element of an infinite list in Haskell? 2010-01-08

    More specifically, how do I generate a new list of every Nth element from an existing infinite list? E.g. if the list is [5, 3, 0, 1, 8, 0, 3, 4, 0, 93, 211, 0 ...] then getting every 3rd element would result in this list [0,0,0,0,0 ...] ------------

  • In Haskell, is there a built-in function that creates a list of one element? 2010-02-03

    Looking for a built-in function that will do the following: mklist x = [x] The benefit is that I can use it in a composition to create a list of one element. Understand that (replicate 1) is available but is there a more direct function? Would be use

  • Finding The Index of Element in a List - Haskell 2010-11-25

    Giving an element in a list, which function can I use to find its index. For instance I want to find the index of 3 in list [1,2,3,4]. Which function exist in Haskell that I can use. Thanks. --------------Solutions------------- Take a look here: Find

  • Updating tuple elements of a list in Haskell 2010-12-04

    A program I am writing in Haskell has a list instance of my defined type: type Locals = [(String, Float)] I am trying to update this list by receiving a String and updating the corresponding float value, but the String might not be in the list at the

  • Duplicate each element in a Haskell List 2010-12-15

    by functions How can I duplicate every element of a list with itself twice. e.g. duplicate [1,3,5] should return [1,1,3,3,5,5] ? and replace an element by some other element in a list. e.g. replace 3 30 [1, 3 ,4 ,5, 3, 4] should return [1, 30, 4, 5,

  • Why Haskell doesn't have a single element tuple? 2011-02-14

    I'm wondering why Haskell doesn't have a single element tuple. Is it just because nobody needed it so far, or any rational reasons? I found an interesting thread in a comment at the Real World Haskell's website http://book.realworldhaskell.org/read/t

  • How can I get `n`th element from the list in Haskell? 2011-03-07

    How can I access list by index like this C code with Haskell list? int a[] = { 34, 45, 56 }; return a[1]; --------------Solutions------------- Look here, operator !!. I.e. [1,2,3]!!1 gives you 2, since lists are 0-indexed. I'm not saying that there's

  • howto print each element of a [String] with putStrLn in a function in haskell? 2011-04-14

    I want to print a list of strings like ["hallo", "world", "!"] using putStrLn so that I get the output: hello world ! How can I do this with a function? I have printMe :: [String] -> String printMe (x:xs) = but don't know what to do from here ----

  • Haskell - Accessing a Specific Element in a Tuple 2011-04-30

    Haskell-newbie reporting in. Question is as follows: In Haskell, we have fst and snd that return the first and the second elements of a 2-tuple. Why don't we have an easy way of accessing the i-th element from any tuple? Right now I have a 3-tuple, I

  • Replace individual list elements in Haskell? 2011-05-02

    I have a list of elements and I wish to update them: from this: ["Off","Off","Off","Off"] to this: ["Off","Off","On","Off"] As I am somewhat new to Haskell, I have been using (x:xs)!!y to extract and update individual components using the function: r

  • Evaluating undefined elements in Haskell data types 2011-06-25

    if I try > fst(a, b) where a, b are undefined, I get the error that b is undefined. Even on trying snd(a, b) it is b that causes the error first. I have a background in imperative programming. I am wondering if this is some kind of laziness that I

  • retrieve an element from a list of tuple with more than 2 elements (Haskell) 2011-07-04

    I'm new to Haskell and need some help on this situation. I have the following list -- create a type for bank account type AcNo = String type Name = String type City = String type Amnt = Int type AcInfo = [(AcNo, Name, City, Amnt)] -- function to get

  • Is there a ml like language(standard ml/ocaml/f#/haskell/etc) in which list elements are option types 2011-07-09

    It seems to me like [] (empty list) and None/Nothing are so similar. I was wondering if any of that family of languages had the basic list type where each element is an option and the tail guard is Nothing? Is it(not done this way, in languages that

  • Fastest way to get the last element of a list in Haskell 2011-09-11

    What is the fastest way to get the last element of a list in Haskell. Also in next iteration, I want to remove first and last element of the list. What is the most elegant way to do it? I am trying list comprehension, but that does not look very effi

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 1.720 (s). 13 q(s)